• 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

basic question

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

java XYZ ravi // this gives me "I am bad"
if i replace == with .equals it works fine, i dont understand why it's not working with ==

but, this works fine, i don't understand how these two snippets are different?
can anyone please explain me the behavior.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second snippet of code you have assigned the variable st to "ravi", so the IF check will always be true.

In the first code snipped you are doing a check for the first element in the array str which is not equal to "ravi"

What were you expecting in each case?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference depends on the presence of the String in the heap / pool. The == comparison compares the String with the ones in the pool while .equals does a character by character comparison with another String.

String x = "ravi"; creates a string in the string pool
String x = new String("ravi"); creates a string in the pool and heap
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I think what Ravi wants to say is the following:

1) We know that the equality sign '==' means 'equal in reference' but that
for the String object the equality sign will return TRUE if they are referring
to the same string in the heap (i.e. not instantiating a new String object with the keyword "new")

2) Assuming that we run the application passing an argument (below, using "arg1" as a string argument)
why the behaviour is different using the argv array compared to another array when we pass the String
value to a String variable?

... what is different about the argv array?

If you run the following code with "arg1" as an argument, maybe is more clear what i'm trying to say...


 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class XYZ{
public static void main(String str[]){
String[] s={"ankur","ankur","ankur","ankur"};
System.out.println(str[0]+" "+s[0]==str[0]);
}
}

java XYZ ankur

why is the answer only false and not ankur false? and why false when

s[0]==s[1] gives true
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur kothari wrote:class XYZ{
public static void main(String str[]){
String[] s={"ankur","ankur","ankur","ankur"};
System.out.println(str[0]+" "+s[0]==str[0]);
}
}

java XYZ ankur

why is the answer only false and not ankur false? and why false when

s[0]==s[1] gives true



Is it not the case that s[0]==str[0] is comparing the memory locations and not what they are refering to?
 
Davide Crudo
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankur:


java XYZ ankur

why is the answer only false and not ankur false?



in a Boolean expression (correct me if i'm wrong) the left side is evaluated first, so what you are actually saying is:

(str[0]+" "+s[0]) == str[0]

Or: is "ankur ankur" = "ankur" ? Which is only ONE Answer...
if you want to have it correctly evaluated, you need to change
your code as below:




Dave
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh thanks a lot....

@ ziggy: but when i do s[0]==s[1] why does it give me true
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Santiago Bravo

In the first code snipped you are doing a check for the first element in the array str which is not equal to "ravi"
java XYZ ravi


Santiago Bravo, what do you think first element would be, if it is not 'ravi'? please don't reply becuase you WANT to reply. If you have genuine answer do reply, people would appreciate.

Thanks very much Davide Crudo, your reply answers my question.
 
reply
    Bookmark Topic Watch Topic
  • New Topic