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.NetOperators  Print This     

Cross Ref > Operators

By Mike Prestwood

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

 
Operators
 

A language symbol used for assignment, comparison, computational, or as a logical.

Assignment

[Other Languages] 

Languages Focus

Common assignment operators for languages include =, ==, and :=. An assignment operator allows you to assign a value to a variable. The value can be a literal value like "Mike" or 42 or the value stored in another variable or returned by a function.

VB.Net:   =

VB.Net uses = for it's assignment operator.

Syntax Example:  
Dim Age As Integer
Dim FullName As String
  
FullName = "Randy Spitz"
Age = 38
Java:   =

Java uses = for it's assignment operator.





Comparison Operators

[Other Languages] 

General Info: Round Floating Point Numbers

When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.

Languages Focus

A comparison operator compares two values either literals as in "Hello" and 3 or variables as in X and Counter. Most languages use the same operators for comparing both numbers and strings. Perl, for example, uses separate sets of comparison operators for numbers and strings.

VB.Net:   =, <>

Save as VB Classic. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:  
//Does VB.Net evaluate the math correctly? No!
If 0.1 + 0.1 + 0.1 = 0.3 Then
MessageBox.Show("correct")
Else
MessageBox.Show("not correct")
End If
Java:   ==, !=

The Java comparison operators are:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

More Info / Comment




Empty String Check

[Other Languages] 

Languages Focus

An empty string is a zero length string, a string that is equal to null (""), or not assigned. In some languages, you can check if a string is empty by comparing it to an empty string (""). Some languages distinguish between nil and null ("") so checking if the length is 0 is easier.

VB.Net:   String.IsNullOrEmpty

In .Net, a string can be null or empty. The .Net framework offers a static method in the string class: String.IsNullOrEmpty to check if a string is null or empty.

Syntax Example:  
Dim s As String 
  
's = "" 'Uncomment to test 2nd case.
  
If String.IsNullOrEmpty(s) Then
  MessageBox.Show("hello")
End If
Java:   IsEmpty




Logical Operators

[Other Languages] 

Languages Focus

Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.

VB.Net: 

VB.Net logical operators:

And and, as in this and that No Short Circuit
AndAlso and, as in this and that short circuits
Or or, as in this or that No Short Circuit
OrElse or, as in this or that short circuits
Not Not, as in Not This
Xor either or, as in this or that but not both

Syntax Example:  
'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
  'Do something.
End If
Java: 

Java logical operators:

&& and, as in this and that
|| or, as in this or that
! Not, as in Not This
& boolean logical OR (not short circuited)
| boolean logical OR (not short circuited)
?: Ternary (short for if-then-else)
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
^ Bitwise exclusiv OR

Syntax Example:
//Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  //Do something.
}




String Concatenation

[Other Languages] 
VB.Net:  "String Concatenation" + or &

To concatenate two strings, use either the + or & operators. The & operator implicitly converts numbers. If you use the + operator to concatenate a string and a number, you have to cast the number as a string with CStr.

Alternatively, you can use the System.Text.StringBuilder class which frequently but not always provides faster code.

Syntax Example:
Dim FullName
Dim Age
  
//You can use + for strings.
FullName = "Prestwood"
Console.WriteLine("Hello " + FullName)
 
//For implicit casting, use &
Age = 35
Console.WriteLine(FullName & " is " & Age & " years old.")
'Implicit casting of numbers.
'
'This works:
MessageBox.Show(3.3)
  
'This fails:
'MessageBox.Show("" + 3.3)
  
'This works:
MessageBox.Show("" + CStr(3.3))
  
'Implicit casting &. This also works:
MessageBox.Show("" & 3.3)
Java:  "String Concatenation" + or append

In Java, you use either the String concatenation + operator or StringBulder class methods such as append. Since Java compilers frequently create intermediate objects when the + operator is used and don't when StringBuilder.append is used, the append method is faster than the + operator.

In general, use the convenience of a + operator when speed is not an issue. For example, when concatenating a small number of items and when code isn't executed very frequently. A decent rule of thumb is to use the + operator for general purpose programming and then optimize the + operator with StringBuilder.append as needed.

Syntax Example:

Simple + operator example:

System.out.println("Hello" + " " + "Mike.");

 

Using StringBuilder example:

StringBuilder myMsg = new StringBuilder();

myMsg.append("Hello ");
myMsg.append("Mike.");
 
System.out.println(myMsg);




Unary Operators

[Other Languages] 

General Info: Unary Operator

An operation with only one operand (a single input). Common unary operators include + plus, - minus, and bitwise not. Some operators can function as both unary and binary operators. For example, + and - operators can serve as either.

Languages Focus

What unary operators are supported in additoin to the standard plus, minus, and bitwise not.

VB.Net: 

An operation with only one operand (a single input). A unary operator method can return any type but takes only one parameter, which must be the type of the containing class. In addition to the obvious +, -, and Not operators, VB.Net also offers:

isFalse
isTrue

Java: 

An operation with only one operand (a single input). The Java unary operators are ++, --, +, -, ~, and !.

  • + Indicates positive value (numbers are positive without this)
  • - Negates an expression
  • ++ Increment operator by 1
  • -- Decrement operator by 1
  • ! Logical complement operator (inverts the value of a boolean)
  • ~ Bitwise inversion operator (works on integral data types)
More Info / Comment




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


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