Try to predict the output of following code:
public class Geeksforgeeks { public static void main(String[] args) { System.out.println(2+0+1+6+"GeeksforGeeks"); System.out.println("GeeksforGeeks"+2+0+1+6); System.out.println(2+0+1+5+"GeeksforGeeks"+2+0+1+6); System.out.println(2+0+1+5+"GeeksforGeeks"+(2+0+1+6)); } } |
Explanation:
The output is
9GeeksforGeeks
GeeksforGeeks2016
8GeeksforGeeks2016
8GeeksforGeeks9
This unpredictable output is due the fact that the compiler evaluates the given expression from left to right given that the operators have same precedence. Once it encounters the String, it considers the rest of the expression as of a String (again based on the precedence order of the expression).
- System.out.println(2 + 0 + 1 + 6 + “GeeksforGeeks”); // It prints the addition of 2,0,1 and 6 which is equal to 9
- System.out.println(“GeeksforGeeks” + 2 + 0 + 1 + 6); //It prints the concatenation of 2,0,1 and 6 which is 2016 since it encounters the string initially. Basically, Strings take precedence because they have a higher casting priority than integers do.
- System.out.println(2 + 0 + 1 + 5 + “GeeksforGeeks” + 2 + 0 + 1 + 6); //It prints the addition of 2,0,1 and 5 while the concatenation of 2,0,1 and 6 based on the above given examples.
- System.out.println(2 + 0 + 1 + 5 + “GeeksforGeeks” + (2 + 0 + 1 + 6)); //It prints the addition of both 2,0,1 and 5 and 2,0,1 and 6 based due the precedence of ( ) over +. Hence expression in ( ) is calculated first and then the further evaluation takes place.
This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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:
- Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
- Java.util.TreeMap.firstEntry() and firstKey() in Java
- Java.util.TreeMap.containskey() and containsValue() in Java
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- Java.util.TreeMap.put() and putAll() in Java
- Java.util.TreeMap.floorEntry() and floorKey() in Java
- Java Swing | Translucent and shaped Window in Java
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java IO and Java NIO
- Difference between Java and Core Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Difference between Java and C++
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java

