Lecture 1 - Introduction to Java
Lecture 1 - Introduction to Java
13
Dept of CSE, University of Dhaka 13
Phase 1: Creating a Program
Any text editor or Java IDE (Integrated
Development Environment) can be used to
develop Java programs.
Java source-code file names must end with
the .java extension.
Some popular Java IDEs are NetBeans, jEdit,
Eclipse, JBuilder, JCreator etc.
14
Dept of CSE, University of Dhaka 14
Phase 2: Compiling a Java Program into Bytecodes
javac Welcome.java
Compiles the source file “Welcome.java” (and other files if
necessary), transforms the Java source code into bytecodes and
places the bytecodes in a file named “Welcome.class”.
It searches the file “Welcome.java” in the current directory and in
the directories listed in the PATH environment variable.
Bytecodes
They are not machine language binary code.
They are independent of any particular microprocessor or
hardware platform.
They are platform-independent instructions.
Another entity or interpreter is required to convert the
bytecodes into machine codes that the underlying microprocessor
understands.
This is the job of the JVM (Java Virtual Machine)
15
Dept of CSE, University of Dhaka 15
JVM (Java Virtual Machine)
It is a part of the JDK and the foundation of the Java
platform.
It can be installed separately or with JDK.
16
Dept of CSE, University of Dhaka 16
JVM (Java Virtual Machine) (contd.)
It is the JVM that makes Java a portable language.
The same bytecodes can be executed on any platform
containing a compatible JVM.
JVM is available for Windows, Unix, Linux and Solaris.
The JVM is invoked by the “java” command.
java Welcome
It searches the class “Welcome” in the current directory and in the
directories listed in the CLASSPATH environment variable and
executes the “main” method of class “Welcome”.
It issues an error if it cannot find the class “Welcome” or if class
“Welcome” does not contain a method called “main” with proper
signature (more on this will be discussed later)
17
Dept of CSE, University of Dhaka 17
Phase 3: Loading a Program into Memory
One of the components of the JVM is the class
loader.
The class loader takes the .class files containing
the programs bytecodes and transfers them to
primary memory (RAM).
The class loader also loads any of the .class files
provided by Java that our program uses.
The .class files can be loaded from a disk on our
system or over a network (another PC in our
LAN, or the Internet).
18
Dept of CSE, University of Dhaka 18
Phase 4: Bytecode Verification
Another component of the JVM is the bytecode
verifier.
Its job is to ensure that bytecodes are valid and
do not violate Java’s security restrictions.
This feature helps to prevent Java programs
arriving over the network from damaging our
system.
Another contributor for making Java programs
slow.
19
Dept of CSE, University of Dhaka 19
Phase 5: Execution
Now the actual execution of the program begins.
20
Dept of CSE, University of Dhaka 20
Identifiers, Keyword, Statements
21
Dept of CSE, University of Dhaka 21
Identifiers
Identifiers are names for variables, classes, methods etc.
Good ones are compact, but indicate what they stand for
radius, width, height, length
Rules:
May contain upper case, lower case letters, numbers, underscore, dollar sign.
Must not begin with a number.
22
Dept of CSE, University of Dhaka 22
Keywords
Some words are reserved, and can’t be used as
identifiers
23
Dept of CSE, University of Dhaka 23
Capitalization
Case matters!
24
Dept of CSE, University of Dhaka 24
Statements
25
Dept of CSE, University of Dhaka 25
Data types
26
Dept of CSE, University of Dhaka 26
Data Types
Java is a “strong typed language”
Each variable has a declared type.
27
Dept of CSE, University of Dhaka 27
Java Primitive Types
28
Dept of CSE, University of Dhaka 28
Java Primitive Types (Cont.)
Other types:
boolean Either true or false.
29
Dept of CSE, University of Dhaka 29
Primitive variable types
30
Dept of CSE, University of Dhaka 30
Thank you