Definition of a java statement...

Hi

I have this simple method

public void test(int i,float f){

i/f;// divide integer by float

}

Compile javac says that it is not statement .

Modify the method to say this

public void test(int i,float f){

Math.sqrt(i/f);// divide integer by float and invoke sqrt fn using Math class

}

javac does not complain here

My point is this.

The result of Math.sqrt is not used and the compiler just uses the JVM

instruction pop2 so that JVM can pop the result of sqrt from stack at runtime.

Why CANNOT compiler produce the similar bytecode for the first method

say divide i by f and introduce the pop so that JVM can simply remove

the float division result from stack.

So i think i am missing the actual definition of java statement here.

thanks

[852 byte] By [belur_1da] at [2007-11-26 16:32:08]
# 1
> The result of Math.sqrt is not usedSome methods return things that do not need to be used. The compiler does not know how to distinguish between those.
CaptainMorgan08a at 2007-7-8 22:56:44 > top of Java-index,Developer Tools,Java Compiler...
# 2

Hi

>>> "Some methods return things that do not need to be used. The compiler does not know how to distinguish between those."

It can easily know that Math.sqrt actually returns something by definition

but here it is NOT used that is why a pop2 instruction is used....

What i am trying to say here is exactly the above comment i.e compiler

can produce a pop instruction for the former case i/f as it did here...

BECAUSE it knew that Math.sqrt result has to be thrown away

Math.sqrt (..) not assigned to anything also is of same use as is

i/f;

I may be wrong...I just wanted to clarify the definition of java statement

here which in this context confuses me a bit because i tht the compiler

cud have handled it similarly

regards

belur_1da at 2007-7-8 22:56:44 > top of Java-index,Developer Tools,Java Compiler...
# 3

> I just wanted to clarify the

> definition of java statement

> here which in this context confuses me a bit because

> i tht the compiler

> cud have handled it similarly

>

>

> regards

The way I interpret the JLS section 14.8, a line like you posted is not a legal statement. 14.8 lists expressions that can be statements. A method invocation like Math.sqrt() can be a statement. An assignment can be a statement. But something like x/y does not seem to be included.

http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#5984

atmguya at 2007-7-8 22:56:44 > top of Java-index,Developer Tools,Java Compiler...