dcsimg
== overload operators in JAVA ==
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   laeticia_corlean
Posted On:   Wednesday, October 9, 2002 07:30 AM

Hi, I have 2 objects that have the same structure, but belong to 2 different packages: package org.mycorp.utils; public Class LabelIndexBean { private int index = 0; private String label = 0; public LabelIndexBean( int index, String label ) { this->index = index; this->label = label; } public void setIndex( int index ) { this.index = index; } public void setLabel( String label ) { this.label = label; } public int getIndex() { return this.index; } public String getLabel() { return this.label; } } package org.anothercorp.utils; publi   More>>

Hi,


I have 2 objects that have the same structure, but belong to 2 different packages:



package org.mycorp.utils;

public Class LabelIndexBean {

private int index = 0;

private String label = 0;

public LabelIndexBean( int index, String label ) {

this->index = index;

this->label = label;

}

public void setIndex( int index ) { this.index = index; }

public void setLabel( String label ) { this.label = label; }

public int getIndex() { return this.index; }

public String getLabel() { return this.label; }

}


package org.anothercorp.utils;

public Class StringlIndexBean {

private String indexString = 0;

private String theString = 0;

public StringlIndexBean( String indexString, String theString ) {

this.indexString = indexString;

this.theString = theString;

}

public void setIndex( String indexString ) {
this.indexString = indexString; }

public void setLabel( String theString ) { this.theString = theString; }

public String getIndex() { return this.indexString; }

public String getLabel() { return this.theString; }

}




I would like to implement an equality operator in order to be able to say:



StringlIndexBean theStringIndexLabelObj = new StringlIndexBean( "0", "please select");

LabelIndexBean theLabelIndexObj = new LabelIndexBean();

Then


theLabelIndexObj = theStringIndexLabelObj;


In C++ it is possible to overload the standard operators (+=, -= +, -, ...etc ...) How do we do in JAVA?


Thanks for your help!!!

   <<Less

Re: == overload operators in JAVA ==

Posted By:   Lasse_Koskela  
Posted On:   Wednesday, October 9, 2002 07:41 AM

Operator overloading is not supported in Java. But. The "equality operator" behaviour can be implemented by overriding the public boolean equals(Object obj) method from java.lang.Object.
About | Sitemap | Contact