Too broad...
But, a method accepting a String needs a String as a parameter.
The println
is overloaded in PrintStream class so it accepts all of the primitive types, String and the Object type.
bad operand types for binary operator '+'
The point is in "binary" operator. The plus "+" is normally considered as arithmentic (a binary) operator between two numbers. And secondly, the "+" COULD BE CONSIDERED as String concatenation operator.
[Side note, Java doesn't allow overloading of operators]
Binary, therefore arithmetic, logical and bitwise operators expects the defined types for them selfs.
In the case of "+" operator, it is most ofen considered as addition operator.
Expressions with numeric types are being automatically promoted to larger primitive types if needed.
So again, the call to a + b
is only possible with numeric or String operands. If one of a
or b
is not numeric or String operand, the error will be thrown.
It's the implementations of println/print methods of PrintStream class that tries to get a default result of toString
method for given parameter.
The System.out.print(...)
is kind of combination of objects and calls. The System
is static object representing a System in runtime environment, the out
represents standard output stream for a current thread and print
is a method called on that out
PrintStream. There are several different streams, just for example an err
which is standard error output stream.
The standard print
or println
method is overloaded to accept a Object type where those methods tries to call the toString
method on given Object as a parameter. But it cannot evaluate the given expression with +
operand, if the whole expression is not evaluated to Number or String.
Cutting to the chase,
the "+" at first works as arithmetic operator, if next operator is a String it converts the rest of expression to String.
When "+" has two object or incompatibile operators, it cannot determine how the operands should be handled. How it could add list to an array or simply true + true
? For the second (true + true
) the logical &&
must be used.
In your case, when you tried to +
between the different types, the println
gave you an error, because the "+" is only able to concatenate Strings or sum both of the opearands which inherits from Number class.
Answer from @Moyebang is kind of good, because the expression given as parameter in printnl
method evaluates to String type. The answer from @Lotsa isn't very good because the operands are evaluated from left to right, so if the operands before +
are incompatible, the program would throw an error (the second part of an answer is ok).
Such expressions at first are evaluated in their compile time.
They are evaluaded on standard basics from left to right:
System.out.println(1 + 1 + ""); //this gives 2
System.out.println("" + 1 + 1); //this gives 11
System.out.println(1 + "" + new ArrayList<>()); //this gives 1[]
System.out.println(new ArrayList<>() + "" + 1); //this gives []1
System.out.println(new ArrayList<>() + 1 + ""); //and this gives an error
So, from left the mathematical equasions are being evaluated, when it's a String the others are being addeded to that String. The last shows an error because at first the ArrayList object is being added to "1", where there's no common approach on how to add the List to an object.