Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in Java Habib Rostami Lecture 5.

Similar presentations


Presentation on theme: "Object Oriented Programming in Java Habib Rostami Lecture 5."— Presentation transcript:

1 Object Oriented Programming in Java Habib Rostami Lecture 5

2 Today’s Presentation  Java Syntax  Primitive types  Wrappers  Arrays  Packages  Important packages  Modifiers (final,static…)

3 Java Syntax  Extension of C Syntax  Blocks & Scopes Each variable is visible into its block and its inner blocks Fields are accessible where they are visible

4 Primitive types byte8-bit signed integer short16-bit signed integer int32-bit signed integer long64-bit signed integer float32-bit double64-bit char16-bit Unicode character boolean void

5 Wrappers Wrapper classes offers services related to the corresponding primitive types Boolean, Integer, Double, ……

6 Arrays Array indices are integers An array of length n has bounds 0 and n-1 Arrays are homogeneous –However, an array of an object type may contain objects of any subtype of that object For example, an array of Animal may contain objects of type Cat and objects of type Dog An array of Object may contain any type of object (but cannot contain primitives simultaneously)

7 Arrays Arrays are objects –Arrays are allocated by new, manipulated by reference, and garbage collected –However, the usual bracket notation a[i] is provided Arrays are reflective –a.length is the length of array a

8 Arrays int x[] = new int[10]; int x[][] = new int[10][15]; int x[][] = new int[10][]; (ragged)

9 Ragged arrays int ragged[][] = new int[4][]; for (int i = 0; i < 4; i++) { ragged[i] = new int[i + 1]; } for (int i = 0; i < 4; i++) { for (int j = 0; j < ragged[i].length; j++) { ragged[i][j] = 10 * i + j; } } 01230123 0 1 2 3 0 10 20 30 11 21 3133 22 32

10 Packages –Importing existing packages –Declaring new packages –The directory structure and resolving name clashes

11 Packages Replaces the C++ namespace Allows classes to be grouped together Packages can be arranged in a hierarchy Packages scope the contained classes, allowing name clashes to be avoided or resolved You will already have been using packages distributed with the Java SDK So far, you have probably put all of your new classes into a single directory and used the default (anonymous) package –this works fine for a small application with few class files, but as applications increase in size, and include additional third-party classes, you can expect scope issues to arise

12 Declaring a package To incorporate a new class into a package use the package keyword (place this declaration at the top of the file) package package_name; public class class_name { … } For a class to be visible outside of a package it needs public access Recall that a source file can only have one public class, which must have the same name as the file Items default to package visibility - this is an unfortunate choice since it is easy to forget to add the private modifier and end up with public visibility within the package! Package hierarchies can be created: package top_level_package_name.package_name; public class class_name { … }

13 import statements A class can be accessed directly by using the package name, e.g. java.util.ArrayList ra = new java.util.ArrayList (); The import keyword is used to gain access to a class or classes within a package hierarchy without having to use the package name All classes within a package can be imported using import package_name.* A single class within a package can be imported using import package_name.class_name All classes within the java.lang package are implicitly imported Importing a whole package rather than a single class has no effect on code size!

14 Resolving name clashes Even using package hierarchies, it is possible to import two packages which have common names, e.g. java.util and javax.swing both have a Timer class If you don’t use the Timer class there is no problem A specific import statement can be used to resolve the conflict import java.util.*; import javax.swing.*; import java.util.Timer; If both classes are needed then the full package name must be used with each class java.util.Timer t1 = new java.util.Timer (…); javax.swing.Timer t2 = new javax.swing.Timer (…);

15 Important Packages java.lang java.util java.math

16 Modifiers static final


Download ppt "Object Oriented Programming in Java Habib Rostami Lecture 5."

Similar presentations


Ads by Google