Java - is a popular programming language, created in
1995.
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
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side
applications such as Java servlets and Java
ServerPages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for
mobile devices such as cell phones.
A Simple Application
Example
//This application program prints Welcome
//to Java!
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Creating and Compiling Programs
On command line
– javac file.java
Anatomy of a Java Program
Comments
Package
Reserved words
Modifiers
Statements
Blocks
identifiers
Classes
Methods
The main method
Comments
Comments are ignored and are treated as white space
They should be written to enhance readability
Explain what a piece of code does (its interface)
Explain any special tricks, limitations, …
Java has three comment formats:
// comment to end of line
/* comment until
closing */
Package
compiles the source code in Welcome.java, generates
Welcome.class, and stores Welcome.class
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. Other
reserved words in Example are public,
static, and void.
Modifiers
Java uses certain reserved words called modifiers
that specify the properties of the data, methods, and
classes and how they can be used. Examples of
modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public
datum, method, or class can be accessed by other
programs. A private datum or method cannot be
accessed by other programs.
Statements
A statement represents an action or a
sequence of actions. The statement
System.out.println("Welcome to Java!")
in the program in Example s a statement
to display the greeting "Welcome to
Java!" Every statement in Java ends
with a semicolon (;).
Blocks
A pair of braces in a program forms a
block that groups components of a
program.
Identifiers: Syntax
Identifiers are the words a programmer uses in a
program
Identifier syntactic rules:
Can be made up of any length of
letters
digits
underscore character (_)
dollar sign ($)
Cannot begin with a digit
Java is case sensitive
User and user are completely different identifiers
Identifiers: Semantics
Identifiers names can come from the following sources
Fixed in Java as reserved words
public, class, static, void, method, …
Chosen by the programmer to denote something
HelloWorld, main, args
Chosen by a programmer whose code we use:
String, System, out, println
Classes
The class is the essential Java
construct. A class is a template or
blueprint for objects.
Methods
What is System.out.println? It is a method: a
collection of statements that performs a sequence of
operations to display a message on the console.
It is used by invoking a statement with a string
argument. The string argument is enclosed within
parentheses. In this case, the argument is "Welcome
to Java!" You can call the same println method with a
different argument to print a different message.
main Method
The main method provides the control of
program flow. The Java interpreter
executes the application by invoking the
main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
References
Introduction to
Java Programming, 4E (Y. Daniel Liang)
Java elements