• 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 value

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Here in the code below, I have a method which returns an int.
But the call to the method like

p.fliton(); works fine
But I thought it should be assigned to an int like here
int i = p.fliton(); // even this works fine.
Can anyone explain this behaviour ?
public class Test{
public static void main(String argv[]){
Test p = new Test();
p.fliton();
}
public int fliton(){
try{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}

return -1;
}
}

Thanks in Advance.
Aruna
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the int returned is discarded !
Heres something for u to ponder over...
String s = "one";
s = "one" + System.out.println("two");
Does not compile coz : println return void
Therefore, If the compile-time declaration for the method invocation is void, then the method invocation must be a top-level expression, that is, the Expression in an expression statement or in the ForInit or ForUpdate part of a for statement , or a compile-time error occurs
[This message has been edited by Deepak M (edited August 24, 2000).]
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,

Thx for ur reply...but still I'm not clear about it.
Aruna
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An expression denotes nothing if and only if it is a method invocation (�15.12) that invokes a method that does not return a value, that is, a method declared void (�8.4). Such an expression can be used only as an expression statement (�14.8), because every other context in which an expression can appear requires the expression to denote something. An expression statement that is a method invocation may also invoke a method that produces a result; in this case the value returned by the method is quietly discarded.
Therefore in your code, the integer that is returned is discarded !
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx yaar,
Got it clear..... :-)
Aruna
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic