java course 1 computer engineering association. compile your first program public class hello{...

20
JAVA COURSE 1 Computer Engineering Association

Upload: gwendoline-conley

Post on 13-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

JAVA COURSE 1

Computer Engineering Association

Compile your first programPublic class Hello{ public class Hello(){

System.out.println(“Hello”);}

puclic static void main(STring args[]){System.out.println(“In the main”);Hello temp = new Hello();}

}

Command to compile the first program

In java, we use the jdk, which can be downloaded from the website www.java.sun.com

After installation, if we don't want to give the absolute path everytime to compile the program, we need to set the path

In windows 2000, go to [control panel]->[setting]->[system]->[advance] and change the system enviroment variable, PATH , adding the path of [jdk directory]\bin

In linux, you need to edit the .bash_profile file Add Path=[jdkdirectory]/bin Export Path Exec bash --login to make it effective

For windows 95/95/me, you need to edit the autoexec.bat file and add the path as Path = ... same as above

Package

Java use the package mechanism to resolve all the class name

e.g., there are two classes as Hello1, Hello1 But they are different, as they are put in

diofferent directory. How about we import them, as if include

mechanism in C++, C So we simple type import [directory].Hello1; And thus we can use it. The java put all their classes in [jdkdirectory]\

clases

Simple program to demonstrate package

Before we need to set the classpath Same as before setting path, but we set

CLASSPATH now CLASSPATH=\root\demo:\root\j2sdk1.4.0\

classes Then we type the following program as

shown

Syntax: Class definition

Class definition is defined by the following words public(optional) final(optional) abstract(optional)

If we declare final, then we cannot declare abstract, vice versa. And the poition of the above words is not rigid. Class Class name(same as file name) Extends (optional) implements(optional) {}

Meaning of the class keyword

Public means all classes can access it, whetherit is in the same package or not

If not public, then the class is accessed only by the class in the same package.

Final means the class cannot be inherited Abstract means the class must be inherited. Extends means to inherit a class Implements means to inherit from the

interface The {} is needed and should not be omitted.

Syntax about method

Access modifier: [public|private|protected|default]

[abstract|final|synchronized|native|static](optional)

The order of the above two position is not important.

Return type [type|void] Name of method Argument Exception throw(optional) ()(abstract or interface method don't need ())

Meaning of these class syntax

First key word

Visibility Public ProtectedDefault PrivateFrom the same class Y Y Y YFrom any class in the same package Y Y Y NFrom any class outside the package Y N N NFrom a class in the same package Y Y Y NFrom a class outside the same package Y Y N N

Final is the method declares a method that cannot be overridden in the subclass

Abstract means the method that should be implement in the subclass

Static means the method can be access though no object is created, but they can access instance variable or static variable

Synchronized is used in mutil-threading Native means it is in C|C++ code Void means no return type Throw, we explain later

Variable

They are divided into following types: Static variable Instance variable Local variable Final variable Transient variable Volatile variable

CLASS SCOPE SYNTAX: Public, private, default, protected Transient, final, static, volatile type Name

LOCAL VARIABLE (DECLARE WITHIN METHOD) Final(optional) Type Name

Meaning of these words

The modifier are the same as method modifier.

Final means, const if C, C++ Static means the variable can be accessed

even the object is not there. Transient, volatile, talk later

Class variable characteristics

Access through object Like Hello temp = new Hello();

temp.you, (assumed we have you variable) Static variable can be accessed though no

object is there. Hello.temp no object is created.

Local variable

Life Only within the same Dead after the method

Data type

In java, everything is class, except the following data type, which is not class

Int Char Boolean Float Double Byte(8 bit) Long (64 bit) Short(16 bit) String is object. We will talk about it later

About concept of referenial variable

e.g Hello temp = new Hello(); The, there is only one object allocated in

heap area. What is heap and stack? Stack is used for the store of local variable And heap is for the dynamic allocation of

memory, when we use new keyword, we allocate memory for the object.

How about we type Hello temp2 = temp They refer to the same object!.

Concept of object -oriented programming

Encapsulation We package the variable within a class, and so

we ensure some variable are not modified from outside

Inheritance We need to know the concept of ISA and HASA Like there is a employer class And we create a hour_employeer class to inherit

from it. The main point is we ask ourselves, is

hour_work a type of employee?

Casting As we know hour_employee is a type of

employee and inherit from it and thus we can say hour_employee is employee

e.g. Employee temp = new hour_employee It works But now it is employee object The above is called static casting which cast

within compile time And (hour_employee) temp, is called dynamic

casting, that a superclass type want to cast itself to be the subclass. It may not work

Polymorphism We write a method a() in the super class

superclass And a() in the subclass sub, which extends

superclass We declare Superclass temp = new subclass() temp.a() is called not based on the type, but the

actual object allocated in the heap memory How about no a() declare in the sub? A() is called based on the type And this fexibility allow easy programming For example we write a method called Public void outName(Superclass a) We can write outName(subclass)