Conversion rules in Assignments :

In the description below, I am giving basic conversion rules for assignment when source and destination are of different types.

If source and destination are of the same type, assignment happens without any issues.

If source is of smaller size than destination but source and destination are of compatible types, then no casting is required. Implicit widening takes place in this case. An example is assigning an int to a long.

If source and destination are of compatible types, but source is of larger size than destination, explicit casting is required. In this case, if no casting is provided then the program does not compile.

Floating point numbers :

Decimal numbers (for example 1.3) are of type double by default. To make them of type float they must be followed by F (say, 1.3F).

The equality operator :

The equality operator (==) when applied to objects return true if two objects have same reference value, false otherwise. The example below illustrates this —

1

2

3

4

5

Stringstr1="first string";

Stringstr2=newString("first string");

Stringstr3="first string";

booleantest1=(str1==str2);

booleantest2=(str1==str3);

In the example above, test1 is set to false because str1 and str2 point to different references. As str1 and str3 point to the same reference, test2 gets set to true. When a string is initialized without using the new operator, and with an existing string, then the new string also points to the first string’s location. So in the example above, str1 and str3 point to the same pool of memory and hence test2 gets set to true. The string str2 on the other hand is created using the new operator and hence points to a different block of memory. Hence test1 gets set to false.

The conditional operators && and ||

Operator && returns true if both operands are true, false otherwise. Operator || returns false if both operands are false, true otherwise. The important thing to note about these operators is that they are short-circuited. This means that the left operand is evaluated before the right operator. If the result of the operation can be evaluated after computing the left operand, then the right side is not computed. In this respect these operators are different from their bit-wise counterparts – bit-wise and (&), and bit-wise or (|). The bit-wise operators are not short-circuited. This means both the operands of bit-wise operator are always evaluated independent of result of evaluations.

Storing integral types :

All the integer types in Java technology are internally stored in two’s complement. In two’s complement, positive numbers have their corresponding binary representation. Two’s complement representation of negative numbers is generated using the following three step process –

First get the binary representation of the number.

Then interchange zeros and ones in the binary representation.

Finally add one to the result. So for example two’s complement of -18 would be (assuming one byte representation) –

Converting 18 to binary — 0001 0010

Interchanging 0s and 1s — 1110 1101

Adding 1 — 1110 1110

So 1110 1110 would be binary representation of -18 using two bytes and using two’s complement representation.

The shift operators :

The shift left operator in Java technology is “<<“. There are two operators for doing the right shift – signed right shift (>>) and zero fill right shift (>>>).The left shift operator fills the right bits by zero. The effect of each left shift is multiplying the number by two. The example below illustrates this –

After this left shift, i becomes 52 which is same as multiplying i by 4

Zero fill shift right is represented by the symbol >>>. This operator fills the leftmost bits by zeros. So the result of applying the operator >>> is always positive. (In two’s complement representation the leftmost bit is the sign bit. If sign bit is zero, the number is positive, negative otherwise.) The example below illustrates applying the operator >>> on a number.

So the result of applying zero fill right shift operator with operand two on -11 is 1073741821.

Signed right shift operator (>>) fills the left most bit by the sign bit. The result of applying the signed shift bit has the same sign as the left operand. For positive numbers the signed right shift operator and the zero fill right shift operator both give the same results. For negative numbers, their results are different. The example below illustrates the signed right shift.