Java Operators

In an attempt to advance our learning, we will talk about Java operators today.

Java operators

Operators In Java

Java operators are used for performing operations on variables and values.

Two values are added together using the + operator in the example below:

Example: 

public class Main { public static void main(String[] args) { int addition = 100 + 250; System.out.println("After Edition the value of x becomes: "+addition); } }

Example: 

public class Main { public static void main(String[] args) { float ample = 22.41f + 61.2f; System.out.println("After addition of float values Result= "+ample); } }

The + operator is often used to add two values together, like in the example above, but it can also add values, or variables, together:

Example: 

public class Main { public static void main(String[] args) { int result1 = 100 + 150; int result2 = result1 + 250; int result3 = result1 + result2; System.out.println("The Result 1 was: "+result1); System.out.println("The Result 2 was: "+result2); System.out.println("The Final Result was: "+result3); } }

Example: 

public class Main { public static void main(String[] args) { String mrx_1="Java "; String mrx_2="Programming "; String mrx_3="Language "; System.out.println("After Concatenation is: "+mrx_1+mrx_2+mrx_3); } }

Operators in Java are divided into the following groups:

  1. Arithmetic operators.
  2. Assignment operators.
  3. Bitwise operators. (In Java, bitwise operators are rarely used.)
  4. Comparison operators.
  5. Logical operators.


Java Arithmetic Operators

When it comes to Java operators, arithmetic operators are used to perform common mathematical operations.

Operator Name Overview Example Try it
+ Addition Sums the two values. x + y Execute
Subtraction Calculate the difference between two values. x – y Execute
* Multiplication Two values are multiplied. x * y Execute
/ Division Calculates the ratio of two values. x / y Execute
% Modulus Returns the remainder after division x % y Execute
++ Increment 1 is added to a variable’s value ++x Execute
Decrement A variable’s value is decreased by one –x Execute


Java Assignment Operators

In order to assign values to variables, assignment operators are used.

The following example uses the assignment operator (=) to assign 30 to a variable mrx:

Example: 

public class Main { public static void main(String[] args) { int mrx = 30; System.out.println("The value:"+mrx+" is assigned to the variable"); } }

Example: 

public class Main { public static void main(String[] args) { String ample = "James Gosling"; System.out.println("Founder of Java Programming Language is: "+ample); } }

 

Addition assignment (+=) adds a value to a variable:

Example: 

public class Main { public static void main(String[] args) { int number = 30; number += 15; // 5 is added to the number System.out.println(number); } }

Example: 

public class Main { public static void main(String[] args) { int ample = 16; ample += 61; // 15 is added to the mrx System.out.println(ample); } }

Here is a list of all assignment operators:

Operator Example Same As Try it
= mrx = 5 mrx = 5 Execute
+= mrx += 3 mrx = mrx + 3 Execute
-= mrx -= 3 mrx = mrx – 3 Execute
*= mrx *= 3 mrx = mrx * 3 Execute
/= mrx /= 3 mrx = mrx / 3 Execute
%= mrx %= 3 mrx = mrx % 3 Execute
&= mrx &= 3 mrx = mrx & 3 Execute
|= mrx |= 3 mrx = mrx | 3 Execute
^= mrx ^= 3 mrx = mrx ^ 3 Execute
>>= mrx >>= 3 mrx = mrx >> 3 Execute
<<= mrx <<= 3 mrx = mrx << 3 Execute

Java Bitwise Operators

Using Java’s bitwise operators, you can perform operations on individual bits.

In Java, there are several bitwise operators:

Operator Overview
~ Bitwise Components
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR

 


Java Comparison Operators

In Java operators, two values can be compared by using comparison operators:

Operator Name Example Try it
== Equal to mrx == mrexamples Execute
!= Not equal mrx != mrexamples Execute
> Greater than mrx > mrexamples Execute
< Less than mrx < mrexamples Execute
>= Greater than or equal to mrx >= mrexamples Execute
<= Less than or equal to mrx <= mrexamples Execute

Java Logical Operators

A logical operator determines the logic between variables or values in Java Operators:

Operator Name Overview Example Try it
&& Logical and If both statements are true, then true is returned. mrx < 5 &&  mrx < 10 Execute
|| Logical or If one of the statements is true, then true is returned. mrx < 5 || mrx < 4 Execute
! Logical not Returns false if the result is true and vice versa !(mrx < 5 && mrx < 10) Execute


 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *