Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return value

 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
help
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thx yaar,
Got it clear..... :-)
Aruna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic