Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 13th, 2007, 08:28 PM
Authorized User
 
Join Date: Jun 2006
Location: , , .
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



Reply With Quote
  #2 (permalink)  
Old March 15th, 2007, 12:08 PM
Authorized User
 
Join Date: Mar 2007
Location: , , USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.