IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesVB.NetLanguage Basics  Print This     

Cross Ref > Language Basics

By Mike Prestwood

VB.Net versus Java: A side by side comparison between VB.Net and Java.

 
Language Basics
 

Language basics is kind of a catch all for absolute beginner stuff. The items (common names) I chose for language basics is a bit random and include items like case sensitivity, commenting, declaring variables, etc.

Case Sensitivity

[Other Languages] 

Languages Focus

Case sensitiviy in this case is referring to commands and variable names. For example, are "printf" and "PrintF" equivalent? Are fullname and FullName equivalent? When you create commands, operations, methods, or variables should you worry about case?

VB.Net:   No

VB.Net is not case sensitive. If you type any other case for commands or variables, VB.Net will change it to the accepted or defined case. For example, if you type messagebox.show it is converted to MessageBox.Show.

Syntax Example:  

The following code works:

MessageBox.Show("hello")
Java:   Yes

Java is case sensitive.

Customary casing:

  • Classes - Initial Caps (Pascal Casing)
  • Variables - Initial lowecase (Camel case)
  • Methods - Initial lowecase (Camel case)
More Info / Comment




Code Blocks

[Other Languages] 

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

VB.Net:   End Xxx

VB.Net, like VB Classic code blocks, are surrounded by statement ending keywords that all use End such as End Class, End Sub, End Function, and End If.

Syntax Example:
Public Class Dog
End Class
 
Protected Sub MySub
End Sub
  
Public Shared Function MySharedFunction() As Integer
MySharedFunction = 12345
End Function
 
If x Then
End If
Java:   { }

Curly braces are used to bracket code blocks including classes and the methods within a class.

For Java, JavaScript, PHP, and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see moreJava codeformatted that way.

Syntax Example:
public class Dog {
  public bark() {
    System.out.println("Ruff");
  }
}




Comments

[Other Languages] 

Languages Focus

Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.

VB.Net:   ' or REM

Commenting Code
VB.Net, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB.Net does NOT have a multiple line comment.

Syntax Example:
'Single line comment.

REM Old school single line comment.

 


Java:   // or /* ... */

Commenting Code
Java uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment in MS (not ANSI compliant so do NOT use).
/* ANSI compliant single line comment. */
/*
Multiple line
comment.
*/
/*
* This is another popular way 
* to write multi-line comments.
*/




Constants

[Other Languages] 

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

VB.Net:   Const kPI Double = 3.1459

In VB.Net, you define constants with the Const keyword.

All constants are part of a class (no global constants) but you can make a constant public and have access to it using ClassName.ConstantName so long as you have added the class to the project. This works even without creating the class as if the public constants were static, but you cannot use the Shared keyword.

Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.

Syntax Example:
Public Class Convert
Inherits System.Object
 
  Public Const kName As String = "Mike"
  Private Const kPI Double = 3.1459
 
  //Declare two or more on same line too:
Const kFeetToMeter = 3.2808, kMeterToFeet = 0.3048
End Class
[Not specified yet. Coming...]




End of Statement

[Other Languages] 

Languages Focus

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

VB.Net:   Return

A return marks the end of a statement and you cannot combine statements on a single line of code. You can break a single statement into two or more code lines by using a space and underscore " _".

Syntax Example:  
Console.WriteLine("Hello1")
Console.WriteLine("Hello2")
Console.WriteLine("Hello3")

'The following commented code
'on a single line does not work...
'Console.WriteLine("Hello4") Console.WriteLine("Hello5")

'Two or more lines works too with a space+underscore:
Console.WriteLine _
"Hello6";
Java:   ;
Syntax Example:
System.out.println("Hello");




Literals

[Other Languages] 

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

VB.Net:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

To specify a floating point literal between 1 and -1, preceed the decimal with a 0. If you don't, the compiler will auto-correct your code and place a leading 0. It will change .1 to 0.1 automatically. Trailing decimals are not allowed.

Syntax Example:  
Console.WriteLine("Hello")
Console.WriteLine("Hello ""Mike"".")
  
'Does VB.Net evaluate this simple
'floating point math correctly? No! 
If (.1 + .1 + .1) = 0.3 Then
MsgBox "Correct"
Else
MsgBox "Not correct"
End If
Java:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both 1. and 1.0 work as well as .1 and 0.1). In general, Java follows the IEEE 754 Binary Floating-Point Arithmetic standard.

Syntax Example:

System.out.println("Hello");
System.out.println("Hello \"Mike\".");
  
//Does Java evaluate this simple
//floating point math correctly? No!
if ((.1 + .1 + .1) == 0.3) {
System.out.println("Correct");
} else {
System.out.println("Not correct");
}




Variables

[Other Languages] 

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

VB.Net:   Dim x As Integer=0

Variables are case sensitive but VS.Net will auto-fix your variable names to the defined case. You can declare variables in-line wherever you need them and declarative variable assignment is supported.

Syntax Example:  

Dim FullName As String

Dim Age As Integer

Dim Weight As Double

FullName = "Mike Prestwood"

Age = 32

Weight = 154.4

'Declaritive variable assignment:

Dim Married As String = "Y"

MsgBox(Married)

Java:   int x = 0;

Variable names are case sensitive.

The Java basic types are boolean, byte, short, int, long, float, double, and char. You can also declare a variable to hold a particular instance of a class such as String.

Syntax Example:

C++, Java, and C# all use C-like variable declaration.

int a;
int a, b;
int age = 43;
String FullName;




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]