1 of 20

JAVA BASICS

Mr. K. Madasamy

Associate Professor of Computer Science

C.P.A. College

Bodinayakanur

2 of 20

2

Java’s History

  • James Gosling and Sun Microsystems
  • Oak
  • Java, May 20, 1995, Sun World
  • HotJava
    • The first Java-enabled Web browser
  • Early History Website:

http://www.java.com/en/javahistory/index.jsp

3 of 20

3

Characteristics of Java

  • Java Is Simple
  • Java Is Object-Oriented
  • Java Is Distributed
  • Java Is Interpreted
  • Java Is Robust
  • Java Is Secure
  • Java Is Architecture-Neutral
  • Java Is Portable
  • Java's Performance
  • Java Is Multithreaded
  • Java Is Dynamic

www.cs.armstrong.edu/liang/JavaCharacteristics.pdf

4 of 20

4

A Simple Java Program

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

5 of 20

5

Creating and Editing Using NotePad

To use NotePad, type

notepad Welcome.java

from the DOS prompt.

6 of 20

6

Creating, Compiling, and Running Programs

7 of 20

7

Compiling Java Source Code

You can port a source program to any machine with appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown below. Java Virtual Machine is a software that interprets Java bytecode.

8 of 20

8

Compiling and Running Java from the Command Window

  • Set path to JDK bin directory
    • set path=c:\Program Files\java\jdk1.8.0\bin
  • Set classpath to include the current directory
    • set classpath=.
  • Compile
    • javac Welcome.java
  • Run
    • java Welcome

Companion Website

9 of 20

9

Anatomy of a Java Program

  • Class name
  • Main method
  • Statements
  • Statement terminator
  • Reserved words
  • Comments
  • Blocks

10 of 20

10

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Class Name

Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.

11 of 20

11

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Main Method

Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main method.

12 of 20

12

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Statement

A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!“.

13 of 20

13

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Statement Terminator

Every statement in Java ends with a semicolon (;).

14 of 20

14

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

Reserved words

Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class.

15 of 20

15

Programming Style and Documentation

  • Appropriate Comments
  • Naming Conventions
  • Proper Indentation and Spacing Lines
  • Block Styles

16 of 20

16

Naming Conventions

  • Choose meaningful and descriptive names.
  • Class names:
    • Capitalize the first letter of each word in the name. For example, the class name ComputeExpression.

17 of 20

17

Proper Indentation and Spacing

  • Indentation
    • Indent two spaces.

  • Spacing
    • Use blank line to separate segments of the code.

18 of 20

18

Block Styles

Use end-of-line style for braces.

 

19 of 20

19

Programming Errors

  • Syntax Errors
    • Detected by the compiler
  • Runtime Errors
    • Causes the program to abort
  • Logic Errors
    • Produces incorrect result

20 of 20

THANK YOU