Author | Topic: Statements |
raja pratap greenhorn |
posted March 21, 2000 10:55 PM
Which of the following are valid statements and why? Can anyone help me out?? [1]System.out.println(true);
|
Thandapani Saravanan bartender |
posted March 21, 2000 11:28 PM
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.
|
Sharad Kanmurde greenhorn |
posted March 22, 2000 06:12 AM
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 |
posted March 22, 2000 06:14 AM
statement 1,2,3 are correct and statement 4 is not valid since it is not possible to add integer to boolean.
|
maha anna bartender |
posted March 22, 2000 08:33 AM
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
|
Chris, Grindstaff greenhorn |
posted March 22, 2000 08:48 AM
quote: 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. 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] J D K 1.3 (build 1.3beta-O) [correct answers would be 1, 3] Clearly I should have tested this better, and of course System.out.println((Object)null); will work fine too.
|
sree ranch hand |
posted March 22, 2000 09:31 AM
Maha, System.out.println(null) is giving error. I am using jdk1.2.2.
|
maha anna bartender |
posted March 22, 2000 10:56 AM
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 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); 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 Note: One funny thing is regds
|
sree ranch hand |
posted March 22, 2000 12:16 PM
Thanks Maha.
|
| | |