Sitemap
Evin AYDİN ÜLGEN

Evin AYDİN ÜLGEN

Bursa Teknik University / Computer Engineering

JAVA PROGRAMMING-OPERATORS

Hello to all of you again 🙃, let’s continue java tutorial from where we left off! In this section, we will learn about operators with which we can perform basic operations in Java. We can use arithmetic operations or comparison operations within the program. Let’s have a look at these now!

6 min readFeb 5, 2023

--

ASSIGNMENT OPERATORS → The assignment operator (=) is the simplest operator. In Java, it is used to assign the right variable to the left variable. Assignment is always done from right to left. This is sometimes used for object assignments and sometimes for value assignment.

int i = 98; // value assignment
Book b = new Book(); // object assignment

There are other assignment operators besides the = operator. These are; +=, -=, *=, /=, %= operators.

public class Operators {

public static void main(String[] args) {
double d = 9.7; //basic assignment operation =
int a=90, b=54, c=25, e=11, f=262;
//compound assignments
a += 10; //a =a + 10
b -= 5; //b =b - 5
c *= 4; // c =c * 4
e /= 2; //e =e / 2
f %= 3; //f =f % 3

System.out.println(a); //a is 100
System.out.println(b); //b is 49
System.out.println(c); //c is 100
System.out.println(d); //d is 9.7
System.out.println(e); //e is 5
System.out.println(f); //f is 1
}

}

Assignment operations can also be done in the form of multiple assignments. That is, while assigning one value to another, we can assign it to another value at the same time.

int i = 34;
int j;
int k = j = i; //multiple assignment

ARITHMETIC OPERATIONS → It is used to perform mathematical operations. These are;

+ ; does the addition
- ; does the subtraction
* ; does the multiplication
/ ; does the division
% ; takes a mod
++ ; value is incremented by 1
- - ; value is decremented by 1

Increment(++) and Decrement(- -) Operators ; These operators can be at the beginning or the end of the variables. If these operators will come to the head; first, increment (pre-increment → ++x) or decrement (pre-decrement →- -x) operations are performed. Then the value of the operand is determined.
If it comes to an end; first the value of the operand is determined, then the increment (post-increment → x++) or decrement (post-decrement → x- -) operations are performed.

Get Evin AYDİN ÜLGEN’s stories in your inbox

Join Medium for free to get updates from this writer.

Now let’s take a complex example and show its output !👉 👈

public class ArithmeticOperators {

public static void main(String[] args) {
int i = 18;
int j = 14;
System.out.println("The result of the addition operation: "+(i + j));
System.out.println("The result of the subtraction operation: "+(i - j));
System.out.println("The result of the multiplication operation: "+(i * j));
System.out.println("The result of the division operation: "+(i / j));

//Increment(++) and Decrement(- -) Operators
int number = 15;
int a = ++number; //"a" and number is 16
int b = number++; //"b" is 16 and number is 17
int c = --number; //"c" and number is 16
int d = number--; //"d" is 16 and number is 15
int e = number; //"e" is 15
System.out.println("a value is: "+a);
System.out.println("b value is: "+b);
System.out.println("c value is: "+c);
System.out.println("d value is: "+d);
System.out.println("e value is: "+e);
}

}
Press enter or click to view image in full size
program output

RELATIONAL OPERATORS → These operators are used to make various comparisons between data. It decides the relationship of one operand to another. It is used in equality or ordering situations. These operators return a boolean type value. Relational operators are often used in control loops like if,while. These operators ;

< ; smaller
> ; bigger
<= ; equal or less than
>= ; equal or greater
== ; equal
!= ; not equal

public class RelationalOperators {

public static void main(String[] args) {
int number1 = 90;
int number2 = 76;

System.out.println("(number1 > number2) operation result is "+ (number1>number2));
System.out.println("(number1 < number2) operation result is "+ (number1<number2));
System.out.println("(number1 >= number2) operation result is "+ (number1>=number2));
System.out.println("(number1 <= number2) operation result is "+ (number1<=number2));
System.out.println("(number1 == number2) operation result is "+ (number1==number2));
System.out.println("(number1 != number2) operation result is "+ (number1!=number2));

}

}
program output

LOGICAL OPERATORS → Logical operators operate on boolean values. These are;

& ; Logical AND operation
| ; Logical OR operation
&& ; Conditional AND operation. Returns true as long as both values being compared are true
|| ; Conditional OR operation. Returns true as long as at least one of the two values being compared is true
! ; Logical NOT operation. Returns true if the given boolean value is false, false if true
^ ; Logical XOR operation


public class RelationalOperators {

public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;

System.out.println("b1 & b2 = " +(b1&b2));//false & false = false
//false & true = false
//true & false = false
//true & true = true
System.out.println("b1 | b2 = " +(b1|b2));//false | false = false
//false | true = true
//true | false = true
//true | true = true
System.out.println("b1 && b2 = " +(b1&&b2));//false && false = false
//false && true = false
//true && false = false
//true && true = true
System.out.println("b1 || b2 = " +(b1||b2));//false || false = false
//false || true = true
//true || false = true
//true || true = true
System.out.println("b1 ^ b2 = " +(b1^b2));//false ^ false = false
//false ^ true = true
//true ^ false = true
//true ^ true = false
System.out.println("!b2 = " +(!b2)); //!false = true
//!true = false
}

}
program output

BITWISE OPERATORS → These operators use bits to invert these bits or perform operations such as shifts.

~ ; Reverses bits
& ; Bitwise AND performs
| ; Bitwise OR operation
^; Bitwise XOR operation
>> ; Bitwise shifts the number to the right by the desired value.
Here, 0 (if the number is positive) or 1 (if the number is negative) is added to the far left according to the sign of the number.
<< ; Bitwise shifts the number to the left by the desired value.

bits 1 and 0
public class RelationalOperators {

public static void main(String[] args) {
byte a = 42; //00000000 00000000 00000000 00101010
byte b = 15; //00000000 00000000 00000000 00001111
System.out.println(a | b); //00000000 00000000 00000000 00101111
//0 | 0 = 0
//0 | 1 = 1
//1 | 0 = 1
//1 | 1 = 1
System.out.println(a & b); //00000000 00000000 00000000 00001010
//0 & 0 = 0
//0 & 1 = 0
//1 & 0 = 0
//1 & 1 = 1
System.out.println(a ^ b); //00000000 00000000 00000000 00100101
//0 ^ 0 = 0
//0 ^ 1 = 1
//1 ^ 0 = 1
//1 ^ 1 = 0
System.out.println( ~ a); //11111111 11111111 11111111 11010101
//~1 = 0
//~0 = 1
System.out.println(b << 2); //00000000 00000000 00000000 00111100
}

}
program output

OPERATOR’S PRIORITY → All operators have an operation priority and this operation priority should be considered in the software language. Now let’s look at the priority order of operators from high to low!

  • Parentheses, accessing array elements
  • The ++ and — — operators at the end → x++ , x- -
  • The ++ and — — operators at the start → ++x ,- -x
  • Creating an object with new
  • Multiplication, division, mode operation (*, /, %)
  • Addition and subtraction (+, -)
  • Shifting operators (<<,>>)
  • Relational Operators (<, >, < =, > =)
  • Logical Operators (&, |, ^)

In this tutorial, I explained operators and how they are used. I hope it was a useful blog for you.🙏

--

--

Evin AYDİN ÜLGEN
Evin AYDİN ÜLGEN

Written by Evin AYDİN ÜLGEN

Bursa Teknik University / Computer Engineering