11
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 programming styles: non-object oriented and object oriented and even mixture of them. Java is a more pure object oriented language Everything is Object in Java To program in Java, one should learn OOP first

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

Embed Size (px)

Citation preview

Page 1: 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

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 programming styles: non-object oriented and object oriented and even mixture of them.

● Java is a more pure object oriented language– Everything is Object in Java– To program in Java, one should learn OOP first

Page 2: 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

Objects are manipulated with references

● The identifier (variable) we use to manipulate objects are actually referenc to object

● A variable may not refer to any object when it is defined:– String s;

● Or it may be refer to an object:– String s=”asdf”;

● All objects should be created before usage:– String s = new String(“asdf”);

Page 3: 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

Where data is stored

● Registers: Fastest storage. It is inside processor. No control on registers in Java programs

● Stack. A part of RAM. Processor has stack pointer for accessing stack. Java stores object references on stack.

● Heap: all Java objects are stored in heap when a new statement is reached at runtime. Java runtime system manages storage allocation of heap.

● Static storage (fixed location): Java objects never stores in static storage

● Constant storage● Non-RAM storage: for example disk storage

Page 4: 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

Special case: primitive types

● Types like int that are used frequently in programs, and need a small amount of storage are not treated as objects.

● They are not created with new and are not references (for efficiency)

● They are stored in stack (for efficiency)

Page 5: 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

Primitive types

- Wrapper classes: char c = 'x'; Character char = new Character(c);

Page 6: 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

Arrays

● Unlike C, Java does range checking and initialization for arrays

● An array of objects is initialized with null references

Page 7: 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

Never need to destroy an object● Scoping:

● Lifetime of Objects– Objects may live even

after end of scope – Garbage collector

destroys un-used objects

Page 8: 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

Creating new data types: class

● With a class definition, a new type is created

● Fields (Data members)

Page 9: 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

Creating new types: Class

● Methods, arguments and return values

Page 10: 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

First Java program//: c02:HelloDate.java// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002// www.BruceEckel.com. See copyright notice in CopyRight.txt.import java.util.*;

/** The first Thinking in Java example program. * Displays a string and today's date. * @author Bruce Eckel * @author www.BruceEckel.com * @version 2.0*/public class HelloDate { /** Sole entry point to class & application * @param args array of string arguments * @return No return value * @exception exceptions No exceptions thrown */ public static void main(String[] args) { System.out.println("Hello, it's: "); System.out.println(new Date()); }} ///:~

Page 11: 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

Compiling an running

● javac and java● Editing, compiling and running the program using

eclipse IDE.● Generating documentation using javadoc● Coding style:

class AllTheColorsOfTheRainbow { int anIntegerRepresentingColors; void changeTheHueOfTheColor(int newHue) { // ... } // ...}