---------------------------------------------- Q14 Which of the following do not lead to a runtime error? A) "john" + "was" + "here" B) "john" + 3 C) 3 + 5 D) 5 + 5.5 Select all correct answers. ----------------------------------------------- I know the answer is ABCD. My question is: Will B or D lead to a compile error since the first operand and the second operand are different type? Say: System.out.println("john" + 3); or System.out.println(5 + 5.5); What the printing results will be? Many many thanks! --Moya
Padma Shyam
Greenhorn
Joined: Feb 14, 2002
Posts: 7
posted
0
In the statement System.out.println("John"+3) 3 is also converted into String and so the output is John3. In the second one,numeric promotion takes place and so 5 is converted into double.The output is 10.5.
Jennifer Wallace
Ranch Hand
Joined: Nov 30, 2001
Posts: 102
posted
0
All the above cases would work just fine! > When the operator '+' is used with numeric operands, if both the operands are not of the same type then binary numeric promotion takes place. So here, System.out.println(5 + 5.5); Integer literal '5' is converted to floating point literal (5.0) and added with 5.5 giving you 10.5 as the result . Please see rules for this conversion in the below specified link! > '+' is also String Concatenation operator when even one of the operand is a string. So, 'any datatype' + String = String Here, System.out.println("john" + 3); Wrapper Classes are used to convert numeric datatypes to their corresponding Wrapper classes and then that ref. is converted to a String. The exact rules are found here in JLS 15.18 http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html
Rajinder Yadav
Ranch Hand
Joined: Jan 18, 2002
Posts: 178
posted
0
Try the following code to understand what is going on! System.out.println(3+5+" = "+3+5); If you guessed the output would be "8 = 8" then you're wrong The output will be (drum roll please): "8 = 35" Why such an output? Statements are evaluated from left to right, actually the + operator is left associative. Here is the break down of the steps that take place: step 1: 3+5 gets evaluated first, the two are both numbers so an arithmetic addition takes place step 2: the result from step one gets added to the string " = ", the result is "8 = " since the + operator is overloaded to concat with strings step 3: "8 = " gets added to interger value 3, since Strings overload the + operator to concat strings, the value 3 is promoted to a string object and added resulting in "8 = 3" step 4: like string 3, with 5 getting added to a string
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Moya Green
Ranch Hand
Joined: Jan 24, 2002
Posts: 49
posted
0
Thank you, guys! Your explanations are very helpful. I got it. --Moya
mahesh zarkar
Greenhorn
Joined: Feb 14, 2002
Posts: 24
posted
0
Mayo, Where can i get the mock exams for Juhn Hunt. Please advice. Thank you.