aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string + int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string + int" Watch "string + int" New topic
Author

string + int

javi cervera
Greenhorn

Joined: Jan 17, 2002
Posts: 16
this code compile:
String s = "hello" + 7 ;
ok
but this compile also?
String s = 7 + "hello";
in my compiler yes, but i have read in certification books that this code not compile. What is the answer in the exam?

Thanks in advance.
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
yes. In both the cases, 7 will be converted into string and will be concatenated to hello

Originally posted by wasarta:
this code compile:
String s = "hello" + 7 ;
ok
but this compile also?
String s = 7 + "hello";
in my compiler yes, but i have read in certification books that this code not compile. What is the answer in the exam?

Thanks in advance.


MT
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Wellcome to the Ranch wasarta.
Please read our namimg policy and change your displayed name accordingly.


SCJP2. Please Indent your code using UBB Code
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The Java compiler treats + as string concatenation if either operand is a String.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
The order is important, though. See this sample code:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string + int