This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

why not i += s?  RSS feed

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 48)
Given the following variables
char c = 'c';
int i = 10;
double d = 10;
long l = 1;
String s = "Hello";
Which of the following will compile without error?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;
I wondering why 3) is not the correct answer? I know i = i+s will giving a correct answer but why not i += s? Thank You.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i += s will fail because you are trying to add a integer and a string. That doesnt make any sense to me , nor will it to the java compiler, so it cribs.
on the same token, i = i + s; is no good. that will fail too
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i = i + s; won't compile either because i + s is a String. You can not assign a String to a int.
 
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When one of the operands of a "+" operation is a String, the other non-String operand(s) get converted to a String and the result of the expression is a String.
Therefore, (s + i) will result in the String "Hello10". So far, so good. However, when you try to assign this String value to an int, the Java compiler will complain about an incompatible type.
(s += i) will work
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried out this,
int i=0;
String s="hello";
i+=s;
System.out.println(i);
result given 59336428.
But the answer for this question is only 2).
char c = 'c';
int i = 10;
double d = 10;
long l = 1;
String s = "Hello";
Which of the following will compile without error?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;
And i make a mistake that i + s will compile fine but not i = i+s;
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What mistake did you make? I didn't understand your last sentence. Also, the code you list does NOT compile, at least not under jdk1.3.0_02. I get the following error, as I expected:
:roll:

"TestAssignment.java": Error #: 354 : incompatible types; found: java.lang.String, required: int at line 10, column 5
"TestAssignment.java": Error #: 374 : primitive type or String required for left hand side of += operator at line 10, column 3
[ January 09, 2002: Message edited by: Rob Ross ]
 
Junilu Lacar
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SteffySY Sing,
I did try compiling the (i += s) code using the Scrapbook in VisualAge for Java Version 3.5.3 and was surprised to find that it was successful. I ran it and got something similar to what you got: a number (mine was a different one though). What IDE or version of Java are you using?
For the purpose of the certification exam, the code should not compile. I tried compiling under Java 1.3.1_01 and got an "inconvertible type" compile error.
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also used Scrapbook in VisualAge for Java Version 3.5
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rob,
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rob,
At first i thought i = i+s will compile fine but i found out i'm wrong.
Steffy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!