The & operator in Java has two definite functions:
- As a Relational Operator: & is used as a relational operator to check a conditional statement just like && operator. Both even give the same result, i.e. true if all conditions are true, false if any one condition is false.
However, there is a slight difference between them, which highlights the functionality of & operator:
- && operator: It only evaluates the next condition, if the condition before it is true. If anyone condition is false, it does not evaluate the statement any further.
- & operator: It evaluates all conditions even if they are false. Thus, any change in the data values due to the conditions will only be reflected in this case.
Example:
// Java program to demonstrate// & operator as relational operatorimportjava.io.*;classGFG {publicstaticvoidmain(String[] args){intx =5, y =7, z =9;System.out.println("Demonstrating && operator");if((x > y) && (x++ > z));elseSystem.out.println("Value of x: "+ x);System.out.println("\nDemonstrating & operator");if((x > y) & (x++ > z));elseSystem.out.println("Value of x: "+ x);}}chevron_rightfilter_noneOutput:Demonstrating && operator Value of x: 5 Demonstrating & operator Value of x: 6
- As a Bitwise AND: & operator is used for adding Bitwise numbers in Java. Bitwise numbers are binary numbers stored in the form of integers. Some people will ask what is the use of these Bitwise numbers anyway? Why not store every number in its decimal form and perform the normal operations using our traditional operators: +, -, /, %, *. Its because all our encoding and decoding of data is done in bits, as they allow packing a huge amount of information into a tiny space.
Example:
// Java program to demonstrate// & operator as bitwise operatorimportjava.io.*;classGFG {publicstaticvoidmain(String[] args){inta =12;intb =25;System.out.println("Demonstrating & operator\n");intc = a & b;System.out.println(a +" & "+ b +" = "+ c);}}chevron_rightfilter_noneOutput:Demonstrating & operator 12 & 25 = 8
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- && operator in Java with Examples
- 'AND' vs '&&' as operator in PHP
- Ternary operator vs Null coalescing operator in PHP
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Equality (==) operator in Java with Examples
- Java Unary Operator with Examples
- Java Ternary Operator with Examples
- Java Assignment Operator with Examples
- Diamond operator for Anonymous Inner Class with Examples in Java
- Retrieving Elements from Collection in Java (For-each, Iterator, ListIterator & EnumerationIterator)
- new operator vs newInstance() method in Java
- instanceof operator vs isInstance() method in Java
- new operator in Java
- Java Ternary Operator Puzzle
- Double colon (::) operator in Java
- Difference between concat() and + operator in Java
- || operator in Java
- Java Program To Find Largest Between Three Numbers Using Ternary Operator
- Routing Information Protocol (RIP) V1 & V2
- Samsung R & D Interview questions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

