Chapter 2 – Operators and Statements

Operators and Statements

# all of the arithmetic operator may be applied to any java primitive, except Boolean and String.

# only additional operator + and += may be applied to String value, which result in String concatenation.

Numeric Promotion

# conversion of a smaller numeric type to a larger numeric type.

byte,char,short => int

int => long

long,float => double


  1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types
  2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s data type
  3. Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int
  4. After all promotion has occurred and the operands have the same data

Unary Operators

# requires exactly one operand or variable

Increment and Decrement Operator

# ++, — => can be applied to numeric operands

# takes higher precedence

Assignment Operator

# throws compiler error if it detects you are trying to convert from large to smaller datatype

Ex: int n=1.0; //type mismatch: cannot convert from double to int

Compound Assignment Operator

Ex: x*=z; => x=x*z;

Ternary Operator

Ex: (n>2?n>4?10:8:7)

For loop

Ex: for(i=0;i<10;){i=i++;System.out.print(“Hello World”);}

Above line will result in infinite loop

Casing Primitive Values

long x = 10;

int y = 5;

y = y * x; // DOES NOT COMPILE


long x=10;

int y=5;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s