Hello, I'd like to create a function that accepts two objects and compares properties between them. Example

public boolean (obj1, obj2){
           if obj1.property1 = obj2.property2
                    return true
           else
                    return false
}

How can I do this?

Is this just a syntax question? Because your psuedocode is (nearly) correct.

// MyObject is the class of the object you want to compare
public boolean(MyObject obj1, MyObject obj2){
    if (obj1.getSomeProperty() == obj2.getSomeProperty())
        return true;
    else
        return false
}

Or, more simply:

public boolean(MyObject obj1, MyObject obj2){
    return obj1.getSomeProperty() == obj2.getSomeProperty();
}

> Hello, I'd like to create a function
_______________________^^^^^

Method would be the correct terminology here.

> that accepts two objects and compares properties between them.

That largely depends on the type of those properties. If they are primitives, the comparison operator will do the job. If they are reference types, comparing their references doesn't serve much of a purpose. Then again it depends on whether you are in need of a deep or a shallow comparison.

If you are in need to comparing two objects of the same run-time type, consider overriding the equals() method of the Object class for your custom class. If you are in need of selective comparison of properties, look into the Comparator interface.

Also, explaining the real scenario rather than the end objective would fetch more helpful answers.

I just did that quickly, without paying too much attention to the syntax. My question isn't related on syntax rather on how to use objects as 'data types" sorta to use them as arguments. Let me give you another example

public void readThis(String read){
          System.out.println("The string to read is: " + read);
}

What I want is, instead of the argument being a string data type, I'd like the argument to be an object. The whole purpose of this is that I want to compare the properties of 2 objects. Say objects are tables, I want to input 2 tables and use the method I'm creating to determine if both objects are the same (have the same property values)

I hope that clear things up a little

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.