Top Banner
Objectives Key Features of Java Technology Define class and application Writing a simple Java Application Java Virtual Machine’s function Garbage Collection Code Security
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BasicJava1

Objectives

Key Features of Java Technology Define class and application Writing a simple Java Application Java Virtual Machine’s function Garbage Collection Code Security

Page 2: BasicJava1

What is the Java Technology?

A programming Language A Development environment An Application environment A deployment environment

Page 3: BasicJava1

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 developmentCode Portability

Page 4: BasicJava1

Primary Goals (contd..)

Enables users to run more than one thread Loads classes Dynamically Furnishes better security

Page 5: BasicJava1

The following features fulfill these goals The Java Virtual Machine Garbage Collection Code Security

Page 6: BasicJava1

// 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

Page 7: BasicJava1

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);

}

}

Page 8: BasicJava1

Compiling and Running

Compiling TestGreeting.javajavac TestGreeting.java

Greeting.java is compiled automatically Running an application

Java TestGeeting

Page 9: BasicJava1

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

Page 10: BasicJava1

JVM

Unix DOS JavaOS

TestGreeting.java Greeting.java

TestGreeting.class Greeting.class

javacAlso compiles

JVM JVM

Can run on multiple platforms

java

Com

pil

eR

un

tim

e

Page 11: BasicJava1

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

Page 12: BasicJava1

Code Security

TestGreeting.java

TestGreeting.class

javac

Class Loader

Byte code verifier

Interpreter

Runtime

JIT Code

Generator

Hardware

Network

Network

Compile Runtime

Page 13: BasicJava1

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

Page 14: BasicJava1

Class Loader

Loads all classes necessary for the execution of a program

Maintain classes of the local file system in separate “namespace”

Prevents spoofing

Page 15: BasicJava1

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

Page 16: BasicJava1

Object Oriented Programming

Page 17: BasicJava1

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

Page 18: BasicJava1

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

Page 19: BasicJava1

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

Page 20: BasicJava1

Declaring Java Classes

Examplepublic class Vehicle{

private double maxLoad;

public void setMaxLoad(double val)

{

maxLoad=val;

}

}

Page 21: BasicJava1

Declaring Attributes

Examplepublic class Foo{

public int x;

private float y = 1000.0F;

private String name=“Fred Smith”;

}

Page 22: BasicJava1

Declaring Methods

Examplespublic class Thing{private int x;public int getX(){

return x;}public void setX(int new_x){

x=new_x;}

}

Page 23: BasicJava1

Accessing Object Members

The “dot” notation <object>.<member> is used to access object members including attributes and methods

Examplething1.setX(47);

thing1.x=47;

Page 24: BasicJava1

Information Hiding

The Problem

MyDate

+day : int+month : int+year : int

Client code has direct access to internal dataMyDate d = new MyDate();

d.day=32; //invalid day

d.month=2;d.day=30;//wrong

d.day=d.day+1; //no check

Page 25: BasicJava1

Information Hiding The Solution:

MyDate

-day: int-month: int-year: int+getDay(): int +getMonth(): int+getYear(): int+setDay(d: int): boolean+setMonth(m: int)+setYear(y: int)-validDay(d: int): boolean

Client code must use setters / getters to create internal data

MyDate d = new MyDate();

d.setDay(32);

//invalid returns false

d.setMonth(2);d.setDay(30);

//setDay returns false

d.setDay(d.getDay()+1);

//will return false if wrap occurs

Page 26: BasicJava1

Encapsulation

Hides implementation details

Forces the user to use an interface to access data

Makes the code more maintainable

MyDate

-date: long

+getDay(): int +getMonth(): int+getYear(): int+setDay(d: int)+setMonth(m: int)+setYear(y: int)-validDay(d: int): boolean

Page 27: BasicJava1

Declaring Constructors

Examplepublic class Thing{

private int x;

public Thing(){

x=47;

}

public Thing(int new_x){

x=new_x;

}

}

Page 28: BasicJava1

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

Page 29: BasicJava1

Source File Layout

Examplepackage shipping.reports.web;import shipping.domain.*;import java.util.List;import java.io.*;public class VehicleCapacityRepport{private List vehicles;public void generateReport(Writer op){

---}

}

Page 30: BasicJava1

Packages

Packages help manage large Software Systems Packages can contain classes and sub-packages

shipping

GUI

reports

domain

Company Vehicle

Truck RiverBarge

Page 31: BasicJava1

The package Statement

Examplepackage 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

Page 32: BasicJava1

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

Page 33: BasicJava1

Directory Layout and Packages

Packages are stored in the Directory tree containing the package name

Example

shipping/domain/

Company.classVehicle.classRiverBarge.classTruck.class

GUI/reports/

VehicleCapacityReport.class

Page 34: BasicJava1

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