• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

no assignment is needed here?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {

public static void main(String[] args) {
Test t=new Test();
t.amethod();
}

int amethod () {
return -1;
}
}
The program compile and run (putput nothing). So it's not necessary to assign t.amethod to an int variable?
can somebody explain this? Thanks.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
t.amethod() is an expression statement and the value is discarded. Quoting from JLS


14.8 Expression Statements
Certain kinds of expressions may be used as statements by following them with semicolons:
ExpressionStatement:
StatementExpression ;
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded. Execution of the expression statement completes normally if and only if evaluation of the expression completes normally.


here t.amethod() is MethodInvocation and the value is discarded.

Originally posted by Bin Wang:
public class Test {

public static void main(String[] args) {
Test t=new Test();
t.amethod();
}

int amethod () {
return -1;
}
}
The program compile and run (putput nothing). So it's not necessary to assign t.amethod to an int variable?
can somebody explain this? Thanks.



[This message has been edited by Anshul Manisha (edited June 26, 2001).]
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic