ObjectOriented Programming in Java - PowerPoint PPT Presentation

About This Presentation
Title:

ObjectOriented Programming in Java

Description:

The Java Virtual Machine. The Java Application Programming ... Integer (byre, short, int, long) 0. Floating point - 0.0f or 0.0d. Object reference - null ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 41
Provided by: cmh66
Learn more at: http://www.cs.albany.edu
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming in Java


1
Object-Oriented Programming in
Java
  • Mei-Hwa Chen
  • mhc_at_cs.albany.edu
  • http//www.cs.albany.edu/mhc/csi445r.shtml

2
Course Overview
  • Lab. Assignments Quizzes 40
  • Midterm Exam 20
  • Final Exam 20
  • Projects 20

3
Object-Oriented Programming
  • Encapsulation
  • Inheritance
  • Polymorphism

4
Java Overview
  • Simple
  • Familiar
  • Object-Oriented
  • Secure
  • Multithreaded
  • Robust
  • Architecture-neutral
  • Portable
  • Interpreted
  • High performance
  • Distributed
  • Dynamic

5
The Java Platform
  • The Java Virtual Machine
  • The Java Application Programming Interface (Java
    API)

Java Program
Java API
Java Platform
Java Virtual Machine
Hardware-Based Platform
6
Compiler javac
Java Program
Simple.java
Simple.class
Java API
Interpreter VM java.exe
Interpreter VM java.exe
Interpreter VM java.exe
NT
Solaris
Linux
7
Features Removed from C/C
  • Typedefs, defines, and Preprocessor
  • Structures and Unions
  • Enums
  • Functions
  • Multiple Inheritance
  • Goto Statements
  • Operator Overloading
  • Automatic Coercions
  • Pointers

8
Features Unique to Java
  • Multithreading
  • Packages, Interfaces
  • Garbage collection
  • API
  • Break, continue
  • gtgtgt
  • Documentation comment / . /
  • String

9
Features That Differ
  • Boolean data type
  • Access specifiers
  • Exception handling

10
Compilation Unit
  • PackageDeclarationImportDeclationTypeDeclarat
    ion

11
PackageDeclaration
  • Package Name

12
ImportDeclaration
  • SingleTypeImportDeclaration
  • import Name
  • TypeImportOndemandDeclaration
  • import Name.

13
TypeDeclaration
  • Class declararion
  • Interface declaration

14
A Simple Example
  • package myPackage
  • import java.util.Vector
  • import java.io.
  • public class Myclass
  • Interface MyInterface ..

15
program
Consists of
classes
Consists of
members
are
fields
methods
are
are
variables
objects
void
typed
Declared with
types
classes
constructors
parameters
fields
statements
Inner classes
are
are
are
Invocation Assignment Repetition Selection excepti
on
Special typed methods
fields
16
Java Types
  • Primitive Types
  • Reference Types

17
Primitive Types
  • boolean either true or false
  • char 16-bit Unicode \u0000 \uffff065535
  • byte 8-bit signed -128 127
  • short 16-bit signed -215 215 -1
  • int 32-bit signed -231 231 - 1
  • long 64-bit signed -263 263 - 1
  • float - 32-bit, 6/7 significant bits
  • s. m. 2e s 1, -1, m lt 224 e -149 104
  • double 64-bit, 15 significant bits
  • m lt 254 e -1075 970

18
Initial Values
  • Boolean false
  • Char /u0000
  • Integer (byre, short, int, long) 0
  • Floating point - 0.0f or 0.0d
  • Object reference - null

19
Constants
  • Variables can be declared as constant using
    static and final keyword
  • static final double pi 3.14159
  • Examples
  • 37
  • 045
  • 0x25
  • 5
  • Steffi

20
Identifiers
  • Sequence of letters (Unicode), numbers,
    underscore (_) and Dollar Sign ().
  • First Character must be a letter, or _.
  • Case is significant
  • Java naming convention
  • Class first letter uppercase MyClass
  • Variable first letter lowercase myInt

21
Literals
  • A literal is a value that may be assigned to a
    primitive or string variable or passed as an
    argument to a method call.
  • boolean literals
  • boolean isBig true
  • boolean isLittle false

22
char Literals
  • char c w
  • char c \u4567
  • \n Newline \r Return
  • \t Tab \b backspace
  • \f Formfeed \\ Single Quote
  • \ Double Quote \\ Backslash

23
Integral Literals
  • int i1 28
  • int i1 034
  • int i1 0x1c
  • int i1 0x1C
  • int i1 0X1c
  • int i1 0XC
  • long l 0x7ffffffffffffffL

24
Floating-point Literals
  • A decimal point 1.414
  • The letter E or e, indicating scientific
    notation 4.23E21
  • The suffix F or f, indicating a 32-bits float
    literal 1.828f
  • The suffix D or d, indicating a 64-bit double
    literal 1234d

25
Type Casting
  • Expressions can be cast or converted to a
    different type
  • int int_var
  • double double_var
  • int_var double_var // Error!
  • int_var (int) double_var
  • double_var (double) int_var 37
  • Checked both at compile-time and run-time

26
Primitive Conversion Assignment
  • Widening conversion-automatic conversion
  • The two types are compatible
  • The destination type is larger than the source
    type
  • int int_var
  • double double_var
  • double_var int_var
  • int_var double_var // Error!

27
Widening Conversions
char
int
long
float
double
short
byte
28
Casting Incompatible Types
  • Narrowing conversion- cast
  • int_var (int) double_var
  • byte b int i 258 double 323.142
  • b (byte) i / b 1 /
  • 00000000 00000000 00000001 00000011
  • i (int) d / i 323 /
  • b (byte) d / b 67 /
  • 00000000 00000000 00000001 01000011

29
Primitive Conversion Method Call
  • float frads 2.34567f double d
  • d Math.cos(frads)
  • double d 12.0
  • object ob myVector.elementAt(d)
  • // Error!
  • object ob myVector.elementAt((int)d)

30
Primitive Conversion Arithmetic Promotion
  • All byte and short values are promoted to int
  • If one operand is a long, the whole expression is
    promoted to long
  • If one operand is float, the whole expression is
    promoted to float
  • If any of the operands is double, the result is
    double.

31
Automatic Type Promotion
  • byte a 40 byte b 50 byte c 100
  • ind d a b / c
  • b b 2 // Error!
  • b (byte) (b 2)

32
Reference Types
  • Arrays
  • Classes
  • Interfaces

33
Java References
  • An object is a non-primitive type.
  • A reference variable is a name for an object
  • Reference variables hold the memory locations of
    the object
  • Objects have
  • Variables (fields)
  • Methods (functions)

34
Arrays
  • An array is a group of like-typed variables that
    are referred to by a common name.
  • One-Dimensional
  • Multidimensional
  • Java arrays are objects with operators.
  • Arrays are indexed by integers.
  • Java includes bounds checking on arrays.

35
The Class Java.lang.object
  • Public class Object
  • public final Class getClass()
  • public String toString()

36
Array Members
  • The public final field length, which contains the
    number of components of the array.
  • The public method clone, which overrides the
    method of the same name in class Object and
    throws no checked exceptions.
  • All the members inherited from class Object

37
Arrays
  • Declarations
  • int month_days int matrix
  • int month_days int matrix
  • Memory Allocation
  • month_days new int12
  • matrix new int45
  • int month_days new int12
  • int matrix new int23

38
Array Initialization
  • Java arrays always start at 0!
  • month_days0 31 matrix00 1
  • Declaration, allocation, and initialization
  • int month_days 31, 28,
  • int matrix 0, 1, 2, 1, 2,3

39
Making Arrays Bigger
  • int temp
  • temp primes
  • primes new inttemp.length2
  • for (int i0 i lt temp.length i)
  • primes i tempi

40
Array Standard Exception
  • ArrayStoreException
  • An attempt has been made to store into an array
    component a value whose class is not assignment
    compatible with the component type of the array.
  • IndexOutOfBoundException
  • NegativeArraySizeException
Write a Comment
User Comments (0)
About PowerShow.com