• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

strings - marcus green mocks

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The points to note here are as follows -

  • The '+' operator is left-to-right associative.
  • 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.
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a decimal equivalent (0-127) of the character code.
stevie
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about : -
System.out.println('1'+'2'+"String");
System.out.println("String"+2+'3');
what happens behind the scene ?
Ajith ???
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic