Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can System.out.printIn() accept integers?

Tags:

java

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.

like image 207
OneZero Avatar asked May 24 '12 22:05

OneZero


2 Answers

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

like image 78
MAV Avatar answered Oct 06 '22 04:10

MAV


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)
like image 25
Eng.Fouad Avatar answered Oct 06 '22 04:10

Eng.Fouad