Everything Is an Object 2015-10-311. Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.

Slides:



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

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Names and Bindings.
The Web Warrior Guide to Web Design Technologies
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to scripting
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
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.
The Java Programming Language
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Learners Support Publications Classes and Objects.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Object-Oriented Programming in C++
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Javadoc Dwight Deugo Nesa Matic
Copyright Curt Hill Variables What are they? Why do we need them?
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Variable Scope & Lifetime
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Chapter 2 - Introduction to C Programming
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 2 - Introduction to C Programming
Chapter 1: Computer Systems
Classes and Objects.
Object Oriented Programming in java
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Everything Is an Object

Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television (the object) and a remote control (the reference). For example:

You must create all the objects Where storage lives? Special case: primitive types Arrays in Java

Where the storage lives? Registers. This is the fastest storage because it exists in a place different from that of other storage: inside the processor. However, the number of registers is severely limited, so registers are allocated as they are needed. The stack. This lives in the general random- access memory (RAM) area, but has direct support from the processor via its stack pointer. The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live

Constant storage. Constant values are often placed directly in the program code, which is safe since they can never change. Non-RAM storage. If data lives completely outside a program, it can exist while the program is not running, outside the control of the program. The two primary examples of this are streamed objects, in which objects are turned into streams of bytes, generally to be sent to another machine, and persistent objects, in which the objects are placed on disk so they will hold their state even when the program is terminated

Special case: primitive types Instead of creating the variable by using new, an “automatic” variable is created that is not a reference

All numeric types are signed, so don’t look for unsigned types. The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false

Wrapper classes for primitive types The “wrapper” classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type. For example:

High-precision numbers Java includes two classes for performing high-precision arithmetic: BigInteger and BigDecimal. BigInteger supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations. BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations,

Arrays in Java A Java array is guaranteed to be initialized and cannot be accessed outside of its range. When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: null. When Java sees null, it recognizes that the reference in question isn’t pointing to an object. You must assign an object to each reference before you use it, and if you try to use a reference that’s still null, the problem will be reported at run time. Thus, typical array errors are prevented in Java

You never need to destroy an object In most programming languages, the concept of the lifetime of a variable occupies a significant portion of the programming effort. Scoping – lifetimes of primitive types Scope of objects

Scoping Most procedural languages have the concept of scope. This determines both the visibility and lifetime of the names defined within that scope. In C, C++, and Java, scope is determined by the placement of curly braces {}

Scope: Example A variable defined within a scope is available only to the end of that scope. Any text after a ‘//’ to the end of a line is a comment

Scope of objects Java objects do not have the same lifetimes as primitives. When you create a Java object using new, it hangs around past the end of the scope. For example:

Objects created with new stay around for as long as you want them. Java has a garbage collector, which looks at all the objects that were created with new and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. This means that you never need to worry about reclaiming memory yourself

Creating new data types: class Use the keyword class to mean “I’m about to tell you what a new type of object looks like.” The class keyword is followed by the name of the new type. For example:

This introduces a new type, although the class body consists only of a comment. Create an object of this type using new:

Fields and methods When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: fields (sometimes called data members), and methods (sometimes called member functions)

Fields A field is an object of any type that you can talk to via its reference, or a primitive type. Each object keeps its own storage for its fields; ordinary fields are not shared among objects

Default values for primitive members When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it

This guarantee doesn’t apply to local variables. You get a compile-time error telling you the variable might not have been initialized

Methods, arguments, and return values In many languages (like C and C++), the term function is used to describe a named subroutine. The term that is more commonly used in Java is method, as in “a way to do something.” The fundamental parts of a method are the name, the arguments, the return type, and the body

The return type describes the value that comes back from the method after you call it. The argument list gives the types and names for the information that you want to pass into the method. The method name and argument list (which is called the signature of the method) uniquely identify that method

The argument list The method argument list specifies what information you pass into the method. You are actually passing references when handing objects. The return keyword, which does two things: First, it means “Leave the method, I’m done.” Second, if the method produces a value, that value is placed right after the return statement

Building a Java program Name visibility Using other components The static keyword

Name visibility A problem in any programming language is the control of names. Java uses package as C++ uses namespace. Package name: Use Internet domain name in reverse since domain names are guaranteed to be unique. The entire package name is lowercase

Using other components Whenever you want to use a predefined class in your program, the compiler must know how to locate it. Using the import keyword to tell the compiler to bring in a package

The static keyword To have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created. Need a method that isn’t associated with any particular object of this class. Achieve both of these effects with the static keyword

To make a field or method static, you simply place the keyword before the definition. For example:

There are two ways to refer to a static variable. Refer to it via an object. Refer to it directly through its class name

Comments and embedded documentation There are two types of comments in Java. The first is the traditional C-style comment that was inherited by C++. These comments begin with a /* and continue, possibly across many lines, until a */. The second form of comment comes from C++. It is the single-line comment, which starts with a // and continues until the end of the line

Comment documentation The biggest problem with documenting code has been maintaining that documentation. You need a special comment syntax to mark the documentation and a tool to extract those comments and put them in a useful form. The tool to extract the comments is called javadoc

Comment documentation: Syntax All of the Javadoc commands occur only within /** comments. The comments end with */ as usual. There are two primary ways to use Javadoc: Embed HTML or use “doc tags.” Standalone doc tags are commands that start with an and are placed at the beginning of a comment line. (A leading ‘*’, however, is ignored.) Inline doc tags can appear anywhere within a Javadoc comment and also start with an but are surrounded by curly braces. There are three “types” of comment documentation, which correspond to the element the comment precedes: class, field, or method

Comment documentation: Embedded HTML Javadoc passes HTML commands through to the generated HTML document. This allows you full use of HTML. For example:

Some example package.class#member

Documentation example

Coding style To capitalize the first letter of a class name. If the class name consists of several words, they are run together (that is, you don’t use underscores to separate the names), and the first letter of each embedded word is capitalized

Summary How to write a simple program. Overview of the language and some of its basic ideas