i am learning java and practicing it daily,i wrote the following code and wondered about the output
class test
{
public static void main(String args[])
{
System.out.println(1+2+ " = " +10+2);
}
}
here the output was 3=102,and wondered about the following “Java starts treating everything as a String once it has encountered a string in System out statement”
can anyone explain this ?i m confused why does it accept it as string?
Java parses program text without regard to the types of expression. As motivation, consider if they were fields written after the method in the class. So, as string concatenation and addition share the same operator, we have
is equivalent to
Folding constants, we have