Objectives w Key Features of Java Technology w

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

What is the Java Technology? w A programming Language w A Development environment w An Application environment w A deployment environment

Primary Goals of Java Technology w Provides an easy to use language by n n 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 l Code Portability l

Primary Goals (contd. . ) w Enables users to run more than one thread w Loads classes Dynamically w Furnishes better security

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

A Basic Java Application // Test. Greeting. java // Sample "Hello World" Application public class Test. Greeting{ 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 w Compiling Test. Greeting. javac Test. Greeting. java w Greeting. java is compiled automatically w Running an application Java Test. Geeting

The Java Virtual Machine w Provides hardware platform specifications w Reads compiled byte codes that are platform independent w Is implemented as software or hardware w Is implemented in a Java technology development tool or a web browser

Compile Test. Greeting. javac Greeting. java Also compiles Test. Greeting. class Runtime java Greeting. class JVM Can run on multiple platforms Unix JVM DOS JVM Java. OS

Garbage Collection w Java Provides a system level thread to track memory allocation w Garbage Collection n Checks for and frees memory no longer needed Is done automatically Can vary across JVM implementations

Code Security Runtime Compile Class Loader Test. Greeting. javac Networ k Byte code verifier Interpreter JIT Code Generator Test. Greeting. class Runtime Hardware

Java Runtime Environment w JVM Performs 3 main tasks n n n Loads Code – Performed by class loader Verifies Code – Performed by bytecode verifier Executes Code – Performed by runtime interpreter

Class Loader w Loads all classes necessary for the execution of a program w Maintain classes of the local file system in separate “namespace” w Prevents spoofing

Bytecode Verifier w Ensures that n n n 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 w Define modeling concepts n Abstraction, encapsulation and Packages w Define class, member, attribute, method, constructor and package w Use access modifiers private and public w Invoke a method on a particular Object

Abstraction w Functions – Write an algorithm once to be used in many situations w Objects – Group a related set of attributes and behaviors into a class w Frameworks and API’s – Large group of objects that support a complex activity

Classes as Blueprints for Objects w A blueprint is a description from which many physical devices are constructed w A class is a description of an object n n A class describes the data each object includes A class describes the behaviors that each object exhibits w Classes support 3 key features of OOP • Encapsulati • Inheritance • Polymorphi on sm

Declaring Java Classes w Example public class Vehicle{ private double max. Load; public void set. Max. Load(double val) { max. Load=val; } }

Declaring Attributes w Example public class Foo{ public int x; private float y = 1000. 0 F; private String name=“Fred Smith”; }

Declaring Methods w Examples public class Thing{ private int x; public int get. X(){ return x; } public void set. X(int new_x){ x=new_x; } }

Accessing Object Members w The “dot” notation <object>. <member> is used to access object members including attributes and methods w Example thing 1. set. X(47); thing 1. x=47;

Information Hiding The Problem My. Date +day : int +month : int +year : int Client code has direct access to internal data My. Date d = new My. Date(); d. day=32; //invalid day d. month=2; d. day=30; //wrong d. day=d. day+1; //no check

Information Hiding w The Solution: My. Date -day: int -month: int -year: int +get. Day(): int +get. Month(): int +get. Year(): int +set. Day(d: int): boolean +set. Month(m: int) +set. Year(y: int) -valid. Day(d: int): boolean Client code must use setters / getters to create internal data My. Date d = new My. Date(); d. set. Day(32); //invalid returns false d. set. Month(2); d. set. Day(30); //set. Day returns false d. set. Day(d. get. Day()+1); //will return false if wrap occurs

Encapsulation My. Date -date: long +get. Day(): int +get. Month(): int +get. Year(): int +set. Day(d: int) +set. Month(m: int) +set. Year(y: int) -valid. Day(d: int): boolean w Hides implementation details w Forces the user to use an interface to access data w Makes the code more maintainable

Declaring Constructors w Example public class Thing{ private int x; public Thing(){ x=47; } public Thing(int new_x){ x=new_x; } }

The Default Constructor w There is always at least one constructor in every class w If no constructors are provided a default constructor will be present n n The default constructor takes no arguments The default constructor has no body w Enables you to create object instances without having to write a constructor

Source File Layout w Example package shipping. reports. web; import shipping. domain. *; import java. util. List; import java. io. *; public class Vehicle. Capacity. Repport{ private List vehicles; public void generate. Report(Writer op){ --} }

Packages w Packages help manage large Software Systems w Packages can contain classes and sub-packages shipping domain GUI reports Company Truck Vehicle River. Barge

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

The import Statement w Precedes all class declarations w Tells the compiler where to find classes to use w Examples import java. io. Writer; //Specify a class to access import java. io. *; //all classes in the package

Directory Layout and Packages w Packages are stored in the Directory tree containing the package name w Example shipping/ domain/ Company. class Vehicle. class River. Barge. class Truck. class GUI/ reports/ Vehicle. Capacity. Report. class

Terminology Recap w Class – The source-code blueprint for a run-time object w Object – An instance of a class w Attribute – A data element of an object AKA: data member, instance variable, data field w Method – A behavioral element of an object AKA: algorithm, function, procedure w Constructor – A method-like construct used to initialize an object w Package – A group of classes and/or sub-packages
- Slides: 34