Upload
clemence-crawford
View
216
Download
2
Embed Size (px)
Citation preview
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
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”);
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
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)
Primitive types
- Wrapper classes: char c = 'x'; Character char = new Character(c);
Arrays
● Unlike C, Java does range checking and initialization for arrays
● An array of objects is initialized with null references
Never need to destroy an object● Scoping:
● Lifetime of Objects– Objects may live even
after end of scope – Garbage collector
destroys un-used objects
Creating new data types: class
● With a class definition, a new type is created
● Fields (Data members)
Creating new types: Class
● Methods, arguments and return values
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()); }} ///:~
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) { // ... } // ...}