This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Array and ==operator.  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator is suppose to compare the reference of the of the two operands if i am not wrong.
In the following code i understand false being printed for the second println statement but why is that when it comes to arrays it prints true.


class Check1{

public static void main(String args[]){

String s[] = {"1"};
System.out.println(s[0]=="1");//prints true
String d = new String("1");
System.out.println(d=="1");//prints false
}
}


Even if you excute the following code it prints true!!!

int i[] = {1};
Long l[] = {1l};
System.out.println(i[0]==l[0]);//prints true

Please explain.
Thanks in advacne.
Srikanth.C.S
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Srikanth CS:
The == operator is suppose to compare the reference of the of the two operands if i am not wrong.
In the following code i understand false being printed for the second println statement but why is that when it comes to arrays it prints true.


class Check1{

public static void main(String args[]){

String s[] = {"1"};
System.out.println(s[0]=="1");//prints true
String d = new String("1");
System.out.println(d=="1");//prints false
}
}



Here new String("1") creates a new String object which is not the same as "1".


Even if you excute the following code it prints true!!!

int i[] = {1};
Long l[] = {1l};
System.out.println(i[0]==l[0]);//prints true

Please explain.
Thanks in advacne.
Srikanth.C.S



When you use an int and a Long in an ==, the Long will be unboxed, and the int promoted to a long for the comparison.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When you use an int and a Long in an ==, the Long will be unboxed, and the int promoted to a long for the comparison.


Strangely...that does not happen when I use

System.out.println(l1[0].equals(l2[0]));
This gives false.

Can someone explain why the RULES are so easy!!!
[ April 29, 2007: Message edited by: megha joshi ]
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by megha joshi:

Strangely...that does not happen when I use

System.out.println(l1[0].equals(l2[0]));
This gives false.

Can someone explain why the RULES are so easy!!!

[ April 29, 2007: Message edited by: megha joshi ]



That code is different from the earlier code.

In the earlier code, a primitive was being compared to a wrapper. In that case, the wrapper was unboxed.

I'm not exactly sure where you are getting l1 and l2 from, but if they refer to an array of Longs and an array of Integers, then the equals method will necessarily return false.

This is from the API docs for Long. But the same idea will apply for Integer.

Compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!

Got it...I was looking for some generic set of rules...I got them with playing around Srikanth's code a bit....also the "not null" point that Keith mentioned was helpful...

Thanks,
Megha
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Strangely...that does not happen when I use

System.out.println(l1[0].equals(l2[0]));
This gives false.

Can someone explain why the RULES are so easy!!!

[ April 29, 2007: Message edited by: megha joshi ]



Sorry Megha, but what are l1 and l2;
Please give some more lines above your code before equals comparison.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!