File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Array and ==operator. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Array and ==operator." Watch "Regarding Array and ==operator." New topic
Author

Regarding Array and ==operator.

Srikanth CS
Greenhorn

Joined: Apr 10, 2007
Posts: 9
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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206

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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 20, 2007
Posts: 206
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
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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.


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Array and ==operator.
 
Similar Threads
Comparing (==) strings
Conditional Operator
Few Tricky Ques.
2 Questions
Dan's Tests | Question from 'Conditional Operator'