So I started learning java several days ago and got a question. For the next expression:
String foo=123;
is not allowed. However, in System.out.printIn()
, we can use something like:
int x=5;
System.out.println(x);
Since implicitly assigning a integer to a string is not allowed, why the expression above works? Anyone can give a detailed explanation? I'm also wondering when can we use this kind of implicit thing and when we cannot.
The reason you can call println with an integer is because the method is overloaded. Basically there are more than one method called println and one of them accept an integer.
Take a look here: PrintStream
There are so many overloaded methods of the PrintStream System.out
:
println(boolean x)
println(char x)
println(int x)
println(long x)
println(float x)
println(double x)
println(char x[])
println(String x)
println(Object x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With