Вы находитесь на странице: 1из 34

Introduction To Java

1
Objectives of This Session

State the history of Java


Describe features of Java
State what is a class
Distinguish between a class and a structure
Construct a simple Java class (HelloWorld, Date)
State public/private access specifiers.
Demonstrate the use of Accessor & Mutator methods
List the sequence of activities for Java program execution
State the list of primitive data types used in Java

2
Refer Core Java
Chapter 1

History of Java

Background: Electronic consumer devices


Sun commissioned Project Green
Developed language Oak -Later renamed to Java
Was dismissed as just another OO programming lang
Became popular with the rising popularity of www

3
Features of java

Features of Java
Object Oriented Architecture neutral
Simple Portable
Robust Interpreted
Distributed Dynamic
Secure Multi threaded

4
Simple

Java- designed as closely to C++ as possible


Omits many confusing & rarely used features:
E.g.
No Header files, structures, unions,pointer arithmetic,
operator overloading,virtual base class etc.

5
Distributed

Distributed System
Extensive library of routines for coping with TCP/IP
protocols like HTTP & FTP
Applications can access objects across the net with ease
E.g.
Creating socket connections,Servlets & CGI scripting
RMI enables communication between different objects

6
Robust

Reliable
Early checking for potential problems.
Dynamic checking to eliminate error-prone situations.
Developer doesnt have to worry about
Bad pointers
Memory allocation errors.
Memory leakage.

7
Secure

Java apps used in distributed environments too

Thus lot of emphasis on security

Security Manager

Byte code verifier

8
Architecture Neutral

Java compiler generates byte codes


An architecture-neutral file format to transport code
efficiently independent of hardware platforms &
Operating Systems.

9
Windows
.c file

Source
Compile Mac
Code

Compile
UNIX

10
Windows
.Java file .class file

Source Byte Mac


Code
Compile Code

UNIX

11
Portable

Java defines behavior of its basic data types & behavior of


its arithmetic operators
Thus programs are the same on every platform no data
type incompatibilities across platforms.
E.g.
int always a 32-bit int
E.g.
Strings stored in standard Unicode format

12
Multi Threaded

Thread-safe: Lib functions implemented such that it can be


executed by multiple concurrent threads
Built-in support for threads

13
Interpreted

Interpreter can execute bytecode on any machine that has


java interpreter & java run time system
Classes are linked only as needed

14
Structures in C

Structure in C
Supports only data abstraction
Only function pointer can be data member

15
Structures in C

Structure in C
struct directory
{
char Name[30];
long PhoneNo;

};

16
Class

struct keyword can be replaced by class keyword

Generally struct is used in C context while class is used


in Java context
In a Java class functions can be clubbed together in a class
along with the data members
class and object is Object Oriented terminology

17
Class

A class is a template for the creation of like objects


An object is an instance of a class
Map real world entities into classes through data members
& member functions
By writing class & creating objects of that class one can
map two major pillars of object model i.e. abstraction &
encapsulation into software domain

18
Class Syntax

class class-name
{
variable declaration ;
method declaration ;
public static void main(String args[ ])
{
..
}
} // end of class

20
Class greeting

/* a comment */
// another comment
class greeting
{
public static void main(String args[ ]){
System.out.println(Hello world);
} // end of main
} //end of class

21
A sample class

class Date {
int dd, mm, yy;
public void initDate(){
dd = mm = yy = 0;
}
public void setDate(int d, int m, int y){
dd = d;
mm = m;
yy = y;
}
public void dispDate(){
System.out.println(Date is : + dd +-+ mm +-+ yy);
}

22
A sample class

public static void main(String args[])


{
Date d1;
d1 = new Date();
d1.initDate();
d1.dispDate();

d1.setDate(3,7,90);
d1.dispDate();
}
}

23
Access specifiers

Allow to specify the scope of a class member


There are 4 types
public
default
protected
private

24
Accessor & mutator methods

class Demo{
String name;

public void setName(String s){


name = s;
}
public String getName() {
return name;
}
}

25
Accessor and Mutator methods

Assessor methods merely Mutator methods actually


access instance fields / change contents of
variables instance fields
E.g. E.g.
getXXX() methods setXXX() methods

26
A Typical Java Environment

.java file java compiler .class file

class Loader

Valid & Safe Bytecode


Bytecode Verifier

Java Runtime System


JVM

Native OS

27
Class Loader

Loads required .class files in memory


Files can be loaded from
local disk
over a local network
Over the internet

28
Bytecode Verifier

Verifies that the byte codes are safe


i.e. do not violate Javas security restrictions
Checks the internal consistency of the class & the
validity of the code

29
JVM

Is the heart of Java's network orientation


Software abstraction for a generic h/w platform.
Responsible for managing all the details of actually carrying
out platform-specific functions
Highly platform-dependent

30
primitive data types

Type Storage Requirements


int 4 bytes
short 2 bytes
long 8 bytes
byte 1 byte
boolean 1 bit
float 4 bytes
double 8 bytes
char 2 bytes

31
Primitive DateTypes

32
Summary of Primitive Datatypes

33
Summary of Primitive Data Types

Width
Data Types Default Value Wrapper Class
(bits)
boolean false 1 Boolean
byte 0 8 Byte
short 0 16 Short
char \u0000 16 Character
int 0 32 Integer
long 0 64 Long
float +0.0F 32 Float
double +0.0D 64 Double

34

Вам также может понравиться