02-22-2010, 04:17 PM
Very nicely written tutorial, thanks for sharing.
I think you got confused here, and its a common mistake either novice Java programmers make, or programmers who dont look at bytecode make. Beleive it or not, void is also return a type! Even though we dont include the keyword 'return' in it, it doesnt mean it doesnt return something. If it didnt return anything, how would it return data? Take a look at the following class and bytecode:
As you can see here,
the printMessage() method actually did return something.
Also, the term 'void' is a return type(which you just learned). And in the sense your reffering it as, its called a method.
I hope you learned something form my own tutorial. ;)
(02-21-2010, 10:43 PM)tsgh mike Wrote: I mentioned before that a method returns a value. Every method returns a value except for a void. Lets go over what they return.
void
No return type.
I think you got confused here, and its a common mistake either novice Java programmers make, or programmers who dont look at bytecode make. Beleive it or not, void is also return a type! Even though we dont include the keyword 'return' in it, it doesnt mean it doesnt return something. If it didnt return anything, how would it return data? Take a look at the following class and bytecode:
Code:
class MethodByteCode {
public static void main (String args[]) {
printMessage();
}
static void printMessage() {
System.out.println ("Hello, world!!!");
}
}
Code:
Compiled from "MethodByteCode.java"
class MethodByteCode extends java.lang.Object{
MethodByteCode();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: invokestatic #2; //Method printMessage:()V
3: return
static void printMessage();
Code:
0: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #4; //String Hello, world!!!
5: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
8: return
}
Press any key to continue . . .
As you can see here,
Code:
8: return
Also, the term 'void' is a return type(which you just learned). And in the sense your reffering it as, its called a method.
I hope you learned something form my own tutorial. ;)