Operators in java

Operator performs operations. In java, there are 7 types of operators:

Arithmetic Operators

These operators are used in mathematics calculations. Arithmetic operands are:

OperatorDescription
+addition
-Subtraction
*Multiplication
/Division
%Modulus (Reminder)
++Increase by 1
--Decrease by 1

Example:

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. //Arithmetic operators
  7. int a =5;
  8. int b = 6;
  9. // c= 11
  10. int c = a + b;
  11. // print 11
  12. System.out.println(c);
  13. // print 11 - 5
  14. System.out.println(c-a);
  15. // print 11/5
  16. System.out.println(c/a);
  17. // print 11 % 5
  18. System.out.println(c%a);
  19. // print 5
  20. System.out.println(a);
  21. // increase a by 1
  22. a++;
  23. // print a again, a = 6
  24. System.out.println(a);
  25. // decrease a
  26. a--;
  27. // print a again, a = 5
  28. System.out.println(a);
  29. }
  30. }

Output:

  1. 11
  2. 6
  3. 2
  4. 1
  5. 5
  6. 6
  7. 5

Relational Operators

Relational operators are used to compare one operand to another. These compares two operands and return true if comparison is right otherwise false.

OperatorsDescriptors
==both are equal
!=both are not equal
<left is smaller
>right is smaller
<=left is smaller or equal
>=right is smaller or equal

Example:

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. System.out.println(8==9);
  7. System.out.println(8!=9);
  8. System.out.println(8<9);
  9. System.out.println(8>9);
  10. System.out.println(8<=9);
  11. System.out.println(8>=9);
  12. }
  13. }

Output:

  1. false
  2. true
  3. true
  4. false
  5. true
  6. false

Bitwise Operators:

Bitwise operators performs operations on individual bits in an integer.

int a = 20 ( in binary, a = 10100) and b = 50 ( in binary, 110010). These operators will change these numbers in binary and will operate functions.

OperatorDescription
&And Operator
|Or Operator
^XOR operator
<<Left operator
>>Right operator
>>>Shift right zero fill operator

Example:

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. private void method1(String name)
  5. {
  6. System.out.printf("name is %s \n",name);
  7. }
  8. private void method2(String name, int age)
  9. {
  10. System.out.printf("name is %s and age is %d \n",name,age);
  11. }
  12. public static void main(String args[])
  13. {
  14. Tutorials object = new Tutorials();
  15. object.method1("harish");
  16. object.method2("harish", 20);
  17. }
  18. }

Output:

  1. 16
  2. 54
  3. 38
  4. 80
  5. 5
  6. 5

Logical Operators

These are boolean operators return true or false. These are same as logical gates.

OperatorDescription
&&Logical AND operator
||Logical OR operator
!Logical NOT operator

Example: 3 && 4 is true and 3 && 0 is false. !0 is true and !3 is false. Try to print all operators.

Assignment Operators

OperatorsExample
=a = 10
+=a+=4 is equivalent to a = a + 4
-=a-=4 is equivalent to a = a - 4
/=a/=4 is equivalent to a = a / 4
%=a%=4 is equivalent to a = a %+ 4
<<=a<<=4 is equivalent to a = a << 4
>>=a>>=4 is equivalent to a = a >> 4
&=a&=4 is equivalent to a = a & 4
^=a^+=4 is equivalent to a = a ^ 4
|=a|=4 is equivalent to a = a | 4

Misc Operators:

This operator check a condition if condition is true then it will do first work otherwise it will do alternative work.

Syntax is : variable x =(condition)? if_true_work: if_false_work

Example:

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. int b = 3;
  7. // condition is b == 3 or not
  8. // if b == 3 then a = 4
  9. // otherwise a = 9
  10. int a =(b == 3)? 4 : 9;
  11. System.out.print(a);
  12. }
  13. }


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a technical core team member at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel