VB and Java – Basic Syntax

Programmer
Programmer
Image by 200 Degrees from Pixabay

VB and Java have a significant difference in their basic syntax rules. But the major difference between the two is that JAVA is a case-sensitive language whereas VB is not a case-sensitive language. That means that the words ‘ if ‘ and ‘ If ‘ are the same in VB but they are not the same in JAVA and hence, JAVA compiler will show an error if you write ‘ If ‘ instead of ‘ if ‘. The second major difference is that in Java, every command line is ended with a semi-colon (;) whereas in VB, no symbol is used to end a line. Simply press ‘Enter’ in VB and the line is complete.

Variable Declaration

In Java, it is important that you declare a variable first before it can be used. Whereas in VB, if you don’t declare a variable before using it, it will not show any compiler errors but it will treat the variable as variant. But if you want that like Java, it should show errors if an undeclared variable is used, just place ‘Option Explicit’ command at the beginning of your module. Once you do this, VB compiler will show an error if an undeclared variable is used. Below is the list of the declaration syntax of some of the common variable types used in both VB and JAVA.

VB
Dim i as Integer

Java
int i

VB
Dim s as String

Java
String s

VB
Dim d as Double

Java
double d

VB
Dim f as Single

Java
float f

VB
Dim l as Long

Java
long l

Following is a detailed description of some of the major differences between the syntax of some of the major commands used in VB and Java.

For Loop

Dim i as integer
For i=1 to 12
  (processing here)
Next i

int i;
for(i=0;i<12;i++)
  (processing here)
}

You can notice in the above example that there is a considerable difference between VB and JAVA For Loops. The above example loops and performs user defined operation on data 12 times. In Java, there is a provision if you don’t want to declare a variable before using it in for loop. Replace the line ‘for (i = 0; i <12; i++)‘ with ‘for (int i = 0; i < 12; i++)‘. This statement declares ‘int i‘first and then uses it in for loop. Therefore, with this approach, you don’t need to write a declaration line ‘int i;‘.

Do Loop

Do
  (processing here)
Loop while (condition)

do {
  (processing here)
}
while (condition)

The Do Loop is quite similar in both the languages. But in VB, there are two ways of writing a do loop. One is written above and the other one is as follows:

Do While (condition)<
  (processing here)
Loop

The difference between the two is that the first one checks the looping at the end of the loop whereas the second loop checks the looping at the beginning of the loop.

IF statement

if (condition) Then
  (processing here)
else
  (processing here)
End If

if (condition) {
  (processing here)
}
else {
 (processing here)
}

You can notice than in VB, ‘If‘ statement is followed by a keyword ‘Then‘ whereas in Java, there is no such keyword. The body of the Java ‘if‘ and ‘else‘ statements is enclosed by braces (‘{}’) whereas the body of the VB ‘if‘ and ‘else‘ statement is enclosed between the keywords ‘Then‘ and ‘End If‘.

Select statement

Dim i as Integer
Select Case i
Case 1:
  (processing here)
Case 2:
  (processing here)
Case Default
  (processing here)
End Select

int i;
switch (i) {
case 1:
  (processing here)
case 2:
  (processing here)
default:
  (processing here)
}

In Java, ‘Select‘ statement is called ‘Switch‘ statement.

Functions in VB, Methods in JAVA

Every programming language provides a capability to the developer of writing routines to perform various operations. Such routines in JAVA are called ‘Methods’ and in VB, they are called ‘Functions’. The proper way of writing them in both the languages is as follows:

Function myFunction()
 (instructions here)
End Function

public void myFunction() {
 (instructions here)
}

The routines above are simple and don’t return a value. But consider a situation where you want that some operation is performed in a routine and then, a value is returned. For e.g.: If you want to write a routine which takes to numbers as parameters and calculates and returns their sum, you can do it as follows:

Function myFunction(num1 as Integer,num2 as Integer) as Integer
 Dim sum as Integer
 sum = num1 + num2
 myFunction = sum
End Function

public int myFunction(int num1,int num2) {
 int sum;
 sum = num1 + num2;
 return sum;
}

From the above example, you can notice that though the calculation code is almost the same but the way of returning a value from the function is different. In VB, function’s name is assigned the value that is to return whereas in JAVA, a return statement is used to return a value. In order to make a function value returnable in VB, use ‘as (Variable type)’ keywords post-fixed to the function’s name. For e.g. for writing a function which returns an Integer, use ‘Function myFunc() as Integer‘. If you don’t want to return any value, simply don’t post-fix anything. In Java, ‘void (meaning no value)’ keyword is used in a method if you don’t want to return any value. For returning a value, simply specify the returning variable type in place of ‘ void’. For e.g. ‘public int myFunction()‘ in order to return an Integer value.

Related Posts...
Learn different object changes from VB 6 to VB.NET in this article.
Performing file-system operations work similar in Java across platforms but differ in VB for Windows.
Exception handling is a necessary element of coding. Compare the process and methods of exception
Want to learn networking programming techniques? This article describes the differences and approaches of network
Database Access is a very important aspect of programming. Learn how to do write database
Object serialization is useful for transfer of data across heterogeneous protocols. Learn its implementation in
Do you want to implement control arrays in VB.NET despite of lack of its native
Without reporting, any data centric program is incomplete. Learn how to use crystal reports (one