What does this short part of a code do?
Code :
public boolean same_State (Search_State s2) {
Jugs_State js2= (Jugs_State) s2;
return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
}
I just want to know what the part :
Code :
Jugs_State js2= (Jugs_State) s2;
does.
I know you can create an object by e.g. Classname object = new Classname() but
what is this "(Jugs_State)" in the brackets and why is there no "new" and why is
there a new variable "s2" after that.
Finally, what is the return doing at the end?
Thanks a lot, I just need a general answer of what the code is doing
not exactly what my classes do (that's why I haven't included all the code
it's long).
Re: What does this short part of a code do?
It looks to be 'type casting' your passed in parameter 's2'
Re: What does this short part of a code do?
Quote:
Originally Posted by
Kranti1992
Code :
public boolean same_State (Search_State s2) {
Jugs_State js2= (Jugs_State) s2;
return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
I just want to know what the part :
Code :
Jugs_State js2= (Jugs_State) s2;
does.
Jugs_State is apparently a subclass of Search_State. The data type of the function's parameter is Search_State, but the function apparently requires the Jugs_State methods get_j1() and get_j2() to calculate its return value.
The object s2 already exists so the code in the function declares a reference variable of type Jugs_State named js2. It can't set js2 equal to s2 directly since they are different data types.
That's why a cast is required. This type of casting is valid since the objects are compatible.
There is no "new" in the declaration of js2 because it is not creating a new object, it is referring to the object that is the function's parameter.
Reference: Java Tutorial: Inheritance
Cheers!
Z
Re: What does this short part of a code do?
About the return,
Code java:
return ((j1==js2.get_j1())&& (j2==js2.get_j2()));
The function is from type Boolean, so it will either return true or false.
The && sign is the AND operator. and the == sign is to check if 2 variables are equal to each other.
j1 and j2 are probably some variables declared somewhere in the code.
You should be able to draw a conclusion from this. If you are still having trouble finding out what it will return, respond.