In this lesson, you will learn the tools to get you started within the Java Runtime Environment.
Code is listed in a monospace font, both for code examples and for Java keywords mentioned in the text.
The standard Java convention for names is used:
maxValue).For syntax definitions:
monospace type.| Generic Terms | Substitution Options |
|---|---|
access
|
An access word from: public, protected, private, or it can be omitted |
modifiers
|
one or more terms that modify a declaration; these include the access terms as well as terms like: static, transient, or volatile |
dataType
|
A data type word; this can be a primitive, such as int, or the name of a class, such as Object; variants of this include: returnType and paramType |
variableName
|
The name of a variable; variants on this include paramName and functionName |
ClassName
|
The name of a class; there will be variants of this used for different types of examples, such as: BaseClassName, DerivedClassName, InterfaceName, and ExceptionClassName |
code
|
Executable code goes here |
. . .
|
In an example, omitted code not related to the topic |
Like this Java tutorial? Try our self-paced online Java courses, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
A Java program is run differently than a traditional executable program.
Traditional programs are invoked through the operating system.
Traditional programs are compiled from source code into a machine and OS-specific binary executable file.
When you run a Java program, the OS is actually running the Java Runtime Engine, or JRE, as an executable program; it processes your compiled code through the Java Virtual Machine (usually referred to as the JVM).
A Java source code file is compiled into a bytecode file.
Like this Java tutorial? Try our self-paced online Java course, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
Java source code is written in plain text, using a text editor.
.java
extension.The javac compiler is then used to compile the source code into bytecode.
javac MyClass.java
.class
You then run the java runtime engine, which will then interpret the bytecode to execute the program.
java MyClass
java.main method for.
Like this Java tutorial? Try our self-paced online Java course, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
You can download the SDK (software development kit) including the compiler and runtime engine from Oracle at: http://www.oracle.com/technetwork/java/javase/downloads/index.html . Look for the download of JavaSE 1.6.0.10 (or the latest release of that version).
You can also download the API documentation and even the source code. Note that:
Like this Java tutorial? Try our self-paced online Java course, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
Java programs are compiled and run from an operating system prompt, unless you have installed an IDE that will do this for you directly. If you create an applet, this would run inside a web page in a browser.
After you have installed the JDK, you will need to set at least one environment variable in order to be able to compile and run Java programs.
For more complex projects that pull together elements from different sources, you must set an additional environment variable or two. Note that:
PATH environment variable enables the operating system to find the JDK executables when your working directory is not the JDK's binary directory.CLASSPATH is Java's analog to PATH, the compiler and JVM use it to locate Java classes. JAVA_HOME environment variable. bin and lib).PATH is then set from JAVA_HOME plus \bin.PATH from JAVA_HOMEThe procedure to permanently set the environment variables varies slightly from one version of Windows to another; the following will work in many, including Windows XP. The process for Vista is similar, but slightly different:
JAVA_HOME or PATH already exist.JAVA_HOME exists, check to see that it matches your most recent JDK (or the one you wish to use). JAVA_HOME.C:\Program Files\Java\jdk1.5.0_14. "C:\Program Files\Java\jdk1.5.0_14".C:\Progra~1\Java\jdk1.5.0_14 (you can check if this works in your system by typing it into the address bar of a My Computer window). 
PATH, again select either Add or Edit.PATH, it will work as a User variable.JAVA_HOME, then we are already set correctly .PATH , it will be found first, and therefore supercede any other directory - if you append to path, your directory won't be found if an earlier entry JDK entry exists (the following image shows a prepend).
If you set the variables from a command prompt, they will only hold for that session, but you could create a batch file that you could run each time you open a command prompt window.
To set the PATH from a command prompt or batch file:
set PATH=C:\Progra~1\Java\jdk1.6.0_10\bin;%PATH%
If you need to set the CLASSPATH:
set CLASSPATH=.;%CLASSPATH%
CLASSPATH; this is no longer necessary .$PATH and $CLASSPATH instead of %PATH% and %CLASSPATH%, and that the path element separator is a colon instead of a semicolon. Like this Java tutorial? Try our self-paced online Java courses, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
main() MethodIn order to run as a program, a class must contain a method named main, with a particular argument list. This is similar to the C and C++ languages.
The definition goes inside your class definition, and looks like:
public static void main(String[] args) {
(code goes here)
}
public, because it will be called from outside your class (by the JVM).static keyword defines an element (could be data or functional) that will exist regardless of whether an object of a class has been instantiated. In other words, declaring main as static allows the JVM to call the method and therefore execute the program. That doesn't mean that an object of that class cannot be instantiated, it is just not required.String[] args parameter list states that there will be an array of String objects given to the method - these are the command line arguments.java ClassNameFor example, if we had an executable class called
Hello, in a file called
Hello.java
that compiled to Hello.class, you could run it with:
java Hello
Like this Java tutorial? Try our self-paced online Java course, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
System.out.println()
In order to see something happen, we need to be able to print to the screen.
There is a System class that is automatically available when your program runs (everything in it is static).
stdin, stdout, and stderr (standard output, standard input, and standard error).System.out is a static reference to the standard output stream.
As an object, System.out contains a println(String) method
that accepts a String object, and prints that text to the
screen, ending with a newline (linefeed).
print(String) method that does not place a newline at the end.You can print a String directly, or you can build one from pieces.
String will automatically be created from a quote-delimited series of characters.String by using the + sign; if one item is a String or quote-delimited series of characters, then the rest can be any other type of data.Like this Java tutorial? Try our self-paced online Java courses, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
Sun provides extensive documentation of the API (library of available classes). The documentation for version 6 is available at http://download.oracle.com/javase/6/docs/api/
If you view that page, you will see a list of classes on the left as hyperlinks. Clicking a class name will bring up the documentation for that class on the right.
For example, click on System. The page on the
right contains documentation on the elements of the class, categorized
by type of element (there are links at the top for fields, constructors,
methods, etc.).
Try to locate the out field - note that
the field types, parameter types and return types are hyperlinked,
and that out is a PrintStream. Click that, and
find the println methods in a table with short descriptions.
Select one to see the detailed description. The multiple versions are
an example of method
overloading,
which we will cover in an upcoming lesson. Another lesson will cover
documenting your own classes with the same tool that created this documentation.
In this lesson, you have learned:
Like this Java tutorial? Try our self-paced online Java course, which includes videos and exercises in addition to the content in this Java tutorial. Not sure if you want to pay for that? Register for a free demo of the course.
This page was last updated on 2013-01-03
All pages and graphics in this Java Tutorial is copyright 2013 and are the property of Webucator, Inc. unless otherwise specified. The purpose of this website is to help you learn Java on your own and use of the website implies your agreement to our Terms of Service.