aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simpe SCJP question(reason?) 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 "simpe SCJP question(reason?)" Watch "simpe SCJP question(reason?)" New topic
Author

simpe SCJP question(reason?)

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
class A {
public static void main(String[] args) {
char a = 'a'; // 'a' = 97
char b = 'b'; // 'b' = 98
System.out.print(a + b + "" + a + b);
}}

The output displayed is '195ab'. Give me an Explanation for the Output.

:
minu
[ January 19, 2005: Message edited by: minu su ]

God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Java evaluates data type from left to right, and thus, if you perform a+b, the intermediate result is still an interger. However, when you add (a+b) with "", the intermediate result changes to String, and whatever you add with a String, it will be added in String form and thus 'a' and 'b' are added.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Javier Diaz
Greenhorn

Joined: Mar 17, 2003
Posts: 16
This makes you think about overloading common operators (+ in this case)

Perhaps using '+' for String was not a good decision ...
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Thanx Nick & Javier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simpe SCJP question(reason?)