Thanks to visit codestin.com
Credit goes to www.slideshare.net

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
// 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
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
JVM
Unix DOS JavaOS
TestGreeting.java Greeting.java
TestGreeting.class Greeting.class
javac
Also compiles
JVM JVM
Can run on multiple platforms
java
CompileRuntime
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
TestGreeting.java
TestGreeting.class
javac
Class Loader
Byte code verifier
Interpreter
Runtime
JIT Code
Generator
Hardware
Networ
k
Networ
k
Compile Runtime
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
+day : int
+month : int
+year : int
Client code has direct access to
internal data
MyDate d = new MyDate();
d.day=32; //invalid day
d.month=2;d.day=30;//wrong
d.day=d.day+1; //no check
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
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
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
GUI
reports
domain
Company Vehicle
Truck RiverBarge
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

Java Tutorial 1

  • 1.
    Objectives  Key Featuresof Java Technology  Define class and application  Writing a simple Java Application  Java Virtual Machine’s function  Garbage Collection  Code Security
  • 2.
    What is theJava Technology?  A programming Language  A Development environment  An Application environment  A deployment environment
  • 3.
    Primary Goals ofJava 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
  • 4.
    Primary Goals (contd..) Enables users to run more than one thread  Loads classes Dynamically  Furnishes better security
  • 5.
     The followingfeatures fulfill these goals  The Java Virtual Machine  Garbage Collection  Code Security
  • 6.
    // 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
  • 7.
    A Basic JavaApplication-Contd.. //Greeting.java public class Greeting{ private String salutation; Greeting(String s){ salutation = s; } public void greet(String whom){ System.out.println(salutation+” “+whom); } }
  • 8.
    Compiling and Running Compiling TestGreeting.java javac TestGreeting.java  Greeting.java is compiled automatically  Running an application Java TestGeeting
  • 9.
    The Java VirtualMachine  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
  • 10.
    JVM Unix DOS JavaOS TestGreeting.javaGreeting.java TestGreeting.class Greeting.class javac Also compiles JVM JVM Can run on multiple platforms java CompileRuntime
  • 11.
    Garbage Collection  JavaProvides 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
  • 12.
    Code Security TestGreeting.java TestGreeting.class javac Class Loader Bytecode verifier Interpreter Runtime JIT Code Generator Hardware Networ k Networ k Compile Runtime
  • 13.
    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
  • 14.
    Class Loader  Loadsall classes necessary for the execution of a program  Maintain classes of the local file system in separate “namespace”  Prevents spoofing
  • 15.
    Bytecode Verifier  Ensuresthat  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
  • 16.
  • 17.
    Objectives  Define modelingconcepts  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
  • 18.
    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
  • 19.
    Classes as Blueprintsfor 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
  • 20.
    Declaring Java Classes Example public class Vehicle{ private double maxLoad; public void setMaxLoad(double val) { maxLoad=val; } }
  • 21.
    Declaring Attributes  Example publicclass Foo{ public int x; private float y = 1000.0F; private String name=“Fred Smith”; }
  • 22.
    Declaring Methods  Examples publicclass Thing{ private int x; public int getX(){ return x; } public void setX(int new_x){ x=new_x; } }
  • 23.
    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;
  • 24.
    Information Hiding The Problem MyDate +day: int +month : int +year : int Client code has direct access to internal data MyDate d = new MyDate(); d.day=32; //invalid day d.month=2;d.day=30;//wrong d.day=d.day+1; //no check
  • 25.
    Information Hiding  TheSolution: 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
  • 26.
    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
  • 27.
    Declaring Constructors  Example publicclass Thing{ private int x; public Thing(){ x=47; } public Thing(int new_x){ x=new_x; } }
  • 28.
    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
  • 29.
    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){ --- } }
  • 30.
    Packages  Packages helpmanage large Software Systems  Packages can contain classes and sub-packages shipping GUI reports domain Company Vehicle Truck RiverBarge
  • 31.
    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
  • 32.
    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
  • 33.
    Directory Layout andPackages  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
  • 34.
    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

Editor's Notes

  • #13 Figure Illustrate the JRE and how it enforces code security
  • #16 Java software code passes several tests before actually running on your system. The JVM puts the code through a byte code verifier that tests the format of code fragments and checks them for illegal code.