Download Everything is an object (CH-2)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Everything is an object (CH-2)

Manipulating Objects with References.
String s;
String s = “IS2550”
String s = new String(IS2550);
………..
“IS2550E”
S
0X00000000
S
0XA987640F
Method1()
Method2()
……….
Methodn()
Object Creation


All objects must be created.
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.
 Registers
- Stack
 Heap
- Static Storage
 Constant Storage
- Non-Ram Storage.
Special Case - Primitives






Group of Types that get a special treatment.
Not create like ordinary Objects with ‘NEW’
keyword.
This is done for the sake of efficiency.
Variables created is not a reference
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.
Special Case - Primitives
Primitive Types : Wrapper

To make a non-primitive type of a primitive type
char c = ‘x’;
Character c = new Character(‘x’);

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.
Destroy an Object – No need
Scoping Variables – A variable defined
within a scope is available only at the
end of that scope
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.
GARBAGE COLLECTOR
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.
 Prevents memory leaks

Creating new datatype - Class
Establishing an Object Type
 Keyword – CLASS [“I am about to tell you
how the new object is going look like”]

Class – Fields & Methods

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
Class - Data Member (Pg-111)
Default Values – Primitive (112)

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.
Class – Methods
(Arguments & Return types)
Messages an Object can receive.
 Parts of a Method

Return type
Method Name
Argument List
int sum (int a, int b) {
int result = 0;
result = a + b;
return result;
}
Int x = a.sum(3,2);
Return Value
Method - Arguments
Specifies what information you pass into
the method.
 Arguments can be of Primitive & nonPrimitive type (Reference Type).

Method – Return Type
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.
 For void the keyword may not be used.

Name Visibility




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.
One imports a Package which is a library of
classes.
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
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 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.
Keyword – “static” - Example
First Java Program
//HelloDate.java
Import java.util.*;
Public class HelloDate {
public static void main(String[] args) {
System.out.println(“Hello it is : ”);
System.out.println(new Date());
}
}
Comments
/* …………………………….. */
 The above can stretch across multiple lines.

// …………………………………….
 The above is a one line comment

JAVADOC
Utility that extracts comments and outputs it
as a html.
 One can create a documentation of all the
classes using this utility.
 Only output information on the “public” &
“protected” members.
 It passes an HTML code in the program.

JAVADOC




Comments between /**
….. */.
HTML.
Data Tag
Class Documentation
Tags:




@see
@Version
@author
@since
 Method
documentation
Tags
–
–
–
–
@parm
@return
@throws
@deprecated.
JAVADOC Example (pg 128)
EXERCISE - 1

Exercise at the End of chapter 2 of Thinking in Java. (PG 130-131)

Due 01-14-2002
To be submitted : Problems 1,2,3, 7,12

1.
2.

Zip your java Code and other files and put them in the Digital Drop
Box of the Blackboard.
Please use the following format for naming your file:
FirstnameLastnameAssigntNo.Zip (ShishirGuptaAssign1.Zip).
For practice : Problem 4, 5, 9, 10, 11.