File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

+= operator

Raghav Aggarwala

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

Here is the question

Which of the following code fragments are legal Java Code?

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

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

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

Integer J = new Integer(27);

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,


-----------------<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

Joined: Jul 21, 2005
Posts: 26
thank you sharan and krishnan
I agree. Here's the link:
subject: += operator
jQuery in Action, 3rd edition