File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return" Watch "return" New topic
Author

return

cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
hi

return(y=z);

is the above statement legal , if yes then are we trying to assign z to y and returning z .
Ed Wallen
Ranch Hand

Joined: Feb 11, 2002
Posts: 34
Yes, technically it is "legal" since the compiler will accept it, but it is sloppy coding. Separate the assignment and the return statement to two lines.

-Ed
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
hi,

return(y=z);

The above code is legal if y and z are boolean variables.
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
Hi,
I just ran this code:

With the result being:
int result: 9
String result: the test did work!!!


So these seem to work fine.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Ed Wallen
Ranch Hand

Joined: Feb 11, 2002
Posts: 34
Surasak,

In response to your post:


return(y=z);

The above code is legal if y and z are boolean variables.


y & z do not have to be booleans. This is an assignment operation. This will work as long as z is assignable to y. However (I'll repeat again), even though this works it is sloppy coding.

-Ed
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I certainly agree that it's sloppy coding, and would never perform it in any usable class. I just created the example previously because of the boolean comment.
cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
class test4
{
static int a=10;
static int amethod()
{int z=0;
return(a=z);//line L
}
public static void main(String[] args)
{
System.out.println("hello" + a + amethod());
}

}
result is hello 10 0

and i thought z value is returned as well assigned to a,but a still remains 10.
pls xplain.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

a isn't set to 0 until amethod() is called; it isn't called until after the value of a is used in the println() statement. Switch the order of a and amethod(), and you'll get 0 0.


[Jess in Action][AskingGoodQuestions]
cybel sheriden
Ranch Hand

Joined: Jan 18, 2005
Posts: 50
!oops that was simple ......i should have given a thought before posting
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return
 
Similar Threads
Final parameter in a method
Recursion
how assignement takes place
Elegant Java
C++ class to Java