Object Oriented Programming in Java Habib Rostami Lecture 5.

Slides:



Advertisements
Similar presentations
1.00 Lecture 11 Scope and Access. Variable Lifecycles Instance (or object) variables – Created when their containing object is created – Initialized to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Arrays. The array data structure An array is an indexed sequence of components –Typically, the array occupies sequential storage locations –The length.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
1 Lecture 2: Object Oriented Programming in Java.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Objects & Classes Lakshmish Ramaswamy. instanceOf Operator Testing whether an object is of a certain type (class) object instanceOf classname True if.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Classes, Interfaces and Packages
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Classes and Objects. Primitives Single-valued data items. Not Objects byte8-bit signed two's complement integer short16-bit signed two's complement integer.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
JAVA MULTIPLE CHOICE QUESTION.
CSC240 Computer Science III
Introduction to Computer Science and Object-Oriented Programming
Object Oriented Programming in java
Object Oriented Programming in java
More About Objects and Methods
Arrays.
Java Programming Language
Arrays.
Review: libraries and packages
Presentation transcript:

Object Oriented Programming in Java 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 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

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

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)

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)

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; } }

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