15
Object Oriented Programming

Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Embed Size (px)

Citation preview

Page 1: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Object Oriented Programming

Page 2: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural
Page 3: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Developed at Sun Microsystems in 1991

James Gosling, initially named “OAK”Formally announced java in 1995Object oriented and cant write

procedural programsFunctions are called methodsUnit of a program is the class from

which objects are createdAutomatic garbage collectionSingle inheritance only

Page 4: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Each class contains data and methods which are used to manipulate the data

Programs are small and portableMultithreaded which allows several

operations to be executed concurrently

A rich set of classes and methods are available in java class libraries

Platform IndependentCase sensitive

Page 5: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Edit – use any editorCompile – use command ‘javac’ if

your program compiles correctly, will create a file with extension .class

Execute – use the command ‘java’

Java program development

Page 6: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Keywords are special reserved words in java that you cannot use as identifiers for classes, methods or variables. They have meaning to the compiler, it uses them to understand what your source code is trying to do.

Java language keywords

Page 7: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

class FirstPro { public static void main(String args[]) { System.out.println("Hello World!“); } }

First java Program

Page 8: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

All java source files must end with the extension ‘.java’

Generally contain at most one top level public class definition

If a public class is present, the class name should match the file name

Java Source files

Page 9: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

If these are present then they must appear in the following order.Package declarationsImport statementsClass definitions

Top level elements appears in a file

Page 10: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

An identifier is a word used by a programmer to name a variable, method class or label.Keywords may not be used as an identifierMust begin with a letter, a dollar sign($) or an

underscore( _ ).Subsequent character may be letters, digits, _

or $.Cannot include white spaces. ref: Note

Identifiers

Page 11: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Type identifier;Type identifier=initial value;

Declaring Variables

Page 12: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Primitive Data TypesType Bits Range

boolean 1 true, false

char 16 0 to 65,535

byte 8 -128 to +127

short 16 -32,768 to +32,767

int 32 -232 to +232-1

long 64 -264 to +264-1

float 32 -3.4E+38 to +3.4E+38 (approx)

double 64 -1.8E+308 to +1.8E+308 (approx)

Page 13: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

int grade; // an integer to hold a grade, no initial value

int grade = 0; // an integer to hold a grade with initial value 0

char answer; // an answer to something – one character

String name; // a string to hold a nameString name = "Gumboot"; // as above, with an

initial valueboolean finished; // to hold "true" if finishedboolean finished = false; // as above, but with

an initial value

Page 14: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Declarationsint grade;char answer;String name;

Assignmentsgrade = 70;answer = 'a';name = "alan turing";

Declarations and Assignments

Page 15: Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural

Default Values

Data Type Default Value (for fields)

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

String (or any object)   null

boolean false