Download presentation
Presentation is loading. Please wait.
Published byClemence Crawford Modified over 9 years ago
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 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
2
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”);
3
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
4
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)
5
Primitive types - Wrapper classes: char c = 'x'; Character char = new Character(c);
6
Arrays ● Unlike C, Java does range checking and initialization for arrays ● An array of objects is initialized with null references
7
Never need to destroy an object ● Scoping: ● Lifetime of Objects – Objects may live even after end of scope – Garbage collector destroys un-used objects
8
Creating new data types: class ● With a class definition, a new type is created ● Fields (Data members)
9
Creating new types: Class ● Methods, arguments and return values
10
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()); } } ///:~
11
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) { //... } //... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.