• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

difference between add("hello") and add(new String("hello"));

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any difference between;

List<String> alist = new ArrayList<String>();
alist.add("hello"); //1
alist.add(new String("hello")); //2

difference between add("hello") and adding string using new operator.
or
List<Integer> alist = new ArrayList<Integer>();
alist.add(1200);
alist.add(new Integer(1200));

Please suggest!

Thanks and Regards,
cmbhatt
[ April 04, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,

You have n't mentioned what kind of difference you are looking for ?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra-



In the code above, line 2 creates an unnecessary String object. You can verify this by doing an == test between "hello" and new String("hello"). It fails because a new object is being created. Whereas an == test between "hello" and another "hello" (both literals) will succeed due to the fact that they are the same object in the String pool. Generally it's never a good idea to use the new keyword when creating Strings. It makes it so that Java can't do tricks with the String pool to optimize performance. In fact, many coding standards tools (e.g. Checkstyle) specifically have rules to prohibit such statements.




As far as I know there is really no difference between line 3 and line 4. Lists cannot hold primitives. So pre-Java 5, the code above would not compile due to line 3. As of Java 5 line 3 is fine. The int primitive gets auto-boxed into an Integer object and ends up being pretty much the same as line 4 behind the scenes.

I hope that answers your question.
Joshua Smith
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules on Autoboxing on Integer fields is as follows:

If the value is between -128 and 127 and you are using the auto boxing method of creating your Integer object, then the value is immutable. This therefore means that if 2 or more objects have the same value, only 1 occurance of the value will be found in memory and the value is therefore shared. If one of the Integer objects is declared using the new Integer method (e.g.Integer i8 = new Integer(100) then there will be more that one object allocated in memory. Outside of this range (-128 to 127) the value becomes mutable and therefore irrespective of the method of the Integer creation (autoboxing or new Integer) the objects will always reference different objects.

e.g.

public class test {


public static void main (String[] args) {
// immutable references
Integer i7 = 100;
Integer i8 = new Integer(100);
Integer i9 = 100;
if (i7!=i8) System.out.println("i7 and i8 reference different objects");// if statement returns true
if (i7==i8) System.out.println("i7 and i8 reference same object");// if statement returns false
if (i7.equals(i8)) System.out.println("i7 and i8 contents are the same");// if statement returns true
if (i7==i9) System.out.println("i7 and i9 reference same object");// if statement returns true

// mutable value comparison because value is greater than 127
Integer i1=1000;
Integer i2=1000;
Integer i3=new Integer(1000);
if(i1 != i2)System.out.println("i1 and i2 reference different objects");// if statement returns true
// because they are referencing different objects
if(i1 == i2)System.out.println("i1 and i2 reference same object");// if statement returns false
if(i1.equals(i2))System.out.println("i1 and i2 contents are the same");// if statement returns true
if(i1!=i3)System.out.println("i1 and i3 reference different objects");// if statement returns true
if(i1.equals(i3))System.out.println("i1 and i3 contents are the same");// if statement returns true
}
}
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Smith,

cmbhatt
reply
    Bookmark Topic Watch Topic
  • New Topic