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, such as C or C++ the source code, or codethe thatinstructions iswhich are written by the developer, is compiled by a compiler into an object file, which is then linked with other object files to create an executable file. TheThis file is in machine language, and is intended for a single operating system/processor combination,. so This means that the developer would have to compile the program separately for separate operating system/processor combinations.
 
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>
<pre> a7 f4 73 5a 1b 92 7d</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 something called the Java Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. ItAt traversesexecution time the JVM translates the compiled bytecode andinto runsmachine itlanguage which can then be executed by the hardware. There are different versions of the JVM thatwhich are compatible with eachvarious operating systemsystems, and can runallowing the same code. Thereto isbe virtuallyexecuted noon different platforms. This makes little difference forto the end-user, buthowever thisit makesgreatly itreduces athe loteffort easierneeded forto develop cross-platform applications on the part of the developersdeveloper.
 
== Installing the Java Development Kit ==