Resources‎ > ‎Java Basics‎ > ‎

Java and Syntax Overview

The Java programming language is an object-oriented language designed to be portable.  Unlike languages like C, Java programs follow a "write once, run anywhere" model.  You write the program one time, compile it to byte code, and run it on any computer that has a Java Virtual Machine (JVM) available.

Implementation and Compilation

Writing and running a Java program involves three steps.
  1. The programmer writes Java code and stores that code in files ending with the extension .java.
    1. Open an editor of your choice and write your code.  Class X must be stored in file
  2. The Java code is compiled into Java byte code.  The byte code for the code stored in file is stored in file X.class.
    1. From the command line, navigate to the directory where your Java file is stored and type javac
    2. If there are errors in your program, the compiler will tell you.  If there are no errors, a .class file will be created.
  3. The Java Virtual Machine (JVM) is launched.  The JVM reads and interprets the byte code stored in the specified class files.  The result is a running program.
    1. From the command line, navigate to the directory where your Java file is stored and type java classname
    2. Note that you do not include an extension after the class name.
For more information see the following:

Syntax Highlights

  • Java programs consist of one or more classes.  A class X is stored in a file 
  • There are two ways to comment your code in Java.  The first option is to use //.  A // will comment every from the the // to the end of the line.
    //declare a variable a
    int a;

    a = 4; //initialize a to the value 4
    The second option is to use /* and */.  These symbols allow for multiline comments.  Everything from the /* until the */ is designated as a comment.
     * Declare a variable a
     * initialize
    its value to 4.
    int a = 4;
    Java also provides a special tool called javadoc that translates comments that are in a specific format into HTML pages. 
  • Statements end with a semi-colon (;). 
            int a = 4;
  • Code blocks are surrounded by curly braces ({ }).  This applies to if statements, loops, classes, and methods.
class X {
    //class definition

if (condition) {
    //body of if

while (condition) {
    //body of while

void method() {
    //body of method