everything is an object (ch-2) manipulating objects with references. manipulating objects with...
TRANSCRIPT
Everything is an object (CH-2) Manipulating Objects with References.Manipulating Objects with References.
String s; String s = “IS2550”String s; String s = “IS2550” String s = new String(IS2550);String s = new String(IS2550);
SS SS
0X000000000X00000000 0XA987640F0XA987640F
………………....
““IS2550E”IS2550E”
Method1()Method1()
Method2()Method2()
………………..
Methodn()Methodn()
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’.Make me a new Object of type ‘String’. By Placing an initial value I also specify how I By Placing an initial value I also specify how I
want my string to be made.want my string to be made. Where Storage Lives.Where Storage Lives.
Registers Registers - Stack- Stack Heap Heap - Static Storage- Static Storage Constant StorageConstant Storage - Non-Ram Storage.- Non-Ram Storage.
Special Case - Primitives
Group of Group of TypesTypes that get a special treatment. that get a special treatment. Not create like ordinary Objects with ‘NEW’ Not create like ordinary Objects with ‘NEW’
keyword.keyword. This is done for the sake of efficiency.This is done for the sake of efficiency. Variables created is Variables created is notnot a reference a reference The variable holds the value and are placed on the The variable holds the value and are placed on the
Stack for faster access.Stack for faster access. The size of the variables are fixed and is The size of the variables are fixed and is
independent of the architecture.independent of the architecture.
Special Case - Primitives
Primitive Types : Wrapper To make a non-primitive type of a primitive typeTo make a non-primitive type of a primitive type
char c = ‘x’;char c = ‘x’;Character c = new Character(‘x’);Character c = new Character(‘x’);
High Precision Numbers (Not Primitive)High Precision Numbers (Not Primitive) BigInteger BigInteger - Arbitrary Precision fixed point numbers.- Arbitrary Precision fixed point numbers. BigDecimal BigDecimal - Arbitrary Precision floating point - Arbitrary Precision floating point
numbers.numbers.
Accurately represent values of any size without losingAccurately represent values of any size without losingany information during operation.any information during operation.
Destroy an Object – No needScoping Variables – A variable defined Scoping Variables – A variable defined
within a scope is available only at the within a scope is available only at the
end of that scopeend of that scope
Destroy an Object – No needScoping Objects – Unlike variables Scoping Objects – Unlike variables
Objects created using ‘NEW’ keyword hangs Objects created using ‘NEW’ keyword hangs around past the end of the scope. around past the end of the scope.
{{String s = new String(“IS2550”);String s = new String(“IS2550”);
}}
The reference ‘s’ vanishes at the end of the scope. The reference ‘s’ vanishes at the end of the scope. However the object that s was pointing to still remains However the object that s was pointing to still remains
in memory. Sometimes there is no way to access the in memory. Sometimes there is no way to access the object as in the above example.object as in the above example.
GARBAGE COLLECTOR
Java does not require that you explicitly Java does not require that you explicitly destroy the objects that you create.destroy the objects that you create.
To prevent the ‘orphaned’ objects to fill up To prevent the ‘orphaned’ objects to fill up memory and halt the program, Java utility memory and halt the program, Java utility “Garbage Collector”, built into the language “Garbage Collector”, built into the language runs from time to time to clean up and free runs from time to time to clean up and free the memory.the memory.
Prevents memory leaksPrevents memory leaks
Creating new datatype - Class
Establishing an Object Type Establishing an Object Type Keyword – CLASS [“Keyword – CLASS [“I am about to tell you I am about to tell you
how the new object is going look likehow the new object is going look like”]”]
Class – Fields & Methods
2 kinds of Elements2 kinds of Elements Data Members or AttributesData Members or Attributes
Object Reference (must be initialized)Object Reference (must be initialized)Primitive Types (has default initialization)Primitive Types (has default initialization)Data members are not shared among Data members are not shared among
Objects.Objects.Content determines the state of the object at Content determines the state of the object at
a particular point in time.a particular point in time. Function Members or MethodsFunction Members or Methods
Class - Data Member (Pg-111)
Default Values – Primitive (112)
Guarantees a default Guarantees a default value if the attribute is a value if the attribute is a member of the Class.member of the Class.
This does not apply to the This does not apply to the local variables within a local variables within a function declaration.function declaration.
Class – Methods (Arguments & Return types)
Messages an Object can receive. Messages an Object can receive. Parts of a MethodParts of a Method
int sum (int a, int b) {int sum (int a, int b) {
int result = 0;int result = 0;
result = a + b;result = a + b;
return result;return result;
}}
Return typeReturn type Method NameMethod Name Argument ListArgument List
Return ValueReturn Value
Int x = a.sum(3,2);Int x = a.sum(3,2);
Method - Arguments
Specifies what information you pass into Specifies what information you pass into the method.the method.
Arguments can be of Primitive & non-Arguments can be of Primitive & non-Primitive type (Reference Type). Primitive type (Reference Type).
Method – Return Type
Return type can be a primitive type, a Return type can be a primitive type, a non primitive type (reference type or non primitive type (reference type or object type) or nothing (void).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.
Name Visibility Whenever one wants to use a function or utility Whenever one wants to use a function or utility
the compiler must know where it is located.the compiler must know where it is located. ““import” keyword is used to specify the location import” keyword is used to specify the location
of the utility.of the utility. One imports a Package which is a library of One imports a Package which is a library of
classes.classes. For Java Standard Library the compiler handles it For Java Standard Library the compiler handles it
internally.internally.
Import java.util.ArrayList; // To use Java ArrayList ClassImport java.util.ArrayList; // To use Java ArrayList Class
Import java.util.*; // Import Collection of classesImport java.util.*; // Import Collection of classes
Keyword – “static”
To allow only one piece of storage for a particular To allow only one piece of storage for a particular piece of data regardless of how many objects are piece of data regardless of how many objects are being created.being created.
To allow to call a method that isn’t associated To allow to call a method that isn’t associated with any particular of the class i.e. a method with any particular of the class i.e. a method which can be called without creating an object.which can be called without creating an object.
Sometimes referred as CLASS Attribute or Sometimes referred as CLASS Attribute or Method.Method.
Keyword – “static” - Example
First Java Program
//HelloDate.java//HelloDate.java
Import java.util.*;Import java.util.*;
Public class HelloDate {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());
}}
}}
Comments
/* …………………………….. *//* …………………………….. */ The above can stretch across multiple lines.The above can stretch across multiple lines.
// …………………………………….// ……………………………………. The above is a one line commentThe above is a one line comment
JAVADOC
Utility that extracts comments and outputs it Utility that extracts comments and outputs it as a html.as a html.
One can create a documentation of all the One can create a documentation of all the classes using this utility.classes using this utility.
Only output information on the “public” & Only output information on the “public” & “protected” members.“protected” members.
It passes an HTML code in the program.It passes an HTML code in the program.
JAVADOC Comments between /** Comments between /**
….. */.….. */. HTML.HTML. Data TagData Tag Class Documentation Class Documentation
Tags:Tags: @see @see @Version@Version @author@author @since@since
Method Method documentation documentation TagsTags– @parm@parm– @return@return– @throws@throws– @deprecated.@deprecated.
JAVADOC Example (pg 128)
EXERCISE - 1 Exercise at the End of chapter 2 of Thinking in Java. (PG 130-131)Exercise at the End of chapter 2 of Thinking in Java. (PG 130-131)
Due 01-14-2002Due 01-14-2002 To be submitted : Problems 1,2,3, 7,12 To be submitted : Problems 1,2,3, 7,12
1.1. Zip your java Code and other files and put them in the Digital Drop Zip your java Code and other files and put them in the Digital Drop Box of the Blackboard.Box of the Blackboard.
2.2. Please use the following format for naming your file: Please use the following format for naming your file: FirstnameLastnameAssigntNo.Zip (ShishirGuptaAssign1.Zip).FirstnameLastnameAssigntNo.Zip (ShishirGuptaAssign1.Zip).
For practice : Problem 4, 5, 9, 10, 11.For practice : Problem 4, 5, 9, 10, 11.