160 likes | 169 Views
Learn Java syntax, primitive types, wrappers, arrays, packages, modifiers. Understand scope rules, wrappers vs primitives, array structure, package management.
E N D
Object Oriented ProgramminginJava Habib Rostami Lecture 5
Today’s Presentation • Java Syntax • Primitive types • Wrappers • Arrays • Packages • Important packages • Modifiers (final,static…)
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
Primitive types byte 8-bit signed integer short 16-bit signed integer int 32-bit signed integer long 64-bit signed integer float 32-bit double 64-bit char 16-bit Unicode character boolean void
Wrappers • Wrapper classes offers services related to the corresponding primitive types • Boolean, Integer, Double, ……
Arrays • Array indices are integers • An array of lengthnhas bounds0andn-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)
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
Arrays • int x[] = new int[10]; • int x[][] = new int[10][15]; • int x[][] = new int[10][]; (ragged)
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; }} 0 1 2 3 0123 0 10 11 20 21 22 30 31 32 33 Ragged arrays
Packages • Packages • Importing existing packages • Declaring new packages • The directory structure and resolving name clashes
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
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 { … }
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!
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 (…);
Important Packages • java.lang • java.util • java.math
Modifiers • static • final