Lect-3 Introduction To JAVA - 1
Lect-3 Introduction To JAVA - 1
int x;
X
…
X
x = 5; 5
Reference variables
(in Java)
Button x(“Click”);
“click”
Pointers (in C++)
• Variables can be explicitly declared as
pointers to objects
Button *x;
X
…
Button Object
x = new Button(“click”); X
“click”
Disposing of allocated memory
• In Java, garbage collection is automatic
– Memory allocated to objects are reclaimed
when no variables refer to them
– Need to set reference variables to null when
the object is no longer needed
• No pointer • Pointer
• No multiple • Multiple inheritance
inheritance • Manual garbage
• Automatic garbage collection
collection • Operator
• No operator overloading
overloading • goto statement and
• No goto statement structure and union
and no structure and data structure
union data structure
Why Java ?
www.cs.armstrong.edu/liang/intro6e/JavaCharacteristics.pdf
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
Java is partially modeled on C++, but
• Java Is Interpreted
greatly simplified and improved. It is like
• Java Is Robust
C++ but with more functionality and fewer
• Java Is Secure negative aspects
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted Java is inherently object-oriented. It
• Java Is Robust was designed from the start to be
• Java Is Secure object-oriented.
• Java Is Architecture-Neutral
One of the central issues in software
• Java Is Portable
development is how to reuse code.
• Java's Performance Object-oriented programming provides
• Java Is Multithreaded great flexibility, modularity, clarity, and
• Java Is Dynamic reusability through encapsulation,
inheritance, and polymorphism.
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
Distributed computing involves several
• Java Is Robust
computers working together on a
• Java Is Secure network.
• Java Is Architecture-Neutral
• Java Is Portable Java is designed to make distributed
• Java's Performance computing (e.g. Web Services) easy.
• Java Is Multithreaded Since networking capability is
• Java Is Dynamic inherently integrated into Java, writing
network programs is like sending and
receiving data to and from a file
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
You need an interpreter to run Java
• Java Is Secure programs.
• Java Is Architecture-Neutral
• Java Is Portable The programs are compiled into
• Java's Performance bytecode. The bytecode is machine-
• Java Is Multithreaded independent and can run on any
• Java Is Dynamic machine that has a Java interpreter,
which is part of the JVM
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
Java compiler can detect many
• Java Is Secure problems that would first show up at
• Java Is Architecture-Neutral execution time in other languages
• Java Is Portable
• Java's Performance Java has eliminated certain types of
• Java Is Multithreaded error-prone programming constructs
• Java Is Dynamic found in other languages (default
value, memory leak)
Compiler creates
Phase 2 Compiler Disk bytecodes and stores
them on disk in a file
ending with .class.
Primary
Memory
Phase 3 Class Loader Class loader reads
.class files
containing
bytecodes from disk
and puts those
Disk bytecodes in
. .. memory.
..
.
Primary
Memory
Phase 4 Bytecode Verifier Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
Java’s security
restrictions.
. ..
..
.
Primary
Memory Interpreter reads
Phase 5 Interpreter bytecodes and
translates them into a
language that the
computer can
understand, possibly
storing data values as
the program executes.
. ..
..
.
Java Environment
1. JDK:
• Appletviewer ( for viewing applets)
• Javac (Compiler)
• Java (Interpreter)
• Javah (for C header files)
• Javadoc ( for creating HTML description)
Java Environment
2. Application Package Interface (API)
Contains hundreds of classes and methods grouped into several
functional packages:
• Language Support Package
• Utility Packages (random num. gen., sys. date manipulation)
• Input/Output Packages
• Networking Packages (implementing networking app. )
• AWT Package (classes for painting graphics and images)
• Applet Package (web page using java)
The Evolution of Java
When it sees /*, it scans for the next */ and ignores any
text between /* and */
Example
/* Traditional "Hello World!" program. */
// package pack1;
// import java.lang.System;
class prog1
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Package Statement
• Javac command compiles the source code prog1.java then,
generates prog1.class and store it under a directory which
is
called as name of the package
• package statement if used must be the first statement in a
compilation unit. Its syntax is:
package packageName;
❑ For example:
package pack1;
Example
/* Traditional "Hello World!" program. */
// package pack1;
// import java.lang.System;
class prog1
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Import Statement
// package pack1;
// import java.lang.System;
class prog1
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Classes
• Class declarations contain a keyword class and an
identifier
• Class members are enclosed within braces. The
syntax of defining a class is shown below:
class prog1
{ // program code
}
Example
/* Traditional "Hello World!" program. */
// package pack1;
// import java.lang.System;
class prog1
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Main method
• To execute a class, it must contain a valid main
method
// package pack1;
// import java.lang.System;
class prog1
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
System class
System.out.println("Hello World!");
x = in.nextInt();
y = in.nextInt();
z = x + y;
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block