Thursday, January 13, 2011

SCJP part-2 by Madhu Reddy

                                             Chapter 2: Operators and Assignments.



2.1. Increment and Decrement operation:
Increment operation:
1. Pre increment………….. ++x
2. Post increment……………x++
Decrement operation:
1. Pre decrement………………--x
2. Post decrement………………x- -


Expression        initial value of x        Final value of y      Final value of x
Y = ++X;                          5                                       6                             6
  Y = X++;                          5                                       5                             6
Y = --X;                            5                                       4                             4
Y = X--;                            5                                       5                             4


Increment and decrement operators we can not apply for constants. 
We should be apply  only for variables.
Ex: Y = X++; ………..Wright
Y = 5++ …Compile time exception
Final values can not be incremented and decremented violation leads to compile time
exception.


Floating point values can be incremented or decremented.
Q) byte a = 4;
     byte b = 6;
     byte c = a + b;
Compile time exception
Q) final byte a = 4;
      final byte b = 6;
      byte c = a + b;
No compile time exception, Output is 10
Q) byte a = 4;
     byte b = 6;
     byte c = (byte) (a + b);
No compile time exception, Output is 10


If we can apply arithmetic operator between two operands ‘a’ and ‘b’ the result is max
(int. type of a, type of b)
19
Example:
byte + byte = int
byte + short = int
short + byte = int
float + int = float
float + double = double
char + int = int


2.2. Shift operator:


The shift operator is very helpful in J2ME technology for getting
compression date and high resolution.
1) Left shift operator (<<).
2) Right shift operator (>>).
3) Unsigned right shift operator (>>>).


1) Left shift operator:
a<<n: Shift all the bits of ‘a’ n cells to the left hand side by filling right hand side
places with zero.
Hence a<<n = a*2^n …… (0 to 31)
= (n = n%32) if n>32
= (n = n+32) if n<0
Ex:
Q1) 4<<2 = 4*2^2 = 4*4 = 16
Q2) 8<<-3 = -3 + 32 = 29….. 8*2^29
Q3) 3<<33 = 33 % 32 = 1 …..3*2^1 = 6
Note: If ‘a’ and ‘b’ are compile time constants the compiler will performs the calculation
automatically. There is no type checking.
If these are not compiling time constants then compiler never calculates its value. It
should find its type by using MAX (int, type of ‘a’).
2) Right shift operator:
a>>n: Shift all the bits of ‘a’ n cells to right hand side by filling left hand side
places with sign bit.
Hence a>>n = a/2^n …… (0 to 31)
= (n = n%32) if n>=32
= (n = n+32) if n<0
20
3) Unsigned right shift operator:
Unsigned right shift operator (a>>>n): Shift all the bits of ‘a’ n cells to the right
side by filling left hand side cells with zero always.
If ‘a’ is positive there is no difference between unsigned right shift operator and right
shift operator.
If ‘a’ is +ve
a>>>n is a/2^n... the result of unsigned right shift operator is +ve.


Q) Which of the following is not a valid operator in java?
1. <<
2. >>
3. <<< no such operator in java (Ans)
4. >>>


Note: The shift operators we can not apply for floating point data types (float and
double). Violation leads to compile time exception.



2.3. Arithmetic operators:

       The float and double classes contain some constants for representing +ve and –ve
infinities. Hence 10/0.0 is not a runtime exception. We will get +ve infinity.
% and / these operators results arithmetic exception only for integral types.
System.out.println (0/0); gives run time exception.

For non integral type System.out.println (0/0.0); gives NaN (not a number).
If the result of arithmetic operation is not defined we will get arithmetic exception only
in integral data types.

But in the case of float and double classes for representing undefined values constants 
are defined which are NaN (not a number).




2.4. String concatenation operator:


       Almost all binary operators are left associative.
       Almost all unary operators are right associative.
int a = 10;
int b = 20;
string c = “abc”;
int d = 40;

a + b + c + d = 30 + c + d = 30abc + 40 = 30abc40
b + c + a + d = 20abc + a + d = 20abc10 + d = 20abc1040
c + a + b + d = abc10 + b + d = abc1020 + d = abc102040

At least one operand is the string type then only ‘+ ‘operator acts as concatenation
operator.


If both are number the ‘+ ‘operator acts as arithmetic operator.
System.out.println (" "+10+20+30+" ") gives 102030
System.out.println (10+20+30) gives 60.



2.5. Equality operators: 


        The return type of Equality operator is Boolean.
We can use Equality operator for comparing two numbers, two characters and two
object references also.

Equality operators for object references:


If s1 and s2 pointing to same object on the heap then s1==s2 returns true.
StringBuffer s3 = s2;
StringBuffer s1 = new StringBuffer (“abc”);
StringBuffer s2 = new StringBuffer (“bcd”);
S1--------------abc
S2--------------bcd--------------S3
S2==S3 gives true.
Sting s = “surya”
StringBuffer s1 = new StringBuffer (“surya”);
System.out.println(s==s1); give Compile time exception.

If we can apply == (or) ! = operator between two objects of different types then we
will get compile time exception saying incomparable objects.


2.6. Relational operators:


Return type of these operators is always Boolean.
We should apply these operators only for primitives.
10<20 give true
10<10.05f gives true
10<30<40 gives compile time exception.
For this type of expressions
First it will evaluate (10<30)<40 = true<40 this gives compile time exception like
operator can not be applied for Boolean and int.





2.7. Bitwise operator:


                                       & -----------AND
                                       | ------------ OR
                                      ^ ------------XOR


A ---------B--------- A&B--------- A|B--------- A^B
T--------- T--------- T ----------------T---------------F
T--------- F--------- F ----------------T --------------T
F--------- T--------- F ----------------T --------------T
---------F--------- F ---------------- --------------F
We can apply these operators not only for Boolean, for the integral types also.
4 & 5 ----- 4
4-- 100
&
5--101
-----------
100 --- 4
------------



~ Bitwise compliment operator: 


  We can apply these operators for integral types.
         We never use these operators for Boolean types.
Ex: ~4
4 = 000----------0100
Write opposite
111----------1011
Apply and operator for both then result is 1’s complement.
---------------------------
000----------0101 = 5
~4 is -5
Why it is in negative means the MSB (most significant bit is 1 that means
negative).
! Boolean complement operator:
! true = false
This operator can be applied only for Boolean types not for integral types.


2.8 Short circuit operators:


&&-------Short circuit AND operator
||----------Short circuit OR operator
These two operators applicable only for Boolean types.




Example:
class Temporary
{
public static void main (String args[])
{
int i = 10;
int j = 10;
if(++i<10 && ++j>15)
{
i++;
System.out.println(i);
System.out.println(j);
}
else{
j++;
System.out.println(i);
System.out.println(j);
}
}
}




O/p is :














1 comment:

Javin @ Tibco RV Tutorial said...

Good effort madhu keep it up. you can also put the links you liked and mock questions which you gone through.

Thanks
JAvin
How to find deadlock in Java ?