• 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

Strings

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static final String[] name1 = new String[] {"aaa","bbb"};
public static final String[] name2 = {"aaa","bbb"};
public static void main(String args[])
{
boolean b1 = name1[0]==name2[0];
System.out.println("name1[0] == name2[0]" + b1);
}

the out put is name1[0]== name2[0] true.

so the above intializations are same ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rami reddy marri:
public static final String[] name1 = new String[] {"aaa","bbb"};
public static final String[] name2 = {"aaa","bbb"};
public static void main(String args[])
{
boolean b1 = name1[0]==name2[0];
System.out.println("name1[0] == name2[0]" + b1);
}

the out put is name1[0]== name2[0] true.

so the above intializations are same ?



No the arrays are different.

However, String literals are stored in a String pool and all references to the String literal are to the same object.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The normal strings like String s="Hello";

will be stored in string pool. As you might be knowing that string maintains a constant pool. If you get any object same to "Hello".


It will not allocate new memory for it rather assigns the reference to same object which is in pool.

Meaning String s1="Hello";
now s and s1 are two references pointing to same object which is in constant string pool.

But where as if we use new operator it creates a new object.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1)public static final String[] name1 = new String[] {"aaa","bbb"};
(2)public static final String[] name2 = {"aaa","bbb"};

It is showing me as name1[0] == name2[0] = true. So alias are same . But in the first declaration and intialization , i created using new and assigned . But in second i assigned string literals . So if we intilaize as in (1) , then it is same as (2) ?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call to new creates a new String array.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two different arrays. However they have references to the same objects.

Search for String constant pool in the forum.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi javier/kaith

thanks for reply.

public static final String[] name1 = {"rami", "siva"};
public static final String[] name2 = {"rami", "siva"};

in this name1 == name2 returns "false".

here we are not creating new string array.

can you explain in detail
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are creating arrays implicitly.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following two statements:

and:


are two way to write the absolutely same thing. New array is created in both of them.


and

are different. The first uses the String from constant pool, the second creates a new String different from the "aaa" in constant pool.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to specify which element in String OBJECT you want to compare, you cannot compare References with == operator, for that i think you need equals method.

to me it's mirroring

String [] array1 = {"abc", "def"};
String [] array2 = {"abc", "def");

if you check arrya1[0] == array2[0] ==> gives you TRUE
if you check array1.equals(array2) ==> gives you FALSE

=== WHY ==

the first one seems obvious since we are comparing values of arrays
the second one seems we are comparing bit representation string array location. and It's gotta be different since we have two objects.

===Please correct me if i am wrong anywhere so that i can have better idea "

--Thank you
--Chintan Ramavat
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'==' checks the objects compared are in same location or not and 'equals' method checks if the the objects compared are "deeply meaningfully" equal or not conversely as Chintan as said.

If you create two strings...

String a = "what?"
String b = "what?"

Both are stored in String pool. Infact, if such Strings are found to be equal then they are referred to same object.

a (reference variable)----> (object containing "What?") <----- b(reference variable)

result of a == b is true

however...if we create String objects in following fashion...

String a = new String("what?");
String b = new String("what?");

then two different objects will be created where a will refer to different object and b will refer to different object.

result of a == b would be false.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic