Sei sulla pagina 1di 34

Objectives

 Key Features of Java Technology


 Define class and application
 Writing a simple Java Application
 Java Virtual Machine’s function
 Garbage Collection
 Code Security
What is the Java Technology?
 A programming Language
 A Development environment
 An Application environment
 A deployment environment
Primary Goals of Java
Technology
 Provides an easy to use language by
 Avoiding the pitfalls of other languages such as
pointers, memory management etc
 Object Oriented
 Enable users to create streamlined and clear code
 Provides an interpreted environment
 Speed of development
 Code Portability
Primary Goals (contd..)

 Enables users to run more than one thread


 Loads classes Dynamically
 Furnishes better security
 The following features fulfill these goals
 The Java Virtual Machine
 Garbage Collection
 Code Security
A Basic Java Application

// TestGreeting.java
// Sample "Hello World" Application
public class TestGreeting{
public static void main(String[] args) {
Greeting hello = new Greeting("Hello");
hello.greet("World");
}
}
A Basic Java Application-Contd..
//Greeting.java
public class Greeting{
private String salutation;
Greeting(String s){
salutation = s;
}
public void greet(String whom){
System.out.println(salutation+” “+whom);
}
}
Compiling and Running

 Compiling TestGreeting.java
javac TestGreeting.java
 Greeting.java is compiled automatically
 Running an application
Java TestGeeting
The Java Virtual Machine

 Provides hardware platform specifications


 Reads compiled byte codes that are platform
independent
 Is implemented as software or hardware
 Is implemented in a Java technology
development tool or a web browser
TestGreeting.java
Compile
Greeting.java
javac
Also compiles

TestGreeting.class Greeting.class

java JVM
Runtime

Can run on multiple platforms

Unix DOS JavaOS


JVM JVM
Garbage Collection

 Java Provides a system level thread to track


memory allocation
 Garbage Collection
 Checks for and frees memory no longer needed
 Is done automatically
 Can vary across JVM implementations
Code Security
Compile Runtime

Class Loader

TestGreeting.java Networ Byte code verifier


javac k
Interpreter
JIT Code
Generator
TestGreeting.class Runtime

Hardware
Java Runtime Environment

 JVM Performs 3 main tasks


 Loads Code – Performed by class loader
 Verifies Code – Performed by bytecode verifier
 Executes Code – Performed by runtime
interpreter
Class Loader

 Loads all classes necessary for the execution


of a program
 Maintain classes of the local file system in
separate “namespace”
 Prevents spoofing
Bytecode Verifier

 Ensures that
 The code adheres to the JVM specification
 The code does not violate system integrity
 The code causes no operand stack overflows or
underflows
 The parameter types for al operational code are
correct
 No illegal data conversions have occurred
Object Oriented Programming
Objectives

 Define modeling concepts


 Abstraction, encapsulation and Packages
 Define class, member, attribute, method,
constructor and package
 Use access modifiers private and
public
 Invoke a method on a particular Object
Abstraction

 Functions – Write an algorithm once to be


used in many situations
 Objects – Group a related set of attributes
and behaviors into a class
 Frameworks and API’s – Large group of
objects that support a complex activity
Classes as Blueprints for Objects

 A blueprint is a description from which


many physical devices are constructed
 A class is a description of an object
 A class describes the data each object includes
 A class describes the behaviors that each object
exhibits
 Classes support 3 key features of OOP
•Encapsulation •Inheritance •Polymorphism
Declaring Java Classes

 Example
public class Vehicle{
private double maxLoad;
public void setMaxLoad(double val)
{
maxLoad=val;
}
}
Declaring Attributes

 Example
public class Foo{
public int x;
private float y = 1000.0F;
private String name=“Fred
Smith”;
}
Declaring Methods

 Examples
public class Thing{
private int x;
public int getX(){
return x;
}
public void setX(int new_x){
x=new_x;
}
}
Accessing Object Members

 The “dot” notation <object>.<member> is


used to access object members including
attributes and methods
 Example
thing1.setX(47);
thing1.x=47;
Information Hiding

The Problem
MyDate Client code has direct access to
internal data
+day : int
MyDate d = new MyDate();
+month : int
+year : int d.day=32; //invalid day
d.month=2;d.day=30;//wrong
d.day=d.day+1; //no check
Information Hiding
 The Solution:
MyDate Client code must use setters /
-day: int getters to create internal data
-month: int MyDate d = new MyDate();
-year: int d.setDay(32);
+getDay(): int //invalid returns false
+getMonth(): int
d.setMonth(2);d.setDay(30);
+getYear(): int
//setDay returns false
+setDay(d: int): boolean
+setMonth(m: int) d.setDay(d.getDay()+1);
+setYear(y: int) //will return false if wrap
occurs
-validDay(d: int): boolean
Encapsulation

MyDate  Hides implementation


-date: long details
 Forces the user to use an
+getDay(): int interface to access data
+getMonth(): int
+getYear(): int  Makes the code more
+setDay(d: int) maintainable
+setMonth(m: int)
+setYear(y: int)
-validDay(d: int): boolean
Declaring Constructors

 Example
public class Thing{
private int x;
public Thing(){
x=47;
}
public Thing(int new_x){
x=new_x;
}
}
The Default Constructor

 There is always at least one constructor in


every class
 If no constructors are provided a default
constructor will be present
 The default constructor takes no arguments
 The default constructor has no body
 Enables you to create object instances
without having to write a constructor
Source File Layout

 Example
package shipping.reports.web;
import shipping.domain.*;
import java.util.List;
import java.io.*;
public class VehicleCapacityRepport{
private List vehicles;
public void generateReport(Writer op){
---
}
}
Packages

 Packages help manage large Software Systems


 Packages can contain classes and sub-packages
shipping

domain
GUI
Company Vehicle

Truck RiverBarge
reports
The package Statement

 Example
package shipping.reports.web;
 Specify the package declaration at the beginning of
the source file
 Only one package declaration per source file
 If no package declaration is declared, the class
belongs to the default package
 Package names must be hierarchical and separated
by dots
The import Statement

 Precedes all class declarations


 Tells the compiler where to find classes to
use
 Examples
import java.io.Writer; //Specify a class to access

import java.io.*; //all classes in the package


Directory Layout and Packages
 Packages are stored in the Directory tree containing the
package name
 Example

shipping/
domain/
Company.class
Vehicle.class
RiverBarge.class
Truck.class
GUI/
reports/
VehicleCapacityReport.class
Terminology Recap

 Class – The source-code blueprint for a run-time object


 Object – An instance of a class
 Attribute – A data element of an object
AKA: data member, instance variable, data field
 Method – A behavioral element of an object
AKA: algorithm, function, procedure
 Constructor – A method-like construct used to initialize
an object
 Package – A group of classes and/or sub-packages

Potrebbero piacerti anche