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

+= operator

Raghav Aggarwala
Greenhorn

Joined: Jul 21, 2005
Posts: 26
Hi Guys,

Here is the question

Which of the following code fragments are legal Java Code?

A.
String A = "abcdefg";
A -= "cde";

B.
String A = "abcdefg";
A += "cde";

C.
Integer J = new Integer(27);
J -= 7;

D.
Integer J = new Integer(27);
J--;


The answer to the above question is B. Explaination: Since String is the only class for which an assignment operator += is defined.
Source: Bill Brogden Exam Cram 2 Sample Test Two

Now I tried compiling option C & D. Both works fine. Are options C and D legal???

Please help..

thank you all,

Raghav


-----------------<br />SCJP 1.4<br />Target SCWCD by December 2007
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
yes += and -= will work for Integer and += will work for String also.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
>>are options C and D legal???

In 5.0, Yes.
Raghav Aggarwala
Greenhorn

Joined: Jul 21, 2005
Posts: 26
thank you sharan and krishnan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: += operator