Operators in Java

Following will be covered in this tutorial.

  • Assignment Operators
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators

Assignment Operators

Assignment operators are used to assign values to variables.

     =            Assignment operator

For example :

int number = 10;

boolean isInteger = true;

TextField textInput = new TextField();

Important :

  • The “true” keyword should be in lower case unless it will cause a compile error.
  • Do not worry about the last assignment is used to create a object in java.WE will discuss about those Object Oriented Programming (OOP) in future tutorials.

Compound Assignment Operators

    +=          Assignment by addition operator

-=          Assignment by subtraction operator

Consider the following two operations.

total = total + 10 ;
newScore = newScore – 2 ;

These two operations can be done by using the compound operators.

total += 10 ;
newScore -= 2 ;

Likewise you can use the below operators.

  • *=
  • /=
  • %=

Arithmetic Operators

As we all know, the basic arithmetic operators are :

  • +    addition
  • –    subtraction
  • *    multiplication
  • /     division
  • %    modulus

Prefix, Postfix Increments and Decrements

++x    prefix increment

–x    prefix decrement

x++    postfix increment

x–    postfix decrement

Examine the following bit of code.

capture

Relational Operators

These operators evaluate the relationship of the values on the either side of the operator and returns a boolean value.

 <    less than operator

<=   less than or equal operator

>     greater than operator

>=   greater than or equal operator

Equality Operators

These operators directly compares equality of primitives (numbers, characters, booleans).

 ==    equal to operator

!=      not equal to operator

Check out these code lines with the output.

capture1

Logical Operators

The 2 main logical operators are “AND” and “OR”. But java use some other symbols to denote them.

&&     and

||        or

Actually these are the short circuit version if the two operators “&” and “|”.

Check these code segments.

3

The bitwise operators are another type of operators that I will be covering another tutorial but not in this one because I have to explain a lot on that title.

So this is almost all about the operators. In the next tutorial we will be discussing about string objects and there methods which are really important in java programming.

Thank you. See ya !!!

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑