You are on page 1of 19

Chapter 1

Programming
Languages
Machine Language Assembly Language
High-Level Language
The high-level languages are English-like and easy to learn
and program. For example, the following is a high-level
language statement that computes the area of a circle with
radius 5:
area = 5 * 5 * 3.1415;

Popular High-Level
Languages

COBOL

(COmmon Business Oriented Language)


FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional
Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language developed by
Microsoft)
Delphi (Pascal-like visual language developed by
Borland)
C++ (an object-oriented language, based on C)
Java (We use it in the book)
3

Compiling Source Code


A program written in a high-level language is
called a source program. Since a computer
cannot understand a source program. Program
called a compiler is used to translate the
source program into a machine language
program called an object program. The object
program is often then linked with other
supporting library code before the object can
be executed on the machine.
Source File

Compiler

Object File

Linker

Excutable File

Optional

Characteristics of Java

Java is ObjectOriented
Java is inherently object-oriented.
Java was designed from the start to be objectoriented.
One of the central issues in software
development is how to reuse code.

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!");
}
}

Creating, Compiling,
and Running
Programs
Create/Modify Source Code

Source code (developed by the programmer)

public class Welcome {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Byte code (generated by the compiler for JVM


to read and interpret, not for you to understand)

Method Welcome()
0 aload_0

Method void main(java.lang.String[])


0 getstatic #2
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4
8 return

Saved on the disk

Source Code

Compile Source Code


i.e., javac Welcome.java
If compilation errors
stored on the disk

Bytecode

Run Byteode
i.e., java Welcome

Result
If runtime errors or incorrect result

Compiling and Running


Java from the Command
Window

Set path to JDK bin directory

Set classpath to include the current


directory

set classpath=.

Compile

set path=c:\Program Files\java\jdk1.5.0\bin

javac Welcome.java

Run

java Welcome
8

Anatomy of a Java
Program

Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method

Comments
In Java, comments are preceded by two
slashes (//) in a line, or enclosed between /*
and */ in one or multiple lines. When the
compiler sees //, it ignores all text after // in
the same line. When it sees /*, it scans for
the next */ and ignores any text between /*
and */.

10

Package
Programmers also use packages to organize
classes belonging to the same category or
providing similar functionality.
Java source files can include a package
statement at the top of the file to designate the
package for the classes the source file defines.

Example:
package chapter1;
11

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.

12

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.

13

Statements
A statement represents an action or a
sequence of actions. The statement
System.out.println("Welcome to Java!") ; is a
statement to display the greeting "Welcome
to Java!" Every statement in Java ends with a
semicolon (;).

14

Blocks
A pair of braces in a program forms a block that groups
components of a program.

public class Test {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Class block
Method block

15

Classes
The class is the essential Java construct. A
class is a template or blueprint for objects.
To program in Java, you must understand
classes and be able to write and use them.

16

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 can be used even without
fully understanding the details of how it
works.

17

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;
}
18

The
showMessageDialog
Method
JOptionPane.showMessageDialog(null,
"Welcome to Java!",
Display Message",
JOptionPane.INFORMATION_MESSAGE);

19

You might also like