concatinating the strings with any other primitive type converts them to string System.out.println(" " + 2 + 3) output 23 System.out.println(2 + 3 +"") output 5 can someone explain why?
Originally posted by sona nagee: concatinating the strings with any other primitive type converts them to string System.out.println(" " + 2 + 3) output 23 System.out.println(2 + 3 +"") output 5 can someone explain why?
The first example resolve as ( (" " +2 ) + 3 ) "If any one argument is a String the other argument gets converted to String" So we get o/p of 23 In the second example resolve as ( (2+3)+"") we proceeds as( 5+"") so we get o/p of 5
In any expression involving a string constant, other operands are converted to their string equivalents( using appropriate wrapper classes ).
String constants, through internally represented as Objects behave as primitives!
Cheers! ------------------ Ajith Kallambella M. Sun Certified Programmer for the Java�2 Platform. IBM Certified Developer - XML and Related Technologies, V1.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
what happens , if I write : System.out.println('2'+3+""); Syatem.out.println( '2'+'3'+""); Will '2' & '3' get converted to String ? Ajith , can you explain?
Originally posted by Stevie Kaligis: [B]System.out.println('2'+3+""); will be evaluate : (('2'+3)+"") 1. ('2'+3) convert to integer, 2. ((50 + 3) + "") output : 53 System.out.println( '2'+'3'+""); will be evaluate : (('2'+'3')+"") 1. ('2'+'3') convert to integer, 2. ((50 + 51) + "") output : 101 Stevie: Could you please explain how '2' in char evaluates to 50 in int? Thanks.
Hi Parimala, 'char' types are represented internally by their Unicode values. The Unicode value of '2' is 50, the Unicode value of '3' is 51. That's why you can perform arithmetic operations on them. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
It's a decimal equivalent (0-127) of the character code. stevie
shailesh sonavadekar
Ranch Hand
Joined: Oct 12, 2000
Posts: 1874
posted
0
what about : - System.out.println('1'+'2'+"String"); System.out.println("String"+2+'3'); what happens behind the scene ? Ajith ???
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Consider the first expression System.out.println('1'+'2'+"String"); Since there is no precedence overriding with the use of paranthesis, all operators have the same precedence and left-to-right associativity. '1' + '2' is an arithmetic expression and the result has to be atleast as wide as an integer. Hence all non-integer operands( '1' and '2' ) are widened to int types and hence get their integer equivalents. The resulting vale is 99. The expression now is 99 + "String" Now apply my rules Since one of the operands is a String object, the other must be converted to String representation. Since 99 can't fit into a byte, I'm guessing Java uses a Short wrapper class to convert 99 to string. ie., Short(99).toString()+"String" which becomes "99String"
The same explanation holds good for the second expression System.out.println("String"+2+'3'); The only difference here is that from the very beginning the expression becomes a String expression and all the operands get converted to their String values since the very first operand itself is a string constants. "String" + 2 + '3' becomes ( "String" + Byte(2).toString) + '3' becomes "String2" + '3' becomes "String2" + Char('3').toString --> Apply my rules No need to convert '3' to integer since the other operand is a string!! becomes "String23" Hope that helps!
[This message has been edited by Ajith Kallambella (edited April 25, 2001).]
Originally posted by Jane Griscti: Hi Parimala, 'char' types are represented internally by their Unicode values. The Unicode value of '2' is 50, the Unicode value of '3' is 51. That's why you can perform arithmetic operations on them. Hope that helps.
Hi Jane, Do we need to memorize the unicode characters? If so ,could you tell me where can I find the unicode values for all the alphabets and numbers? and any necesssary info that you might think is important. (I really hope that I don't have to know this )
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Seema, No, you don't have to memorize them Just need to remember that 'chars' are stored as Unicode values so if you see something like:
You know it's valid. Does help to remember the LF and CR values. See JLS §3.10.6 Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
I think of this as the + operator is overloaded for Strings, not for other types. So if the variable before the + is of type String, the + behaves a sa concatenation operator.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.