Author | Topic: Concatenation |
Suresh Ray greenhorn |
posted March 07, 2000 05:14 PM
Hi, One of Mock exam questions: Which of the following are valid statements? 1) System.out.println(1+1); // ans For (3), IMO, + would convert 'one' to String representation and concatenate with "on" right? Correct me if I am wrong. Would anyone please clarify this please? Thanks..
|
Jim Yingst sheriff |
posted March 07, 2000 05:49 PM
A single character constant could indeed be represented that way, and concatenated onto a string by the + operator. But "one" consists of three characters, and so it must be a string, and needs double quotes. Well, I suppose there's one other way to do it: String s = "on" + 'o' + 'n' + 'e'; The point is, only one char at a time is allowed inside single quotes.
|
Java2learner greenhorn |
posted March 07, 2000 06:04 PM
About the option (2), int i= 2+'2'; Is this an implicit conversion from char to int ? What method is used here for conversion? [This message has been edited by Java2learner (edited March 07, 2000).]
|
maha anna bartender |
posted March 07, 2000 07:14 PM
int i = 2+'2'; This is the case of binary numeric promotion The rules are
Here the last rule applies. Since 2 is an int literal, the other operand char '2' is promoted to int and addition takes place. The result is of type int which is assiginable to LHS var i. regds maha anna [This message has been edited by maha anna (edited March 07, 2000).]
|
monty greenhorn |
posted March 08, 2000 10:29 AM
1) System.out.println(1+1); answer: 1+1 become an int of 2 which is then converted into a string 2) int i= 2+'2'; 3) String s= "on"+'one'; 4) byte b=255; [This message has been edited by monty (edited March 08, 2000).]
|
Java2learner greenhorn |
posted March 08, 2000 02:50 PM
Thanks for your replies..
|
Jane Rozen ranch hand |
posted March 10, 2000 05:24 PM
Just make sure you understand that '2' is not converted to int 2, but to 50 !!
|
| | |