Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
CIT 590 Intro to Programming First lecture on Java.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CIT 590 Intro to Programming First lecture on Java.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Object-Oriented Programming in C++
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Javadoc A very short tutorial. What is it A program that automatically generates documentation of your Java classes in a standard format For each X.java.
Programming in Java CSCI-2220 Object Oriented Programming.
Copyright Curt Hill Variables What are they? Why do we need them?
CSI 3125, Preliminaries, page 1 Compiling the Program.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Variable Scope & Lifetime
More on Arrays Review of Arrays of ints, doubles, chars
CIS 068 JAVA vs. C++ Main Differences CIS 068.
Topic: Classes and Objects
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Java Programming
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Applying OO Concepts Using Java
Core Concepts.
Object Oriented Programming in java
Java Programming Language
Java Basics Data Types in Java.
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Classes and Objects Object Creation
SPL – PS1 Introduction to C++.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new String(IS2550); String s = new String(IS2550); S S 0X XA987640F ……….. “IS2550E” Method1()Method2()……….Methodn()

Object Creation All objects must be created. All objects must be created. String s = new String(“CATAWBA”); String s = new String(“CATAWBA”);  Make me a new Object of type ‘String’.  By Placing an initial value I also specify how I want my string to be made. Where Storage Lives. Where Storage Lives.  Registers - Stack  Heap - Static Storage  Constant Storage- Non-Ram Storage.

Special Case - Primitives Group of Types that get a special treatment. Group of Types that get a special treatment. Not create like ordinary Objects with ‘NEW’ keyword. Not create like ordinary Objects with ‘NEW’ keyword. This is done for the sake of efficiency. This is done for the sake of efficiency. Variables created is not a reference Variables created is not a reference The variable holds the value and are placed on the Stack for faster access. The variable holds the value and are placed on the Stack for faster access. The size of the variables are fixed and is independent of the architecture. The size of the variables are fixed and is independent of the architecture.

Special Case - Primitives

Primitive Types : Wrapper To make a non-primitive type of a primitive type To make a non-primitive type of a primitive type char c = ‘x’; Character c = new Character(‘x’); High Precision Numbers (Not Primitive) High Precision Numbers (Not Primitive)  BigInteger - Arbitrary Precision fixed point numbers.  BigDecimal - Arbitrary Precision floating point numbers. Accurately represent values of any size without losing any information during operation.

Destroy an Object – No need Scoping Variables – A variable defined within a scope is available only at the end of that scope

Destroy an Object – No need Scoping Objects – Unlike variables Objects created using ‘NEW’ keyword hangs around past the end of the scope. { String s = new String(“IS2550”); } The reference ‘s’ vanishes at the end of the scope. However the object that s was pointing to still remains in memory. Sometimes there is no way to access the object as in the above example.

GARBAGE COLLECTOR Java does not require that you explicitly destroy the objects that you create. Java does not require that you explicitly destroy the objects that you create. To prevent the ‘orphaned’ objects to fill up memory and halt the program, Java utility “Garbage Collector”, built into the language runs from time to time to clean up and free the memory. To prevent the ‘orphaned’ objects to fill up memory and halt the program, Java utility “Garbage Collector”, built into the language runs from time to time to clean up and free the memory. Prevents memory leaks Prevents memory leaks

Creating new datatype - Class Establishing an Object Type Establishing an Object Type Keyword – CLASS [“I am about to tell you how the new object is going look like”] Keyword – CLASS [“I am about to tell you how the new object is going look like”]

Class – Fields & Methods 2 kinds of Elements 2 kinds of Elements  Data Members or Attributes  Object Reference (must be initialized)  Primitive Types (has default initialization)  Data members are not shared among Objects.  Content determines the state of the object at a particular point in time.  Function Members or Methods

Class - Data Member (Pg-111)

Default Values – Primitive (112) Guarantees a default value if the attribute is a member of the Class. Guarantees a default value if the attribute is a member of the Class. This does not apply to the local variables within a function declaration. This does not apply to the local variables within a function declaration.

Class – Methods (Arguments & Return types) Messages an Object can receive. Messages an Object can receive. Parts of a Method Parts of a Method int sum (int a, int b) { int result = 0; int result = 0; result = a + b; result = a + b; return result; return result;} Return type Method Name Argument List Return Value Int x = a.sum(3,2);

Method - Arguments Specifies what information you pass into the method. Specifies what information you pass into the method. Arguments can be of Primitive & non- Primitive type (Reference Type). Arguments can be of Primitive & non- Primitive type (Reference Type).

Method – Return Type Return type can be a primitive type, a non primitive type (reference type or object type) or nothing (void). Return type can be a primitive type, a non primitive type (reference type or object type) or nothing (void). ‘return’ keyword is used to return a type. ‘return’ keyword is used to return a type. For void the keyword may not be used. For void the keyword may not be used.

Name Visibility Whenever one wants to use a function or utility the compiler must know where it is located. Whenever one wants to use a function or utility the compiler must know where it is located. “import” keyword is used to specify the location of the utility. “import” keyword is used to specify the location of the utility. One imports a Package which is a library of classes. One imports a Package which is a library of classes. For Java Standard Library the compiler handles it internally. For Java Standard Library the compiler handles it internally. Import java.util.ArrayList; // To use Java ArrayList Class Import java.util.*; // Import Collection of classes

Keyword – “static” To allow only one piece of storage for a particular piece of data regardless of how many objects are being created. To allow only one piece of storage for a particular piece of data regardless of how many objects are being created. To allow to call a method that isn’t associated with any particular of the class i.e. a method which can be called without creating an object. To allow to call a method that isn’t associated with any particular of the class i.e. a method which can be called without creating an object. Sometimes referred as CLASS Attribute or Method. Sometimes referred as CLASS Attribute or Method.

Keyword – “static” - Example

First Java Program //HelloDate.java Import java.util.*; Public class HelloDate { public static void main(String[] args) { public static void main(String[] args) { System.out.println(“Hello it is : ”); System.out.println(“Hello it is : ”); System.out.println(new Date()); System.out.println(new Date()); }}

Comments /* …………………………….. */ /* …………………………….. */ The above can stretch across multiple lines. The above can stretch across multiple lines. // ……………………………………. // ……………………………………. The above is a one line comment The above is a one line comment

JAVADOC Utility that extracts comments and outputs it as a html. Utility that extracts comments and outputs it as a html. One can create a documentation of all the classes using this utility. One can create a documentation of all the classes using this utility. Only output information on the “public” & “protected” members. Only output information on the “public” & “protected” members. It passes an HTML code in the program. It passes an HTML code in the program.

JAVADOC Comments between /** ….. */. Comments between /** ….. */. HTML. HTML. Data Tag Data Tag Class Documentation Tags: Class Documentation Tags: F Method documentation Tags

JAVADOC Example (pg 128)

EXERCISE - 1 F Exercise at the End of chapter 2 of Thinking in Java. (PG ) F Due F To be submitted : Problems 1,2,3, 7,12 1. Zip your java Code and other files and put them in the Digital Drop Box of the Blackboard. 2. Please use the following format for naming your file: FirstnameLastnameAssigntNo.Zip (ShishirGuptaAssign1.Zip). F For practice : Problem 4, 5, 9, 10, 11.