File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strings - marcus green mocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strings - marcus green mocks" Watch "strings - marcus green mocks" New topic
Author

strings - marcus green mocks

sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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?

sona<br />SCJP
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
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?
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
It's a decimal equivalent (0-127) of the character code.
stevie
shailesh sonavadekar
Ranch Hand

Joined: Oct 12, 2000
Posts: 1874
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
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).]
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
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
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
ethan winograd
Greenhorn

Joined: Mar 20, 2001
Posts: 7
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://aspose.com/file-tools
 
subject: strings - marcus green mocks
 
Similar Threads
String.split method result
More Code Concepts...
John Hunt mock exam Q 14
Variable Initialization...
compareTo() Method of the String Class