• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About += operator in array.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I had a question about += operator as follows.

Why names[--a] += "-" and names[--a] = names[--a] + "-" are different in (1) and (2)?
Thanks.

(1)
public class sihao{

public static void main(String[] arg)
{
int a = 1;
String[] names = {"a","b","c"};
names[--a] += "-";

for(int i =0; i<names.length;i++)
{
System.out.println(names[i]);
}
}
}

will print out
a-
b
c

----------------------------------------------------
(2)
public class sihao{

public static void main(String[] arg)
{
int a = 1;
String[] names = {"a","b","c"};
names[--a] = names[--a] + "-";

for(int i =0; i<names.length;i++)
{
System.out.println(names[i]);
}
}
}

will get java.lang.ArrayIndexOutOfBoundsException.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shihao,

Originally posted by Sihao Rui:
Hi, I had a question about += operator as follows.

Why names[--a] += "-" and names[--a] = names[--a] + "-" are different in (1) and (2)?
Thanks.

(2)
public class sihao{

public static void main(String[] arg)
{
int a = 1;
String[] names = {"a","b","c"};
names[--a] = names[--a] + "-";

for(int i =0; i<names.length;i++)
{
System.out.println(names[i]);
}
}
}

will get java.lang.ArrayIndexOutOfBoundsException.



Because on the , the variable a goes negative because it has a value of 1 at the execution of the line. On the left side of the =, a has a value of 0 for the index value and on the right side of the =, a has a value of -1 for the index value. Bad news.

Hope this helps...
Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Sihao Rui
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why names[--a] += "-" works?

Thanks
Sihao
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer the Java Language Specification for "Compound Assignment Operators".

Following is mentioned.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

In the case what you have shown, the expression is evaluated only once and hence it doesn't give exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic