Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

HOME

java certification tutorial

chapter1 | chapter2 | chapter3 | chapter4 | chapter5 | chapter6 |

chapter7 | chapter8 | chapter9 | chapter10 | chapter11 |

 

Chapter 2: Operators and Assignments

About operators:

  • Operators perform some function on either one or two operands or three operands.
  • Operators that require one operand are called Unary Operators.

syntax:

operator op (or)
op operator
  • Operators that require two operands are Binary Operators.

syntax:

op1 operator op2

 

  • Ternary operators are those that require three operands.

syntax:

expr ? op1 : op2

 

Operator Use Description
+
op1 + op2
Adds op1 and op2
-
op1 - op2
Subtracts op2 from op1
*
op1 * op2
Multiplies op1 by op2
/
op1 / op2
Divides op1 by op2
%
op1 % op2
computes the remainder of dividing op1 by op2
+
+op
Promotes + to int if its a byte,short or char
-
-op
Arithmetically negates op
++
op++
Increments op by 1;evaluates to value before incrementing
++
++op
Increments op by 1;evaluates to value after incrementing
--
--op
Decrements op by 1;evaluates to value before decrementing
--
op--

Decrements op by 1;evaluates to value after decrementing

 

  • A Relational Operator compares two values and determines the relationship between them.
Operator
Use
Returns true of
>
op1>op2
op1 is greater than op2
>=
op1>=op2
op1 is greater than or equal to op2
<
op1<op2
op1 is less than op2
<=
op1<=op2
op1 is less than or equal to op2
==
op1==op2
op1 and op2 are equal
!=
op1!=op2
op1 and op2 are not equal

 

Objective illustrated:
In an expression involving the operators &, |, &&, ||, and variables of known values state which operands are evaluated and the value of the expressions.

 

  • Java supports five binary conditional operators.
Operator
Use
Returns true of
&&
op1&&op2
op1 and op2 are both true,conditionally evaluates op2
||
op1||op2
either op1 or op2 is true,conditionally evaluates op2
!
!op1
op is false
&
op1&op2
op1 and op2 are both true,always evaluates op1 and op2
|
op1|op2
either op1 are op2 is true,always evaluates op1 and op2
  • Short Circuit Operators: Under specific conditions the && and || operators do not evaluate both operands.
  • case 1: In the statement if((x>0) && (y>0)). If the result of first operand (x>0) is evaluated as false then the compiler knows that the result can be false, so the right operand (y>0) is not evaluated.
  • case 2: in the statement if((x>0) || (y>0)). If the result of the first operand is evaluated as true then the compiler knows that the result cand be true, so the right operand (y>0) is not evaluated.
  • Java supports other conditional operator ?: operator. This operator is a Ternary Operator and is basically short hand for nested if-else statements.
  • The line "a = x ? b : c" is shorthand for "if (x), a = b, else a =c"
  • Bitwise Operators:A bitwise operator allows you to perform bit manipulation in data.
Operator Use Operation
>>
op1>>op2
shift bits of op1 right by distance op2
<<
op1<<op2
shift bits of op1 left by distance op2
>>>
op1>>>op2
shift bits of op1 right by distance op2(unsigned)
&
op1&op2
bitwise and
|
op1|op2
bitwise or
^
op1^op2
bitwise Xor
~
~op1
bitwise complement

 

  • The three shift operators (>>= <<= >>>) simply shift the bits of the left hand operand over by the number of positions indicated by the right hand operand.
  • The shift occurs in the direction indicated by the operator itself.

 

Ex: 13>>1
shifts the bits of the integer 13 to the right by one position.

1101 is binary representation of 13.

110 is the result of shifting one bit to right

 

byte x=-1; x = x>>>5;
the above is illegal because result of x>>>5 is int and can't be assigned to x which is byte.
x=(byte)(x>>>5) // perfectly legal

 

  • A right shift of 1 bit is equivalent to,but more effecient than, dividing the left hand operand by 2.
  • A left shift of 1 bit is equivalent to multiplying by 2.
op1
op2
op1 & op2
0
0
0
0
1
0
1
0
0
1
1
1
  • The bitwise and operator performs the 'and' function on each parallel pair of bits in each operand. The 'and' function sets the resulting bit to 1 if both operands are 1.
    Suppose you should 'and' the values 12, 13
    12-->1101 13-->1100
    12&13 1100

 

  • The | operator performs the inclusive or operation and ^ performs the exclusive or operation
  • inclusive or means that if either of the two bits are 1 then the result is 1.
The following table show the inclusive or operations
op1
op2
op1 | op2
0
0
0
0
1
1
1
0
1
1
1
1
  • Exclusive or
The following shows the results of exclusive or operations
op1
op2
op1 ^ op2
0
0
0
1
0
1
0
1
0
1
1
0
  • The ~ complement operator inverts the value of each bit of the operand.
  • If the operand bit is 1 the result is 0 and if the operand bit is 0 the result is 1.
  • Ex: complement of 111000 would be 000111

 

Objective illustrated:
Determine the result of applying any operator, including assignment operators and instanceof, to operands of any type, class, scope or accessibility or any combinations of these.

 

Conversions: some times the compiler may apply conversion to one or both of the primitive operands to ensure correct operations.

  • byte b=37; short c=2;

    int d=b+c;

    In the above example both b and c are converted to int values before performing the addition.

 

  • Widening conversions: These conversions are those that dont lose information on overall magnitude.
  • widening conversion is referred to as promotion of a number
  • Ex. The integer primitives byte,char, can all be converted to an int primitive.
  • int primitive can be converted to a long without loss of information.
The compiler converts primitives by promotion according to the following rules:
If either operand is of type double, the other is converted to double.
Otherwise , if either operand is of type float, the other is converted to float.
otherwise , if either operand is of type long the other is converted to long
in all other situations both operands are converted to int primitives

 

  • byte b=2; byte b1=3;
  • b=b*b1;
the above code will not work because before multiplying, both b and b1 will be converted to int and int can't be assigned to byte.

 

  • Widening conversions are also done in method calls.
  • Casting: conversion of operands can also be directed by a cast operator, which consists of a type enclosed in parentheses.
  • long X=0x100024L; int b=40; int c=b+(int)X;
    the compiler will change the value of X from a long to an int before adding b and assigning the result to c

     

  • Narrowing conversions: The conversion that lose significant bits are called narrowing conversions.
  • compiler always requires a specific cast for narrowing conversion
  • Compile time conversion: If the literal value is in the right range the compiler will do some narrowing conversions without requiring a specific cast.

byte X=10; // allowed

float f=10.1; // not allowed causes a compile error float f=(float)10.1; // allowed

the default for a floating point literal is a double. So the float should be explicitly casted for a float

 

some conversions that are not allowed
No integer to boolean primitive conversions are allowed
No numeric to reference variable conversions allowed
No reference to numeric conversions allowed
arrays of primitives of one type cannot be cast to another type.

 

Conversion rules in Assignments
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

 

  • Errors associated with primitives: integers are not protected for making arithmetic errors. integers may throw an ArithmeticException
  • double and float do not throw any exceptions
  • division by 0 in integer will result in ArithmeticException
  • division by 0 in floating point numbers results in positive or negitive infinity.
instanceof operator: this operator is used to test whether a reference is to a particular class, interface, or type of array.
  • instanceof type requires a type as the right operand.
  • null is not instanceof anything. All instanceof tests with null will result in false.
  • also instanceof operator cannot be used with primitives.
Objective illustrated:
Determine the result of applying the boolean equals(Object) method to objects of any combination of the classes java.lang.String, java.lang.Boolean, and java.lang.Object.

 

  • Comparision operator (==) applied on objects/ equals() method: '==' determines whether the variables reference the same object in memory rather than comparing them.
  • If you construct two strings with same literal without using new keyword then

a="Hello";

b="Hello";

if(a= =b) // this returns true

  • '==' operator returns true only if references are to same object .
  • equals() method tests for equality of content .

Ex : Long a1= new Long (100);

Long a2= new Long (100);

Long a3=a1 // a3,a1 refer to same object now.

boolean flag=(a1==a2)// false.

flag=(a1==a3) // flag is assigned true.

flag= a1.equals(a2) // flag is assigned true

  • equals() method signature is public boolean equals(Object obj)
  • equals returns true only if obj is of the same type and a comparision of the contents are identical
  • however Boolean and String classes overide this with more meaning.

String operators:

  • java interprets the + operator to mean concatenation.
  • It also attempts to convert any other primitives or references in the expression to String objects.

toString() method:

  • If a String expression contains an object reference the compiler uses that object's toString method that is inherited or overridden by every other class.

Strings are immutable:

  • When a String object is concatenated actually a new String object is created, the previously reffered String is eligible for garbage collection.

     

Copyrights anilbachi.8m.com

All rights reserved