Why create a Wikiversity account?
Learning Java/Introduction to Java
< Learning Java
1What is Java?
2History of Java
3The Java Platform
4Installing the Java Development Kit
4.1Downloading and Installing
4.2Note on Editions
4.3Configuring Variables
4.3.1For Windows Users
4.3.2For Linux and UNIX
4.3.3For Macintosh
5"Hello World"
6External links
What is Java?​[​edit source]
Java is an Object-Oriented, cross platform, multi-purpose programming language produced by Sun Microsystems. First released in 1995, it was developed to be a machine independent web technology. It was based on C and C++ syntax to make it easy for programmers from those communities to learn. Since then, it has earned a prominent place in the world of computer programming.
Java has many distinct characteristics that have contributed to its popularity:
History of Java​[​edit source]
See History of Java article section at Wikipedia
The Java Platform​[​edit source]
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 the instructions which 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. This file is in machine language, and is intended for a single operating system/processor combination. 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:
public class UserDetails extends java.lang.Object{ 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 .... }
To view the bytecode of a compiled Java program one can execute the following command at the command-line:
javap -c NameOfClassFile
where NameOfClassFile is the file ending in ".class" which was generated during compile time.
When the code is run by the user, it is processed by Java Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. At execution time the JVM translates the compiled bytecode into machine language which can then be executed by the hardware. There are different versions of the JVM which are compatible with various operating systems, allowing the same code to be executed on different platforms. This makes little difference to the end-user, however it greatly reduces the effort needed to develop cross-platform applications on the part of the developer.
Installing the Java Development Kit[edit source]
The Java Development Kit (JDK) contains the core libraries and compiler required to develop Java, and it is distributed by Sun. The JDK should not be confused with the JRE (Java Runtime Environment). The JRE is a virtual machine for running, as opposed to compiling, Java programs.
Downloading and Installing​[​edit source]
To download the JDK, go to http://java.sun.com/javase/downloads/index.jsp​. Click on "JDK 6 Update 3 with NetBeans 6.0". Follow the instructions for downloading the JDK installation file. If you are running Windows, simply run the executable file and follow the installation instructions. If you are using UNIX, Solaris or Linux, it might take a little more work.
If you are using a Macintosh, the latest available JDK is automatically installed by the operating system. Because Java on the Macintosh is developed and maintained by Apple, in coordination with Sun, the current version on the Macintosh may not be the current version that is available from Sun.
Note on Editions​[​edit source]
The JDK comes in three editions.
Now the '2' has been dropped from the name and is generally referred to as Java SE/EE/ME. Also JDK 1.5 is JDK 5 and so on.
Configuring Variables​[​edit source]
Before writing code, it is recommended that you set the Path variable on your system so you can compile your code more easily.
For Windows Users​[​edit source]
For Linux and UNIX[edit source]
One way to set your path in Linux/Unix is to add a path export to your bash profile.
In order to do this, first open your bash profile in a text editor. For example,
pico ~/.bash_profile
Then add this line:
export PATH=$PATH:/usr/local/jdk/bin
Note that the path to the java directory "/usr/local/jdk/bin" may be different on your machine.
Restart your shell.
For Macintosh​[​edit source]
Apple sets everything up for you. Sit back and relax.
The only drawback is that because Apple handles development and maintenance of Java on the Mac, there is usually a delay from the time that a new version is released by Sun and the time that the new version is released on the Mac. Also, getting the latest version sometimes requires an operating system upgrade.
Oh well, you can't have everything.
"Hello World"​[​edit source]
Anytime you learn a computer programming language, it is tradition that the first program you write should be to make your computer say, "Hello World". This is a small feat, but is a good opportunity to make sure that you installed the JDK properly.
The Java compiler reads basic text files. Open up a text editor like Notepad (do not use a complex program like Microsoft Word for this). Type this code, remembering that Java is case-sensitive:
public class HelloWorld { public static void main (String[] args) { System​.​out​.​println​(​"Hello World!"); }}
Here's the JDK 1.5 or later version of Hello World
public class HelloWorld { public static void main​(​String​... args) { System​.​out​.​println​(​"​Hello World!"); }}
Save this file as HelloWorld.java. Start your system's command line and navigate to the folder that you saved HelloWorld.java to. Type javac HelloWorld.java​. This runs the java compiler, javac, and creates a class file, which contains the bytecode for the application. Next type java HelloWorld​. This runs the class file that was just created by the compiler. Your console should print:
Hello World!

Congratulations - you finished reading Intro to Java. Now, you can move on to Basic Java Language

Project: Topic:Java
Previous: Topic:Java — Learning Java/Introduction to Java — Next: Basic Java Language

External links​[​edit source]
Introduction to Java - December 2006
Categories: IntroductionsLearning Java
This page was last edited on 19 April 2020, at 19:27.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.
Privacy policy
About Wikiversity
Mobile view
Cookie statement
TalkContributionsCreate accountLog in
ReadEditEdit sourceView history
Visit the main pageMain PageBrowseRecent changesGuided toursRandomHelpDonatePortalColloquiumNewsProjectsSandboxHelp deskWhat links hereRelated changesSpecial pagesPermanent linkPage informationCite this pageCommonsWikibooksWikidataWikinewsWikipediaWikiquoteWikisourceWikispeciesWikivoyageWiktionaryMeta-WikiOutreachMediaWikiWikimaniaCreate a bookDownload as PDFPrintable versionAdd links