DEV Community

Cover image for Java Programming Language (Operators)
Nihal Islam
Nihal Islam

Posted on • Updated on

Java Programming Language (Operators)

Operators

Java programming language has multiple types of operators to operate two values or variables.

1. Arithmetic Operators
in java there are 5 operators +, -, *, / and % that can be performed between two numbers.

First four operators work the same as it does on mathematics.

class Operators {
    public static void main(String[] args) {
        int a = 15;
        int b = 3;
        //Addition Operator (+)
        System.out.println(a+b); //prints 18

        //Subtraction Operator (-)
        System.out.println(a-b); //prints 12

        //Multiplication Operator (*)
        System.out.println(a*b);// prints 45

        //Division Operator (/)
        System.out.println(a/b); //prints 5
    }
}
Enter fullscreen mode Exit fullscreen mode

In java, division operation between two int numbers gives the floor int value. We can get double values in the following ways.

//Floor division
int a = 15;
int b = 2;
System.out.println(a/b); //prints 7

//If any one number is double, the answer will give us double value
int c = 15;
double d = 2;
System.out.println(c/d); //prints 7.5

//Double wrapper (double) will convert the integer value into a double value
double e = (double) a/b;
System.out.println(e); //prints 7.5
Enter fullscreen mode Exit fullscreen mode

The % operator called modulo operator can be performed between two values that gives us remainder after dividing them.

int a = 15;
int b = 2;
System.out.println(a%b); //prints 1
Enter fullscreen mode Exit fullscreen mode

2. Relational Operators

Relational operators compare two numbers and generate boolean value checking if the comparison is true or false.

class Operators {
    public static void main(String[] args) {
        int a = 39, b = 24;
        //Is Equal To
        System.out.println(a==b); //prints false

        //Not Equal To
        System.out.println(a!=b); //prints true

        //Is Greater Than
        System.out.println(a>b); //prints ture

        //Is Greater Than Or Equals to
        System.out.println(a>=b); //prints true

        //Is Less Than
        System.out.println(a<b); //prints false

        //Is Less Than Or Equals to
        System.out.println(a<=b); //prints false
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Logical Operators
These operators are performed on boolean values and generates another boolean value. The logical operators are &&, || and !.

  • AND operator && only generates true if both of the boolean values are true. Otherwise gives false.
System.out.println(true && true); //prints true

System.out.println(false && true); //prints false

System.out.println(true && false); //prints false

System.out.println(false && false); //prints false
Enter fullscreen mode Exit fullscreen mode
  • OR operator || only generates false if both of the boolean values are false. Otherwise gives true.
System.out.println(true || true); //prints true

System.out.println(false || true); //prints true

System.out.println(true || false); //prints true

System.out.println(false || false); //prints false
Enter fullscreen mode Exit fullscreen mode
  • NOT operator ! is performed on a single boolean value and reverse it.
System.out.println(!false); //prints true

System.out.println(!true); //prints false
Enter fullscreen mode Exit fullscreen mode

Logical operations can also be performed with relational operators. For example,

int a = 28, b = 7;
System.out.println((a>=b) && (a==b)); //prints false

System.out.println((a>=b) || (a==b)); //prints true

System.out.println((a>=b) && !(a==b)); //prints true
Enter fullscreen mode Exit fullscreen mode

Appendix

  • In java we can invert a sign of a number by simply using - in front of a variable.
int a = 5;
System.out.println(-a); //prints -5
Enter fullscreen mode Exit fullscreen mode
  • In Java if we want to increment a single variable by some value we can use the following way
int a = 10;
a = a + 10;
System.out.println(a); //prints 20
Enter fullscreen mode Exit fullscreen mode

Here first we are performing addition operation and then assigning the value in the same variable. We can even make this process short

int a = 10;
a += 10;
System.out.println(a); //prints 20
Enter fullscreen mode Exit fullscreen mode

We can perform other arithmetic operations in the same way.a += 10, a -= 10, a *= 10, a /= 10, a %= 10.

  • There are two operators called increment ++ and decrement -- operators which increments and decrements a value by 1.
int a = 10;
//Instead of writing this
a += 1;
System.out.println(a); //prints 11
//We can write this
a++;
System.out.println(a); //prints 12
//Both does the same thing.
Enter fullscreen mode Exit fullscreen mode

We can use increment and decrement operation in two ways. Prefix increment/decrement ++a; / --a; or Postfix increment/decrement a++; / a--;. Prefix operators first change the value then preview it and Postfix operators first preview the value then change it. For example,

int a = 5;
System.out.println(a++); //prints 5
System.out.println(a); //prints 6
System.out.println(++a); //prints 7
Enter fullscreen mode Exit fullscreen mode

Happy Coding

Top comments (0)