Operators in Java

← Back to Java Language Chapter

Operators are special symbols that perform operations.



  • Arithmetic operation from arithmetic expression (a+b, a/b)

  • The result of arithmetic expression is integer literal or floating point literal.

Example:


Modifying

    byte b2=b1+2 to byte b2=(byte)(b1+2); // type casting
    float f2=f1+10.0 to float f2=(float)(f1+10.0);

Remember the chart drawn below :

Type
byte
Operator + Type
byte
Result Type
byte
byte + short Byte
byte + int Int
byte + long Long
byte + float Float
byte + double Double
short + short Short
short + int Int
short + long Long
short + float Float
short + double Double
int + int Int
int + long long

Example:

It is a form of relational expression whose result is the ‘boolean’ value a < b.

A Logical operator forms logical expression, which is a combination of one or more relational expressions

  • (a>b) && (a>c)

  • (a>b) || (a>c)

The result of logical expression is based on Boolean values, which in turn is based on the following table:

A B A&&B [and] A||B [OR]
T T T T
T F F T
F T F T
F F F F
    Syntax:
      destination_var = source_var;

    Example:
      int x = 10+9.9; // error - Type mismatch: cannot convert from double to int
      System.out.println(10+9.9); //ok
      byte b = 10; // ok
      int x = b; //ok

There are two types of casting:

  • Implicit casting

  • Explicit casting

Implicit casting : When the destination is bigger than the source, then conversion happens automatically, which is called implicit casting. Another term for it is widening.

Explicit casting : When the destination is smaller than the sources, then you have to do explicit casting or narrowing

    Syntax:
      destination_var = (destination type)source_var;

    Example:
      int x = 10+9.9; // error-Type mismatch: cannot convert from double to int
      int x1=(int)10+9.9; // error-Type mismatch: cannot convert from double to int
      int x2=(int) (10+9.9); // ok
      int x3=10+(int)9.9; // ok

It is a type of arithmetic operator. It allows you to add and subtract 1 from variables.

Example:

  • Ternary operator is used to perform simple conditional checking

      Syntax : (Conditional expression)? s1:s2;

  • First, the given expression is evaluated. If the expression is true, then s1 will be executed. If the expression is false, then s2 will be executed.


Question : Write a program to read the two numbers from command line and find the minimum and maximum number.


Question : Write a program to take three numbers from the command line and find the minimum and maximum.

Bitwise operator acts on individuals bits of given numbers.

1. Left shift operator <<
2. Right shift operator >>
3. Bitwise AND &
4. Bitwise OR |
5. Bitwise XOR ^
6. Bitwise NOT ~

Example:

A new operator is used to create an object of a given class.

    Example:
      Hello h = new Hello();

A dot operator is used to refer members of a class using class name or object.

    Example:
      Hello h = new Hello();
      h.a;
      h.show();
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?
assignment =++-=*=/=%=&=^=|=<=>=

Operator Precedence
Post fix expr ++ expr --
Unary ~ ! +!expr -- expr+expr-expr
Multiplicative * / %
Additive + -
Shift < < > >>>>
Relational < > <=> = instance of

Note: no sizeof operator in java

Example:



← Back to Java Language Chapter

Ready for structured training?

Learn Java Full Stack with real projects

Live classes, practical projects, interview preparation and placement support in one guided programme.

View Java Full Stack Course Book Free Demo