File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return value 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return value" Watch "return value" New topic
Author

return value

Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
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

Joined: Jul 20, 2000
Posts: 112
help
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
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

Joined: Jul 20, 2000
Posts: 112
Deepak,

Thx for ur reply...but still I'm not clear about it.
Aruna
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
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

Joined: Jul 20, 2000
Posts: 112
Thx yaar,
Got it clear..... :-)
Aruna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return value
 
Similar Threads
exceptions
equals and hashCode??
return statement
Marcus Exam Q#60
Marcus Mock 3 question #60