Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in "System.out.print()"

 
Rajat Sarkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Xyz
{
public static void main(String[] args)
{
int x=8;
System.out.println("check" + (10>5) +x); //no error
System.out.println((10>5) + x +"hello"); //this gives error
}
}

Why the second one gives error, while the first statement gives no error?
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println((10>5) + x +"hello"); //this gives error


(10>5) evaluates to true
and true + int is not valid operation .. that is why error is coming.
 
Rajat Sarkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!




Try this then !

package codes;



You will get the Output as you want !

Output !

checktrue8
true8hello


 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println("check" + (10>5) +x);

in this statement .. "check" is string. (10>5) return ture which is converted to string and then + operator will concatenate these to string.
then again int is converted to string and so on ...
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the reasoning behind this is due to the way java handles expressions.

Starting from the left, everything after the first string is treated as a string.

(i)System.out.println("check" + (10>5) +x);
(ii)System.out.println((10>5) + x +"hello");
(iii)System.out.println((10>5)+""+x+"hello");

(i)check is a string so result of (10>5) is concatenated as a string along with x
(ii)result of (10>5) is a boolean so attempts to do addition of boolean and int so fails.
(iii)the result of (10>5) is a boolean but because "" is used, then it becomes a string so further elements of the expression are added by string concatenation
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always remember that the statement inside System.out.print is evaluated from left to right.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!


Check this Out !
Check Out the PrintStream API Specification for println methods.

When you say !
  • println() function can take on only one value at a time
  • it can either be boolean or int or string or ....... check api for full detail parameters


  • 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 )


    Also , Check this out !

    Even this will work fine !











    [ October 08, 2008: Message edited by: ram kumar ]
     
    Paul Beckett
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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).
     
    ram kumar
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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).




    Hey Paul,

    Welcome ,

    That was already explained, watch the previous post
    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.
    That was already explained, still thanks for clarifying us on this !
     
    Paul Beckett
    Ranch Hand
    Posts: 96
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That was already explained, watch the previous post


    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.
     
    ram kumar
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.



    Done !
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic