1 / 16

Object Oriented Programming in Java

Learn Java syntax, primitive types, wrappers, arrays, packages, modifiers. Understand scope rules, wrappers vs primitives, array structure, package management.

eflanagan
Download Presentation

Object Oriented Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgramminginJava 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 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

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

  6. 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)

  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. 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

  10. Packages • 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

More Related