Operator and Assignment

Operator produce new values from one or more operandsThe resulting values can be:

  • boolean, int, long double, other primitives or object like String
Statement A java statement ends with a semicolon.
Numeric literals Literal numbers can appear in base ten, hexadecimal and octal forms.
int n = 42;
long j = 42L //Special character as a long
= (Assignment Operators) For primitive assignment assigns values.For objects assignment assigns references.
Dimension a = new Dimension(5,10);Dimension b = a;b.height = 30;
a and b reference the same object. Changing b changes the value of a.
String x = “Java”String y = x;x = x + “Bean”
After String y = x; y and x reference the same variable
After x = x + “Bean”; A new String object is created.y and x do not reference the same variable
Since primitives are stored by value, these value remain.
int b, a;a = 10;b = a;b = 30;a is 10 and b = 30
Arithmetic Operators + – * / %
Modulus operator (%) yield remainder.
Exam Tip Using % on floating-point produces result similar to integer.However NaN and POSITIVE_INFINITY can occur.
Exam Tip Division by 0 generates the runtime exception ArithmeticException.
Taking the square root of a negative number generates a NaN.
NaN can not be compare to any objects. To determine if an float or a double is not a number use:Float.isNaN() or Double.isNan() methods.
Comparison Operators > < >= <=
instanceof The instanceof operator is used to determine if the type of object belongs to a given class or interface (or any of the superclass or superinterfaces). Test if object is in the class herarchy.
It is used on reference variables.
It returns true if the object on the right belongs to the same class hierarchy. Is it a of that class or its superclass of the object on the left.
Button b = new Button(“ada”);Boolean compare = b instanceof Button;
The instanceof operator works on objects (string, arrays and classes) or interfaces.
Exam Tip When the class are not in the same hierarchy the compiler generates the error.
Equality Operator ==!=
Primitives
  • == 5.0L return true

The int is promoted to an long.

References Test to see if the object variables refer to the same object in memory.
Increment / Decrement ++–f
Shift Operator Shift operator are used on integer only.
>> right shift shifts bits to the right producing an integer number (signed).
0x70000000 is: 1879048192:011100000000000000000000000000000×90000000 is: -1879048192:10010000000000000000000000000000Last bit, 1 on the left makes the number signee.
bef: 0xF0000000 -268435456:11110000000000000000000000000000after 0xF0000000 >> 1 -134217728:11111000000000000000000000000000bef: 0x80000000 -2147483648:10000000000000000000000000000000

 

aft: 0x80000000 >> 1 -1073741824:11000000000000000000000000000000

 

bef: 0x80000001 -2147483647:10000000000000000000000000000001

 

aft: 0x80000001 >> 1 -1073741824:11000000000000000000000000000000

 

 

bef: 0x80000000 -2147483648:10000000000000000000000000000000

 

aft: 0x80000000 >> 31 -1:11111111111111111111111111111111

 

 

The above keeps the negative signed bit

<< left shift shifts bits to the left producing an integer number. (signed)
bef: 0x80000000 -2147483648:10000000000000000000000000000000aft: 0x80000000 << 1 0:00000000000000000000000000000000bef: 0x80000001 -2147483647:10000000000000000000000000000001 

aft: 0x80000001 << 1 2:00000000000000000000000000000010

>>> right shift signed shifts bits to the right producing an integer number (unsigned).
bef: 0x80000000 -2147483648:10000000000000000000000000000000aft: 0x80000000 >>> 1 1073741824:01000000000000000000000000000000bef: 0x80000000 -2147483648:10000000000000000000000000000000

 

aft: 0x80000000 >>> 31 1:00000000000000000000000000000001

 

 

 

bef: 0x80000000 -2147483648:10000000000000000000000000000000

 

aft: 0x80000000 >>> 32 -2147483648:10000000000000000000000000000000

 

The above keeps the does not keep the asigned bit (unsigned)

Shift can only move an int 32 bits (0-31)Shift can only move an long 64 bits (0-63)
Bitwise Operators( & | ^ ) & | are used on integer or boolean values.
Logical Operators( && || ) They are used on boolean types only.
Bitwise Complement ~
Conditional (ternary) Operatos ?:
equals() It is a method of the Object class.It compares the data member (content) of the classes.
By default the equals() of the object class only compare reference.It is the same as the == operator.
Exam Tip String object overrides this method to compare values
Exam Tip StringBuffer does not override this method.It uses the object class equals.
Exam Tip equals() method takes only objects as parameters.
String Java performs optimization on Strings. When a literal string is created by several vars, java has these vars reference that one string.
String a = “java”;String b = “java”;a and b reference the same object “java”.b is reassigned to the a variable.
Even if the string is a literal in a different object or package.
class Other { static String java = “java” }
a, b and java of the Other object refer to the same object.
Exam Tip Literal java strings are optimized at compile time to refer to the same object if the literal value are the same.
String java = new String(“java”);This java would not equal a, b. Since the this java is created at runtime.
String ja = “ja”;String jv = ja + “va”;jv would not equal a, b or java var, since the jv is computed at runtime.
Exam Tip Equals test on different objects return false.Integer.equals(Long) return false.
Exam Tip Non literal values, assignments are evaluated at runtime.
Exam Tip Boolean and most of the wrapper object overrides the object equals() method.
pass-by-value: A copy of argument is made and passed to called method.
Primitive data types are passed by value.
Good for scalar (small data). Bad for large data passing.
convert(sb1,sb2);convert(StringBuffer sb1, StringBuffer sb2){sb1.insert(0,”S”);

 

sb2 = sb1;

 

}

 

  • The sb1 and sb2 or the method are local
  • sb2 reassignment of local method does not change the references to sb2 at the data member
  • sb1 reference is changed
Good for sclar (small data) . Bad for large data passed as arguments.
pass-by-reference: The caller gives called method direct access to data (modifiable).
Improves performance. No overhead of copying large data.
Poor security.
Changes made inside the call method changes the values in the outside scope.
  1. int n2 = 4096L;
  2. short s2 = 33000;
Exam Tip BecarefulLine 1. is an error it would require (int) casting.Line 2. is an error value exceeds the short value, it would require a (short) cast.
Exam Tip char A = “u0005’; //Unicode are really hexadecimalsif ( A == 0x0005L ) A is promoted to a long.Thereby the if statement is true
Exam Tip Long L = new Long ( 7 );if ( L.equals( 7L ) ) 

This would not compile, equals() method takes only objects as parameters.

Exam Tip Object A = new Long( 7 );Long L = new Long( 7 ); 

if ( A.equals( L ))

 

A refers to the Long object whose value is 7.

 

L refers to the Long object whose value is 7.

When A.equals( L ) is tested, A refers to a long object. So the if statements would return true.

Testing is based on:?same value – first?same hierarchy – second 

== means same object reference