[ ] | ( ) | |||
++ | -- | ! | ~ | |
* | / | % | ||
+ | - | << | >> | >>> |
< | > | <= | >= | |
= = | != | |||
& | ||||
^ | ||||
| | ||||
&& | ||||
|| | ||||
?: | ||||
= | += | … |
Associativity of Java Operators
almost all - from left to right
= += ... from right to left
Example:
z = p * r % q + w / x - y
==> z =((((p * r) % q) + (w / x)) - y)
6
1 2 4
3 5
z = a + b + k * (c = d = 0)
==> z = ((a + b) +( k* ((c = (d = 0)) )))
6 1 5
4 3 2
Data Types:
There are two data types available in Java −
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
Byte data type is an 8-bit signed two's complement integer
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
Short data type is a 16-bit signed two's complement integer
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
Default value is 0.
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
Integer is generally used as the default data type for integral values unless there is a concern about memory.
The default value is 0
Example: int a = 100000, int b = -200000
Float data type is a single-precision 32-bit IEEE 754 floating point
Float is mainly used to save memory in large arrays of floating point numbers
Default value is 0.0f
Float data type is never used for precise values such as currency
Example: float f1 = 234.5f
double data type is a double-precision 64-bit IEEE 754 floating point
This data type is generally used as the default data type for decimal values, generally the default choice
Double data type should never be used for precise values such as currency
Default value is 0.0d
Example: double d1 = 123.4
The type of the result of the operation is of the type of operands with one exception - if the type of the result must be byte or short it is converted to int.
If the operands are of a different type, a preliminary conversion of the type of operands is automatically performed in such a way that there is no loss of information. The arrows in the figure below shows the possible conversions. The arrows with a dashed lines show possible loss of precision.
public class Oper {
public static void main(String arg[]){
int i1=4, i2 = 5;
byte b=127,b1=1;
double f1 =5.0;
System.out.println("b:"+b+"\tb+b1:"+(b+b1)+"\t++b:"+ ++b); //
byte converted to int
System.out.println("i1/i2:"+(i1/i2)+ "\ti1/f1:"+(i1/f1));
/* i1/i2 - without conversion
- result 0
i1/f1 - i1 is converted in double -
result 0.8 */
}
}
Explicit conversion (caste).
public class Oper {
public static void
main(String arg[]){
int in;
long ln=123L;
double db = 34.5;
in = ln; //type mismatch
in = db; //type mismatch
in = (int)ln; //ok
in = (int)db; //ok
}
}
Almost all operations can be used only with primitive types. Only the operations '=', '= =', and '! =' Can be used with all objects and the "String" class supports the operations '+' and '+ ='.
The '+' operator is used with String objects for concatenation. If an expression begins with a String object, all subsequent operands are converted to "String" (in recent versions, this conversion is performed even if the String object is not the first operand):
int x = 0, y = 1, z =
2;
String sString = "x,
y, z ";
System.out.println(sString
+ x + y + z); // x,y,z 012
System.out.println(sString
+ (x + y + z)); // x,y,z 3
public class Operators { public static void main(String[]arg){ int in; // casting long ln=123L; double db = 34.5; //in = ln; //type mismatch //in = db; //type mismatch in = (int)ln; //ok in = (int)db; //ok /*****************************************************/ String s, s1="Pijo ", s2="loves ",s3= "apples "; //concatenation s= s1+s2; System.out.println(s); s+=s3; // s=s+s3 System.out.println(s); /*****************************************************/ int i1=4, i2 = 5; // division double f1 =5.0; System.out.println("1: "+i1/i2+"\t" +i1/f1); /*****************************************************/ int k, m=3,z=4,p; // assignment p = k =(m = 2) *(z = 4); System.out.println("2: "+k+"\t" + m+"\t" + z+"\t" + p+"\t"); //(p = k) = 2; //error: (p = k) is not variable /*****************************************************/ k=m=3; //increment System.out.println("3: "+ ++k +"\t"+ m++); System.out.println("4: "+ k +"\t" + m); //k= (m++)++; // error: m++ is not variable /*****************************************************/ System.out.println("11%4:\t"+11%4); // modulo System.out.println("23%6:\t"+23%6); System.out.println("-11%3:\t "+ -11%3); /*****************************************************/ System.out.println("25>11:"+(25>11)+ "\t17<=8: "+(17<=8)); //relations System.out.println("25==11:"+(25==11)+ "\t 25!=11: "+(25!=11)); /*****************************************************/ boolean b1=true,b2=true,b3=false,b4=false; // booleans System.out.println("true && true: "+(b1&&b2)+ "\t true && false: "+(b1&&b3)); System.out.println("true || false: "+(b1||b3)+ "\t false || false: "+(b3 || b4)); System.out.println("!true: "+!b1+"\t !false: "+!b3); /*****************************************************/ int a=4, b=9; // ternary System.out.println("ternary:"+ (a>b?a:b)+ "\t"+ (a<b?a:b)); } } |