Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new."— Presentation transcript:

1 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 0X000000000XA987640F ……….. “IS2550E” Method1()Method2()……….Methodn()

2 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.

3 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.

4 Special Case - Primitives

5 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.

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

7 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.

8 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

9 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”]

10 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

11 Class - Data Member (Pg-111)

12 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.

13 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);

14 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).

15 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.

16 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

17 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.

18 Keyword – “static” - Example

19 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()); }}

20 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

21 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.

22 JAVADOC Comments between /** ….. */. Comments between /** ….. */. HTML. HTML. Data Tag Data Tag Class Documentation Tags: Class Documentation Tags:  @see  @Version  @author  @since F Method documentation Tags – @parm – @return – @throws – @deprecated.

23 JAVADOC Example (pg 128)

24 EXERCISE - 1 F Exercise at the End of chapter 2 of Thinking in Java. (PG 130-131) F Due 01-14-2002 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.


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

Similar presentations


Ads by Google