Introduction to Sun Microsystems
java
High level OOPL
Platform Independent
Case Sensitive
-T.Balaji ,
B.E.,M.S.,
Can you make coffee with it?
It was meant to!!
A programming language for appliances!
Doesnt Make Coffee Yet
So Whats Java Good For?
Web applications!
Java Applet
Java Applet
Server
Java on the Web: Java Applets
Clients download applets via Web browser
Browser runs applet in a Java Virtual Machine
(JVM)
Applet
Client
Server
Interactive web, security, and client consistency
Slow to download, inconsistent VMs (besides,
flash won this war)
Java on the Web: J2EE
Thin clients (minimize download)
Java all server side
JSPs
Servlets
Client
Server
EJB
THIS IS WHAT YOULL BE DOING!!
JDBC
The Java programming environment
Compared to C++:
no header files, pointers, etc.
Object-orientation: Classes + Inheritance
Distributed: RMI, Servlet, Distributed object
programming.
Robust: Strong typing + garbage collection
Architecture neutral: architecture neutral
representation
Portable
Java Features
Well defined primitive data types: int, float,
double, char, etc.
int 4 bytes [2,147,648, 2,147,483,647]
Control statements similar to C++: if-thenelse, switch, while, for
Interfaces
Packages
Exceptions
Multi-Threading
Applet model
9
Must Run on Any Architecture
WRITE ONCE, RUN ANYWHERE!
Program
in Java
Java
Compiler
Java Virtual Machine
pretty portable
Java
Bytecode
Java Virtual Machine
10
The Java programming environment
Java programming language specification
Syntax of Java programs
Java byte code: Intermediate representation
for Java programs
Java compiler: Program that Transform Java
programs into Java byte code
Java virtual machine: The whole technology is
based on the concept of JVM. Translator of
byte code into platform specific machine
language. It is the Runtime system that
provides various services to running programs
(Logical representation of JRE)
11
JVM
12
JVM
JVM stands for Java Virtual Machine. Its an abstract
computer or virtual computer which runs the compiled
java
programs.
Actually
JVM
is
a
software
implementation which stands on the top of the real
hardware platform and operating system. It provides
abstraction between the compiled java program and the
hardware and operating system.
JIT: This Compiler converts byte code to native code.
Old Interpreter was replaced with JIT to increase the
performance of java applications.
When JVM compiles the class file it does not compile
the full class file in one shot. Compilation is done on
function basis or file basis. Depending on need basis the
compilation is done. This type of compilation is termed
as JIT or Just-in- Time compilation.
13
How are Java programs written?
Define a class HelloWorld and store it into a
file: HelloWorld.java:
public class HelloWorld {
public static void main (String[] args) {
System.out.println(Hello, World);
}
}
Compile HelloWorld.java
javac HelloWorld.java
Output: HelloWorld.class
Run
java HelloWorld
Output: Hello, World
14
How are variables declared?
Fibonacci:
class Fibonacci {
public static void main(String[] arg) {
int lo = 1;
int hi = 1;
System.out.println(lo);
while (hi < 50) {
System.out.println(hi);
hi = hi + lo;
lo = hi lo;
}
}
}
15
How to define expressions?
Arithmetic: +, -, *,/, %, =
8 + 3 * 2 /4
Use standard precedence and associativity rules
Predicates: ==, !=, >, <, >=, <=
public class Demo {
public static void main (String[] argv) {
boolean b;
b = (2 + 2 == 4);
System.out.println(b);
16
How are simple methods defined?
Every method is defined inside a Java class definition
public class Movie {
public static int movieRating(int s, int a, int d) {
return s+a+d;
}
public class Demo {
public static void main (String argv[]) {
int script = 6, acting = 9, directing = 8;
displayRating(script, acting, directing);
}
public static void displayRating(int s, int a, int d){
System.out.print(The rating of this movie is);
System.out.println(Movie.movieRating(s, a, d));
17
How to extend classes?
Inheritance: mechanism for extending behavior
of classes; leads to construction of hierarchy of
classes [Note: no multiple inheritance]
What happens when class C extends class D:
Inherits
Inherits
Inherits
Inherits
C can:
instance variables
static variables
instance methods
static methods
Add new instance variables
Add new methods (static and dynamic)
Modify methods (only implementation)
Cannot delete anything
18
How to extend classes?
public class Attraction {
public int minutes;
public Attraction() {minutes = 75;}
public int getMinutes() {return minutes;}
public void setMinutes(int d) {minutes = d;}
}
public class Movie extends Attraction {
public int script, acting, directing;
public Movie() {script = 5; acting = 5; directing = 5;}
public Movie(int s, int a, int d) {
script = s; acting = a; directing = d;
}
public int rating() {return script + acting + directing;}
}
public class Symphony extends Attraction {
public int playing, music, conducting;
public Symphony() {playing = music = conducting = 5;}
public Symphony(int p, int m, int c) {
playing = p; music = m; conducting = c;
}
public int rating() {return playing + music + conducting;}
}
19
What are abstract classes?
Abstract class: Merely a place holder for class
definitions; cannot be used to create
public abstract class Attraction {
instances.;
public
public
public
public
public
int minutes;
Attraction() {minutes = 75;}
int getMinutes() {return minutes;}
void setMinutes(int d) {minutes = d;}
abstract void m();
Following is an error:
Attraction x;
x = new Attraction();
Following is not an error:
public class Movie extends Attraction { }
public class Symphony extends Attraction { }
Attraction x;
x = new Movie ();
x = new Symphony();
20
Packages
Object
extends
Attraction
Auxiliaries
Demonstration
extends
Movie
Symphony
How do we organize above classes into a single unit? Put them in
file?
However, only one public class per file (whose name is same as
files)
Solution: Place several files (compilation units) into a package
21