aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Statements" Watch "Statements" New topic
Author

Statements

raja pratap
Greenhorn

Joined: Mar 21, 2000
Posts: 21
Which of the following are valid statements and why?
Can anyone help me out??
[1]System.out.println(true);
[2]System.out.println(null+null);
[3]System.out.println(true+null);
[4]System.out.println(true+8);
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
Only [1] is right. The operator + makes sense only if any one of the operand is String (or if both are numeric values).
Cases 2, 3, 4 don't have a String.
When any of the operand is String the compiler applies toString() method to other operands.


Saravanan
Sharad Kanmurde
Greenhorn

Joined: Mar 15, 2000
Posts: 3
statement 1,2,3 are correct and statement 4 is not valid
since it is not possible to add integer to boolean.
Sharad Kanmurde
Greenhorn

Joined: Mar 15, 2000
Posts: 3
statement 1,2,3 are correct and statement 4 is not valid
since it is not possible to add integer to boolean.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sahrad,
Thandapani is correct. Only answer a) is correct. The overloaded '+' operator in Java works provided the foll. rules are met.
1. Both operands are numeric.
2. atleast One of the 2 operands is String object
In the first case the arithmetic addition is done, and a corresponding Wrapper class is created on the fly and its toString() method is called,and the return String of this toString() method is printed.
In the second case the non-String object's toString() output and the String object are concatenated according to their places in the + operator and the resulted String is printed.
Also note that there is a String conversion for every object type in java including the famous ( ) true,false,null literals.
regds
maha anna
Chris, Grindstaff
Greenhorn

Joined: Mar 07, 2000
Posts: 4
Originally posted by raja pratap:
Which of the following are valid statements and why?
Can anyone help me out??
[1]System.out.println(true);
[2]System.out.println(null+null);
[3]System.out.println(true+null);
[4]System.out.println(true+8);


I don't really consider this a very good question. I can say that because I'm the author of the question <grin>. I was orginally trying to test whether or not you knew that System.out.println(null) would work. The reason that I now consider this a poor question is because different versions of the JDK give different results.
When I originally tested this in VisualAge for Java (where I wrote my mock) the answers are as I indicated (the correct answers in VaJava were 1,2,3.) I tried to also test everything outside VaJava but apparently I missed this one. <dang>
I get interesting results depending on the version of the JDK I use (all of these on WinNT):
J D K 1.1.8 [correct answers would be 1]
NullTest.java:4: Reference to println is ambiguous. It is defined in void println (java.lang.String) and void println(char[]).
System.out.println(null);
^
NullTest.java:5: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+null);
^
NullTest.java:5: Incompatible type for +. Can't convert null to int.
System.out.println(true+null);
^
NullTest.java:6: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+8);
J D K 1.3 (build 1.3beta-O) [correct answers would be 1, 3]
NullTest.java:4: reference to println is ambiguous, both method println(char[])
in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStre
am match
System.out.println(null);
^
NullTest.java:6: cannot resolve symbol
symbol : method + (boolean,int)
location: class NullTest
System.out.println(true+8);

J D K 1.2 ((Classic VM (build JDK-1.2-V, native threads)) [correct answers would be 1]
NullTest.java:4: Reference to println is ambiguous. It is defined in void printl
n(java.lang.String) and void println(char[]).
System.out.println(null);
^
NullTest.java:5: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+null);
^
NullTest.java:5: Incompatible type for +. Can't convert null to int.
System.out.println(true+null);
^
NullTest.java:6: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+8);
Clearly I should have tested this better, and of course System.out.println((Object)null); will work fine too.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Maha,
System.out.println(null) is giving error. I am using jdk1.2.2.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sree,
From JLS
5.1.6 String Conversions
There is a string conversion to type String from every other type, including the null type.
The above type of convertion to String for all objects takes place when they are used in the overloaded '+' operator. As I mentioned in previous post, when one of the operand of + operator is a String object, the other undergoes a string conversion and then added as stated in JLS. So

System.out.println("maha"+null); //prints mahanull
System.out.println("maha"+true); //prints mahatrue
System.out.println("maha"+true); //prints mahafalse

When you see the PrintStream class, there are so many overloaded println(..) methods. (Note that System.out is a PrintStream). Whenever you invoke a method on a object/class the more specific method is always called. Here there are 2 overloaded println(..) methods which take null as an argument and none of them are more specific than the other. They are
System.out.println(Char[] x);
and
System.out.println(String s);
There is another println(Object o) which also can take 'null' as an argument but this is elminated because the println(char[] c) and println(String s) are more specific than this.
When you call System.out.println(null) the compiler does not know which version of println(..) to call. So it says it is ambiguous.
If you would have been called as System.out.println((Object)null); //prints null
System.out.println((String)null); //prints null
Note: One funny thing is
System.out.println((char[])null); //prints NullPointerException
Because the authors who wrote this function forgot to handle the 'null' argument. So the JVM throws Runtime NullPointerException
regds
maha anna
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks Maha.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Statements