File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

+= operator

 
Raghav Aggarwala
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sharan vasandani
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes += and -= will work for Integer and += will work for String also.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>are options C and D legal???

In 5.0, Yes.
 
Raghav Aggarwala
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sharan and krishnan
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic