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.

OperatorNameOverviewExampleTry it
+AdditionSums the two values.x + yExecute
SubtractionCalculate the difference between two values.x – yExecute
*MultiplicationTwo values are multiplied.x * yExecute
/DivisionCalculates the ratio of two values.x / yExecute
%ModulusReturns the remainder after divisionx % yExecute
++Increment1 is added to a variable’s value++xExecute
DecrementA variable’s value is decreased by one–xExecute


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:

OperatorExampleSame AsTry it
=mrx = 5mrx = 5Execute
+=mrx += 3mrx = mrx + 3Execute
-=mrx -= 3mrx = mrx – 3Execute
*=mrx *= 3mrx = mrx * 3Execute
/=mrx /= 3mrx = mrx / 3Execute
%=mrx %= 3mrx = mrx % 3Execute
&=mrx &= 3mrx = mrx & 3Execute
|=mrx |= 3mrx = mrx | 3Execute
^=mrx ^= 3mrx = mrx ^ 3Execute
>>=mrx >>= 3mrx = mrx >> 3Execute
<<=mrx <<= 3mrx = mrx << 3Execute

Java Bitwise Operators

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

In Java, there are several bitwise operators:

OperatorOverview
~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:

OperatorNameExampleTry it
==Equal tomrx == mrexamplesExecute
!=Not equalmrx != mrexamplesExecute
>Greater thanmrx > mrexamplesExecute
<Less thanmrx < mrexamplesExecute
>=Greater than or equal tomrx >= mrexamplesExecute
<=Less than or equal tomrx <= mrexamplesExecute

Java Logical Operators

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

OperatorNameOverviewExampleTry it
&&Logical andIf both statements are true, then true is returned.mrx < 5 &&  mrx < 10Execute
||Logical orIf one of the statements is true, then true is returned.mrx < 5 || mrx < 4Execute
!Logical notReturns 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 *