Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 13th, 2007, 08:28 PM
Authorized User
 
Join Date: Jun 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default About object

Hi

/*********** CODE *************/
public class Test {
 private int num;

 public Test(int num) { this.num = num; }
 public boolean check(Test obj) {
   return equals(obj);
 }

 public static void main(String[] args) {
   Test m = new Test(2);
   Test n = new Test(2);

   System.out.print(m.check(n));
 }
}
/*********** CODE *************/

On the code above java print FALSE, but both value of the objects are equals. I know that those 2 object has different address and that's why they are not equals. My question is how can i make them equals???
In Integer class, if we create 2 different obj with same value, they print statement will print TRUE, How they do that?

I try to create generic class of linked list and i need way to see if the given object as type E is equal to one of the object in my linked list. Since i cannot have an access to obj data, i cannot see that each object carrying same data. but my node class can return obj with type E which is on my list.

Thanks
Mani



 
Old March 15th, 2007, 12:08 PM
Authorized User
 
Join Date: Mar 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you're doing here is checking the equality of the Objects themselves. This means that, because you create both m and n with it's own new statement then m and n will be different according to what the equals method checks for.

In order to have this working the way you want it to, you need to create your own equals method for your class to override Object's equals method.






Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Serializing Object Graph - Assigning Object to Jag venkat.kl C# 0 August 28th, 2006 10:39 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM
Error Occurred creating Report Object: Object does sa_moizatyahoo Classic ASP Professional 0 February 1st, 2005 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.