Intro to JAVA
Download
Report
Transcript Intro to JAVA
Intro to JAVA
By:
Riyaz Malbari
04/07/04
1
History of JAVA
Came into existence at Sun Microsystems, Inc.
in 1991.
Was initially called “Oak” but was renamed
JAVA.
Originally it was not developed for the Internet
but as a software to be embedded in various
consumer electronic devices.
Derives much of its character from C/C++.
04/07/04
2
Types of Programs
Applications
a program that runs on
the computer, under its
operating system.
it is like a normal
program created using
C/C++.
Applets
a tiny JAVA program that
can be transmitted over
the Internet.
it is an intelligent
program.
04/07/04
3
Bytecode
output of a JAVA compiler is Bytecode.
executed by the JAVA run-time system, which is
called the JAVA Virtual Machine (JVM).
creates truly portable programs.
assures security while downloading programs
over the Internet.
04/07/04
4
JAVA Virtual Machine
04/07/04
5
Object-Oriented Programming
The core of JAVA
Encapsulation
Inheritance
Polymorphism
04/07/04
6
A simple program
/* A program to display the message Hello World.
Call this file “hello.java”. */
class hello {
// The program begins with a call to main().
public static void main (String args [ ]) {
System.out.println (“Hello World.”) ;
}
}
save as text file hello.java
at command line type>javac hello.java
execute program by typing>java hello
04/07/04
7
Data Types
Type
Size
Range
byte
8 bits
-128 to 127
short
16 bits
-32768 to 32767
int
32 bits
-2147483648 to 2147483647
long
64 bits
-9223372036854775808 to 9223372036854775807
float
32 bits
1.7e-308 to 1.7e308
double
64 bits
3.4e-038 to 3.4e038
char
16 bits
0 to 65536
boolean
---------
true or false
04/07/04
8
Variables
basic unit of storage
a combination of an identifier, a type and an optional initializer
Syntax: type identifier [=value][, identifier[=value],…];
eg. int a,b,c;
int d=4, e, f=7;
byte w=12;
double pi=3.14159;
char x=‘x’;
Dynamic initialization:
class DyIn {
public static void main (String args[ ]) {
double a=3.0, b=4.0 ;
double c=Math.sqrt (a*a+b*b) ; // c is dynamically initialized
System.out.println (“Hypotenuse is “ + c);
}
}
04/07/04
9
Variable scope
scope is defined by a block { }
variables that are defined within the scope are not visible to the code that is defined outside the scope.
scopes can be nested
objects declared in the outer scope will be visible to code within the inner scope
eg.
class Scope {
public static void main (String args[ ]) {
int x ; // known to all code within main
x=10;
if (x= =10) {
int y=20; // known only to this block
System.out.println (“x and y:” + x + “ “ + y +) ;
x=y*2;
}
y = 100; // y is unknown here and so there is a compile-time error
}
}
variables declared in the inner and outer scope cannot have the same name
eg. class scopeerr {
public static void main (String args[ ]) {
int a=10;
{
int a=20;
// Compile-time error as variable ‘a’ is already defined
}
}
}
04/07/04
10
Arrays
collection of variables of the same data type addressed by a common name
an array element is accessed by its index
can have one or more dimensions
one-dimensional array:
declaration: type var_name[ ];
eg.
int month[];
allocation: var_name=new type[size];
eg. month = new int[12];
both can be done together: int month[ ] = new int[12];
assignment: month[0] = 31;
month[1] = 28;
values can also be assigned during declaration:
int month[ ] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Multi-dimensional arrays: int twoD[ ][ ] = new int[2][3];
[0][0]
[1][0]
[0][1]
[1][1]
04/07/04
[0][2]
[1][2]
11
Class
fundamental concept in JAVA
defines a data type which can be used to create objects of that type
class is a template for an object and object is an instance of a class
declared by the class keyword
Syntax:
class classname
type var;
type methodname (parameter list) {
// body of method
}
Class
Class Members
Instance
variables
Methods
04/07/04
12
Example…
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main (String args[ ]) {
Box mybox = new Box();
// An object called mybox is created
double vol;
// Assign values to mybox’s instance variables.
mybox .width = 10;
mybox .height = 20;
mybox .depth = 15;
vol = mybox .width * mybox .height * mybox .depth;
System.out.println (“The volume is “ + vol) ;
}
}
Save the file as BoxDemo.java
Compilation creates two .class files, one for Box and the other for BoxDemo
Execute the BoxDemo.class file
04/07/04
13
changing the instance variables of one object have no effect on that of another
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main (String args[ ]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
mybox1 .width = 10;
mybox1 .height = 20;
mybox 1.depth = 15;
mybox2 .width = 3;
mybox2 .height = 6;
mybox2 .depth = 9;
vol = mybox1 .width * mybox1 .height * mybox1 .depth;
System.out.println (“The volume is “ + vol) ;
vol = mybox2 .width * mybox2 .height * mybox2 .depth;
System.out.println (“The volume is “ + vol) ;
}
}
04/07/04
14
adding a method to the box class…
class Box {
double width;
double height;
double depth;
void volume() {
System.out.println (“Volume is ” + (width * height * depth)) ;
}
}
class BoxDemo {
public static void main (String args[ ]) {
Box mybox = new Box();
double vol;
mybox .width = 10;
mybox .height = 20;
mybox .depth = 15;
mybox .volume();
}
}
04/07/04
15
Returning a value…
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth ;
}
}
class BoxDemo {
public static void main (String args[ ]) {
Box mybox = new Box();
double vol;
mybox .width = 10;
mybox .height = 20;
mybox .depth = 15;
vol = mybox .volume();
System.out.println (“Volume is “ + vol) ;
}
}
04/07/04
16
Constructors
initializes the object immediately upon creation
has the same name as that of the class
class Box {
double width;
double height;
double depth;
Box() {
// A constructor for the Box class
width = 10;
height = 20;
depth = 15;
}
double volume() {
return width * height * depth ;
}
}
class BoxDemo {
public static void main (String args[ ]) {
Box mybox = new Box();
double vol;
vol = mybox .volume();
System.out.println (“Volume is “ + vol) ;
}
}
04/07/04
17
Method Overloading
implementation of polymorphism
two or more methods within the same class have the same name but different parameter declarations
overloaded methods must differ in the type and/or number of their parameters
class OverloadDemo {
void test() {
System.out.println (“No parameters”) ;
}
void test(int a) {
// Overload test for one integer parameter
System.out.println (“a: ” + a) ;
}
void test(int a, int b) { // Overload test for two integer parameters
System.out.println (“a and b: ” + a + “ “ + b) ;
}
}
class Overload {
public static void main (String args[ ]) {
OverloadDemo ob = new OverloadDemo() ;
ob.test();
ob.test(10);
ob.test(10, 20);
}
}
04/07/04
18
Inheritance
defines a general class that defines traits common to a set of related items
other specific classes add things that are unique to it
a class that is inherited is called a superclass while the one that does the inheriting is called the subclass
the extends19 keyword is used to inherit a class
multilevel hierarchies can be created
04/07/04
19
class A { // Creating a superclass A
int i, j;
void showij() {
System.out.println (“i and j: “ + i +” “ + j) ;
}
}
class B extends A { // Creating a subclass B by extending class A
int k;
void showk() {
System.out.println (“k: “ + k) ;
}
void sum() {
System.out.println (“i + j + k: “ + (i +j + k)) ;
}
}
class Inheritance {
public static void main (String args[]) {
A superOb = new A();
B subob = new B();
superOb.i = 10;
superOb.j = 20;
superOb.showij();
subOb.i = 3;
subOb.j = 6;
subOb.k = 9;
subOb.showij();
subOb.showk();
subOb.sum();
}
}
04/07/04
20
Packages
partitions the class name space
classes defined in a package are not accessible by code outside that package
syntax: package package_name;
the import statement is used to bring certain packages or the entire package into visibility
package Demo;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
System.out.println(name + “: $” + bal) ;
}
}
class AccBal {
public static void main(String args[ ]) {
Balance current[] = new Balance[3] ;
current[0] = new Balance(“Bush”, 0.99);
current[1] = new Balance(“Donald”, 9.99);
current[2] = new Balance(“Rice”, 19.99);
for (int i=0; i<3; i++) current[i].show() ;
}
}
04/07/04
21
Access Specifiers
public
No restrictions. Accessible from anywhere.
private
Only accessible from within the class.
package
Accessible from package members.
protected
Also accessible from subclasses and package members.
04/07/04
22
JAVA for Security
Basic concepts:
Authentication
Authorization
Confidentiality
Integrity
JAVA tools:
JAAS (JAVA Authentication And Authorization Service)
JSSE (JAVA Secure Socket Extensions)
GSS-API (JAVA Generic Security Service Application Programming
Interface)
04/07/04
23
JAVA Security Mechanisms
Authentication
Authorization
Confidentiality
Integrity
JAAS
X
X
JSSE
X
X
X
GSS-API
X
X
X
04/07/04
24
Thank you!
04/07/04
25