JAVA BASICS
Mr. K. Madasamy
Associate Professor of Computer Science
C.P.A. College
Bodinayakanur
2
Java’s History
http://www.java.com/en/javahistory/index.jsp
3
Characteristics of Java
www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
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
Creating and Editing Using NotePad
To use NotePad, type
notepad Welcome.java
from the DOS prompt.
6
Creating, Compiling, and Running Programs
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
Compiling and Running Java from the Command Window
Companion Website
9
Anatomy of a Java Program
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
// 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
// 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
// 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
// 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
Programming Style and Documentation
16
Naming Conventions
17
Proper Indentation and Spacing
18
Block Styles
Use end-of-line style for braces.
19
Programming Errors
THANK YOU