System.out.println((10>5) + x +"hello"); //this gives error
Originally posted by Rajat Sarkar:
quote:
"System.out.println("check" + (10>5) +x); //no error"
but in the first printing statement there is also it seems true + int operation working.it gives no error!
Output !
checktrue8
true8hello
Discussion - the powerfull way to excellence!
System.out.println("check" + (10>5) +x);
Originally posted by Rajat Sarkar:
quote:
"System.out.println("check" + (10>5) +x); //no error"
but in the first printing statement there is also it seems true + int operation working.it gives no error!
In your Case
println( (10>5) + x + "hello" );
(10>5) is a 'true' + x is an 'int' -> it becomes boolean + int
There is no println() function that will take such a data type that is a combination of ( boolean and int )Further, before this condition is checked, see that you cannot add a boolean and an int '+' is not overloaded to such data types.
To make it clear :
They are - Incompatible Data Types for such a '+' Opertation.
But When you say []
In both cases println( (10>5) + "" + x + "hello");
(10>5) + "" -> becomes a string so it calls println(String);
( Point : Adding < datatype+"" > Quotation to any data type will make it a string )
x + "hello" -> becomes a string so it calls println(String);
All in a whole this will evaluate a call to the String variant of the println(String) method, so this works fine !
In your case - there is no such variant of println method with (boolean and int )
Discussion - the powerfull way to excellence!
Originally posted by Paul Beckett:
FYI this doesn't really have anything to do with System.out.println(...). The println method takes pretty much any valid type, be it primitive or object.
The problem is all about what happens when the '+' operator is used and if you try to do an '+' on a boolean and an int then the compiler doesn't know what to do. i.e. how do you sensibly add a boolean and an int (remember boolean is true or false, not 0 or 1)?
You will notice the same behaviour as println if you do:
String s = 1 + "one";
or
String s = (10>5) + x +"hello";
The compiler gives an error (something like) unknown operator '+' for boolean and int.
Rajat Sarkar, for future reference it is often useful to include in your post the actual errors that have occurred (be they exceptions at runtime or compilation errors).
That was already explained, still thanks for clarifying us on this !Further, before this condition is checked, see that you cannot add a boolean and an int '+' is not overloaded to such data types.
To make it clear :
They are - Incompatible Data Types for such a '+' Opertation.
Discussion - the powerfull way to excellence!
That was already explained, watch the previous post
Originally posted by Paul Beckett:
hey Ram, you may want to take your own advice? (read my original post)
in all seriousness I just wanted to make 100% clear that this had nothing to do with System.out.println.
I don't think the reference to printstream api is relevant for this problem. However, the rest of your response appears to be correct.
Discussion - the powerfull way to excellence!