Learning Java/Introduction to Java: Difference between revisions
Content deleted Content added
→The Java Platform: Corrected misinformation regarding bytecode. Provided example code. |
|||
Line 13:
One thing that distinguished Java from some other languages is its ability to run the same compiled code across multiple operating systems.
In these other languages
Java is different in that it does not compile the code into machine language code. Compilation creates bytecode out of the source code. Source code is written and stored in files with the extension of .java, and after compilation a file with a ".class" extension is created which contains the bytecode instructions. Bytecode generally looks something like this:
<pre>
public class UserDetails extends java.lang.Object{
When the code is run by the user, it is processed by something called the Java Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. It traverses the bytecode and runs it. There are different versions of the JVM that are compatible with each operating system and can run the same code. There is virtually no difference for the end-user, but this makes it a lot easier for developers.▼
public UserDetails();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public UserDetails(java.lang.String, java.lang.String, java.lang.String);
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: aload_1
6: putfield #2; //Field userName:Ljava/lang/String;
9: aload_0
10: aload_2
11: putfield #3; //Field password:Ljava/lang/String;
14: aload_0
15: aload_3
16: putfield #4; //Field email:Ljava/lang/String;
19: return
public UserDetails(java.lang.String, java.lang.String, java.lang.String, java.lang.String);
Code:
0: aload_0
....
}
</pre>
To view the bytecode of a compiled Java program one can execute the following command at the command-line:
<pre>
javap -c NameOfClassFile
</pre>
▲When the code is run by the user, it is processed by
== Installing the Java Development Kit ==
|