Operators in Java

Arithmetic Operators:

The value of the post increment expression x++ is that of x, and its effect is to increment x by 1; and similarly for post decrement x--. The value of the pre increment expression ++x is that of x+1, and its effect is to increment x by 1; and similarly for pre decrement --x.

Integer division el/e2 truncates, that is, rounds toward zero, so 10/3 is 3, and (-10)/3 is -3. The integer remainder x%y equals x-(x/y)*y when y is nonzero; it has the same sign as x. Integer division or remainder by zero throws the exception Arithmetic Exception. Integer overflow does not throw an exception but wraps around. Thus, in the int type, the expression 2147483647+1 evaluates to - 2147483648, and the expression -2147483648-1 evaluates to 2147483647.

The floating-point remainder x%y roughly equals x- (((int) (x/y))*y when y is nonzero.Floating point division by zero and floating-point overflow do not throw exceptions but produce special IEEE754 values (of type float or double) such as Infinity or NaN ("not a number").

for example 10/3 output is 3

10/0 Arithmetic Exception.

0/10 output is zero

0/0 Arithmetic Exception.

0.0/0 NaN

-10/0.0f NaN

Logical Operators :

The operators == and ! = require the operand types to be compatible: one must be a subtype of the other. Two values of primitive type are equal (by = =) if they represent the same value after conversion to their common super type.

For instance, 10 and 10.0 are equal. Two values of reference type are equal (by ==) if both are null, or both are references to the same object or array, created by the same execution of the new-operator. Hence do not use == or ! = to compare strings: two strings s1 and s2 may contain the same sequence of characters and therefore be equal by s1.equals (s2), yet be distinct objects and therefore unequal by s1==s2.

The logical operators && and || perform shortcut evaluation: if e1 evaluates to true in el&&e2, then e2 is evaluated to obtain the value of the expression; otherwise e2 is ignored and the value of the expression is false. Conversely, if e1 evaluates to false in e1 || e2, then e2 is evaluated to obtain the value of the expression; otherwise e2 is ignored and the value of the expression is true. By contrast, the operators & (logical strict and) and ^ (logical strict exclusive-or) and | (logical strict or) always evaluate both operands, regardless of the value of the left-hand operand. Usually the shortcut operators && and || are preferable.

Bitwise Complement Operator: ~

The operators ~ (bitwise complement) and & (bitwise and) and ^ (bitwise exclusive-or) and | (bitwise or) may be used on operands of integer type. The operators work in parallel on all bits of the 2's complement representation of the operands.Thus ~n equals (-n) -1 and also equal (-1)^n.

Negation (~) operator can be applicable only for integral types.we can't apply this operator for the boolean type.if we apply for boolean type you can get a compile time error .
For Example ~4 output is 5.

Boolean Complement Operator: !

Applicable only for boolean type.If we will apply for integral types you can get a compile time error.

For Example ! true output is false
!4 //compile time error ,can't apply to int type.

Shift Operators:

The shift operators << >> and >>> shift the bits of the 2's complement representation of the first argument. The two operands are promoted separately, and the result type is the promotion type (int or long) of the first argument. Thus the shift operation is always performed on a 32-bit (int) or a 64-bit (long) value. In the former case, the length of the shift is between 0 and 31 as determined by the five least significant bits of the second argument; in the latter case, it is between 0 and 63 as determined by the six least significant bits of the second argument.

Assignment Expressions:

In the assignment expression x = e, the type of e must be a subtype of the type of x. The type of the expression is the same as the type of x. The assignment is executed by evaluating expression x and then e, and storing e's value in variable x, after a widening conversion if necessary.When e is a compile-time constant of type byte, char, short, or int, and x has type byte, char, or short, a narrowing conversion is done automatically, provided the value of e is within the range
representable in x . The value of the expression x = e is that of x after the assignment.

The assignment operator is right-associative, so the multiple assignment x = y = e has the same meaning as x = (y = e), that is, evaluate the expression e, assign its value to y, and then to x. When e has reference type (object type or array type), only a reference to the object or array is stored in x. Thus the assignment x = e does not copy the object or array .When x and e have the same type, the compound assignment x += e is equivalent to x = x + e;however, x is evaluated only once, so in a[i++] += e the variable i is incremented only once. When the type of x is t, different from the type of e, then x += e is equivalent to x = (t) (x + e), in which the intermediate result (x + e) is converted to type t ; again x is evaluated only once. The other compound assignment operators -=, *=, and so on, are similar.Since assignment associates to the right, and the value of sum += e is that of sum after the assignment, one can write ps[i] = sum += e to first increment sum by e and then store the result in ps[i].

Conditional Expressions:

The conditional expression e1 ? e2 : e3 is legal if e1 has type boolean, and e2 and e3 both have
numeric types, or both have type boolean, or both have compatible reference types. The conditional expression is evaluated by first evaluating el. If e1 evaluates to true, then e2 is evaluated (and not e3); otherwise e3 is evaluated. The resulting value is the value of the conditional expression.

For Example1 (10>20)?10:20;
output is 20

Example2 int a=10;
int b=20;

byte c=(a>b)?10:20;

In this case we will get a compile time error.

final int a=10;
final int b=20;

byte c=(a>b)?10:20;

output is 20

Object Creation Expressions:

The object creation expression
new C(actual-list)
creates a new object of class C and then calls that constructor in class C whose signature matches the arguments in actual-list. The actual-list is evaluated from left to right to obtain a list of argument values. These argument values are bound to the constructor's parameters, an object of the class is created in the memory, the non static fields are given default initial values according to their type, a superclass constructor is called explicitly or implicitly , all non static field initializers and initializer blocks are executed in order of appearance, and finally the constructor body is executed to initialize the object. The value of the constructor call expression is the newly created object, whose class is C. When C is an inner class in class D, and o evaluates to an object of class D, then one may create a C object inside o using the syntax o.new C (actual-list);

Number n1 = new Integer(17);

String s=new String("Java");

Instance Test Expressions:

The instance test e instanceof t is evaluated by evaluating e to a value v. If v is not null and is a
reference to an object of class C, where C is a subtype of t, the result is true; otherwise false.

e instanceof t ,the type of e and t must have some relationship i.e parent to child or child to parent ,otherwise we will get a compile time error saying "Inconvertable types"

Object o=new Object();
Number n1 = new Integer(17);
Number n2 = new Double(3.14);
String s=new String("Java");

System.out.println("n1 is a Double: " + (n1 instanceof Double)); //false

System.out.println("n2 is a Double: " + (n2 instanceof Double)); //true

System.out.println("null is a Double: " + (null instanceof Double)); //false

System.out.println("n2 is a Number: " + (n2 instanceof Number)); //true

System.out.println("o is a Object: " + (o instanceof String)); //false

System.out.println("s is a String: " + (s instanceof Object)); //true



No comments:

Post a Comment