Friday, 15 June 2012

java - What does the return line do? -



java - What does the return line do? -

i have class estado, as:

public class estado implements comparable { public int x; public int y; . . . public boolean equals(object x) { estado e = (estado) x; homecoming this.x==e.x && this.y==e.y; } }

what line

return this.x==e.x && this.y==e.y;

do?

return ...; means "evaluate ... , exit function, making resulting value function's homecoming value."

so let's break down:

this.x==e.x

== equality operator. line checks see if this.x equal e.x (and this.y==e.y same ys).

let's phone call result of x check xresult , result of y check yresult.

next, have &&:

xresult && yresult

&& boolean "and" operator (the spec calls "conditional-and operator"): it's true if both of operands true, false if either of them false.

so function homecoming true if both conditions true, false if either status false.

side note: chrylis points out in comment, there's way function terminate: called exception. first line of function, estado e = (estado) x;, might "throw" (cause) exception if argument passed function refer object, object isn't estado object (so "cast" (estado)x invalid); line asked might throw exception if argument passed function null (doesn't refer object). when exception thrown within function , not "caught" code in function, function stops running not via return statement.

java return this

No comments:

Post a Comment