DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    8

    Object type compare

    I need to be able to make a method which performs a generic Object type comparison:

    boolean compare(Object obj1, Object obj2)
    {
    if(obj1 type == obj2 type)
    return true;
    else
    return false;
    }

    Example:

    Image var1, var2;
    int var3, var4;

    test1 = compare(var1, var2);
    test2 = compare(var3,var4);
    test3 = compare(var1,var4);
    test4 = compare(var2, var3);

    and the result should be: test1=true, test2=true, test3=false, test4=false.

    How can I do?

    I think I can't use "istanceof", because in this case I must know the type of one of the two object in the comparison. In this case the comparison would be no more generic.

    RB
    Thanks
    R.B.

  2. #2
    Join Date
    Jan 2004
    Posts
    2
    The only thing I can think of is making certain cases. Like making a completely genetric compare function with Object I don't think is possible. Like you'd have the instanceof function but you have like things you would think might show up in the program. This way might get a little long so I'd might try and find another way. Sorry.
    "Real programmers don't comment their code because if a program was hard to make it should be hard to understand."

  3. #3
    Join Date
    Oct 2003
    Posts
    8

    Objetc Type Compare (getClass())

    Thanks

    I think I'll solve the problem using the method Object.getClass().toString() and then using a string comparison.
    R.B.

  4. #4
    Join Date
    Jan 2004
    Posts
    1
    boolean compare(Object obj1, Object obj2)
    {
    if ( (obj1.getClass()).instanceOf(obj2.getClass()) )
    return true;

    return false;
    }

  5. #5
    Join Date
    Dec 2002
    Posts
    83
    The above suggestion doesn't compile. I think you're looking for:
    Code:
    class Test 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("\n-=-= Begin Compare test =-=-");
    		String str1 = new String("abc");
    		String str2 = new String("bca");
    		Integer int1 = new Integer(5);
    		
    		System.out.println("should be true: "+compare(str1,str2));
    		System.out.println("should be false: "+compare(str1,int1));
    
    	}
    
    	public static boolean compare(Object obj1, Object obj2)
    	{
    		boolean isEqual = false;
    		if ( (obj1.getClass()).isInstance(obj2) ) {
    			isEqual = true;
    		}
    		return isEqual;
    	}
    }
    -- Steven

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links