Lesson 1: The Java Environment and Our First Program


Introduction

What is Java?

Java is an object-oriented computer programming language

 

What does "object-oriented" mean?

We hear terms like "procedural programming language" and "object-oriented programming language".

Procedural programming is a programming paradigm (or style) where your program is a list of commands performed sequentially or step-by-step.

"Do this / Do That" programming

Historically, a program has been viewed as a logical procedure that takes input data, processes it and produces output data.

Do this Do that

Object-Oriented Programming is a paradigm (or style) where your program is written in such a way that it is modeled as a set of objects with data fields which are manipulated by a set of actions in your program

 

Why program in Java?

It is a "write-once, run-anywhere" programming language, meaning that the code that you write is cross-platform compatible. It can run on a Mac, Windows, Linux, Unix or other operating system / device that has the Java Runtime Environment (JRE). The JRE provides a compatible Java environment from which compiled Java programs can run.

Java Development Kit (JDK) is a special Java environment that not only includes the JRE, but also includes all the necessary tools and environment for compiling Java programs.

Java can be downloaded for free from Oracle at http://www.oracle.com

Programming in Java

1. Create a program using any text editor

HelloWorld.java

2. Compile it using the Java compiler (creates a new file with the .class extension)

% javac HelloWorld.java

The compiler creates a machine readable version of your program. In Java, it is called byte code. Produces another file of the same name, but with the .class file extension.

3. Run (or execute) it by using the Java interpreter

% java HelloWorld

 

editor -> HelloWorld.java -> compiler -> HelloWorld.class -> JVM -> “Hello, World”

public class HelloWorld {
  public static void main(String[] args) {
    System.out.print(“Hello, World”);
    System.out.println();
  }
}

 

Errors

Compile-time errors

Errors that are caught by the system when we compile the program. These are typically syntax errors like misspelled words, missing brackets (or braces), missing semi-colons, etc. The compiler will issue an error message that tries to explain why.

 

Run-time errors

Errors that are caught by the system when we run or execute the program because the program tries to perform an invalid operation.

Example: division by 0.

 

Logical errors

Errors that are caught by the programmer when we execute the program and it produces an incorrect answer, or performs an unintended task.

Example: programming bugs

Input and Output

Inputs to our program provide data that will produce a result.

Simplest way to provide input is through the command line arguments.

public class Hello {
  public static void main(String[] args) {
    System.out.print(“Hello, ”);
    System.out.print(args[0]);
    System.out.println(“. How are you today?”);
  }
}

When the program runs, it reads the command line arguments and prints it back to the screen.

% javac Hello.java
% java Hello Douglas
Hello, Douglas. How are you today?
% java Hello Bob
Hello, Bob. How are you today?

You can use more than one command line argument.

  • args[0] refers to the first argument
  • args[1] refers to the second argument
  • args[2] refers to the third argument

Rules regarding tabs, spaces and newline characters

public class HelloWorld { public static void main (
String[] args) { System.out.println("Hello, World") ; } }

The code above is valude.

But we follow good programming style by using spacing and indenting conventions when we write Java programs (or any programming language).

Similar to the conventions we use when we write poetry.

Rules regarding quotation marks

Characters between the quotation marks are taken literally so that you can specify exactly what gets printed.

If you omit the quotation mark, the compiler will be confused because it needs the quotation marks to distinguish start and end of the string from other parts of the program.

To print a quotation mark literal, a newline or a tab, use:

\”, \n, \t

 

What happens when you omit a bracket (or brace) { } or misspell one of the workds like public or static?

Compile time error will occur when you try to compile your Java code.

 

What is a .java file versus a .class file?

Your Java source code (your program) has the .java extension.

The .class file is the compiled byte code (or machine readable code) that is created when you compile your Java program.