• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

return(y=z);

is the above statement legal , if yes then are we trying to assign z to y and returning z .
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

return(y=z);

The above code is legal if y and z are boolean variables.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ed Wallen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
cybel sheriden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
!oops that was simple ......i should have given a thought before posting
thanks
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic