Is there any difference between
System.out.println(true);
and
System.out.println("true");
Although the output I see is the same, is there any difference with respect to usage or coding style?
Is there any difference between
System.out.println(true);
and
System.out.println("true");
Although the output I see is the same, is there any difference with respect to usage or coding style?
In the first option you pass a boolean
to the PrintWriter
's println
method while in the second you are passing a String
, so different println
methods are called. In the end, the boolean
in the first case is converted to a String
, so the end result is the same.
If you only print a single boolean literal, println(true)
is shorter. If you combine that literal with other String
s, println("value = true")
would make more sense than println("value="+true)
.
true gives boolean value and "true" means charsequence or string value
The PrintWriter's println() method is overriden for many objects and primitives.
If you look internally it uses
write(String.valueOf(obj));
so the obj.toString()
is what does all the magic :)