Using final - PowerPoint PPT Presentation

About This Presentation
Title:

Using final

Description:

public class FinalTest. private static final Circle wheel = new Circle(5.0) ... Runtime rt = Runtime.getRuntime(); long mem = rt.freeMemory ... public class Customers ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 29
Provided by: csHu
Category:
Tags: class | final | using

less

Transcript and Presenter's Notes

Title: Using final


1
Using final
  • We use the notion of constant data to represent
    data that cannot be changed.
  • public class Test
  • static final int someInt 10
  • //
  • someInt 9 // ERROR
  • //

2
Using final
  • What happens when we want a constant object?
  • public class Circle
  • private double radius
  • public Circle(double r)
  • radius r
  • public void setRadius(double r)
  • radius r
  • public double getRadius()
  • return radius

3
Using Final
  • public class FinalTest
  • private static final Circle wheel new
    Circle(5.0)
  • public static void main(String args)
  • System.out.println(radius is
    wheel.getRadius())
  • wheel.setRadius(7.4)
  • System.out.println(now the radius
    is

  • wheel.getRadius())

4
Using Final
  • The output of the code is
  • radius is 5.0
  • now the radius is 7.4
  • How can the wheel object change values when we
    specifically declared it final???

We did not change the value of the variable
wheel. We changed the content of the object
that wheel references.
5
Using final
  • What if we try the following code
  • public class FinalTest
  • private static final Circle wheel new
    Circle(5.0)
  • public static void main(String args)
  • System.out.println(radius is
    wheel.getRadius())
  • wheel new Circle(7.4)
  • System.out.println(now the radius
    is

  • wheel.getRadius())

6
Using Final
  • Compiling this code results in what we expect
  • FinalTest.java9 Cant assign a value to a final
    variable wheel
  • wheel new Circle(7.4)
  • 1 error
  • There is an error because we are attempting to
    modify the reference which was defined as final.
  • The reference is final and thus immutable.
  • The object itself is not affect and thus is
    mutable.

7
Arrays vs. Vectors
  • Java provides two constructs, an array and a
    Vector,
  • which appear to be similar.
  • In fact, the array and Vector are altogether
    different.
  • It is important to understand the differences
    between
  • the two, in order to write efficient code.

8
Arrays summary
  • Arrays have fixed size - after creating an array,
    you cannot add more elements than its maximum
    size. If you do, you will get an
    ArrayIndexOutOfBoundsException
  • In Java, arrays are objects, so any methods
    contained in java.lang.Object can be invoked on
    them.
  • In order to find the length of the array, use the
    public variable length.
  • int ia new intN
  • System.out.println(ia length is
    ia.length)

9
Arrays summary
  • Arrays can hold both primitive types and object
  • references.
  • Default values are used for each entry, based
    on its type.

Type Default value
boolean false
char \u0000
short 0
int 0
long 0
float 0.0
double 0.0
object reference null
10
Vectors summary
  • An Vector grow its size dynamically when more
  • elements are added than its current size can
  • accommodate.
  • As elements get deleted, each element with an
    index
  • greater than the index being removed is shifted
  • downward.
  • Unlike arrays, you call a method on a Vector to
  • determine its size.
  • Vector v new Vector()
  • System.out.println(v length is
    v.size())

11
Vectors summary
  • The Vectors size measures the number of
    elements it
  • holds.
  • Thus, the Vectors size may vary, unlike the
    arrays
  • size which is fixed.
  • A vector is implemented in terms of an array of
  • java.lang.Object.
  • That is, when it grows, or shrinks, the entire
    array
  • must be reallocated and copied.
  • This may cause performance problems if Vectors
    are
  • not used properly.

12
Vectors summary
  • Finally, a Vector may contain only object
    references an
  • not primitive types.
  • Vector v new Vector()
  • v.add(new Turtle()) // OK
  • v.add(5) // ERROR
  • Integer i new Integer(5)
  • v.add(i) // OK

13
Arrays vs. Vectors
If you are working with a primitive type,
consider using an array instead of Vector.
Support for primitive types Support for objects Auto size fast
Array Yes Yes No Yes
Vector No Yes Yes No
14
Memory Management
  • Java provides an automatic memory management with
    its garbage collection.
  • For this reason, free and delete are unnecessary.
  • This may lead some programmers to ignore memory
    issues.
  • The garbage collector frees memory held by an
    object only if the object is no longer being
    referenced.

Turtle one new Turtle()
Turtle two one
one null
two null
15
Memory Management
  • There are many implementation for the garbage
    collection algorithm as there are numerous JVMs.
  • Multiple invocations might be needed to reclaim
    an unreferenced object.
  • Runtime rt Runtime.getRuntime()
  • long mem rt.freeMemory()
  • System.out.println(Free memory is
    mem)
  • //
  • System.gc()
  • //
  • mem rt.freeMemory()
  • System.out.println(Free memory is now
    mem)

16
Memory Management
  • Problems can arise when objects contain instance
    variables that is initialized in the constructor
    and consumes large amounts of memory
  • public class Customers
  • private int cusIdArray
  • public Customers(String filename)
  • int num // read amount from file
  • cusIdArray new intnum
  • for (int i0 iltnum i)
  • cusIdArrayi // value from the
    file

17
Memory Management
  • Suppose we want to display all the ids to the
    screen
  • public class Display
  • public static void main(String args)
  • Customers cust new Customers()
  • // display the ids to the screen
  • // Customers object is no longer
    needed.
  • //
  • // the rest of the application

Assume there are 20,000 different customers !!!
18
Memory Management
  • One solution is to set the local variable cust to
    null, after it is used.
  • public class Display
  • public static void main(String args)
  • Customers cust new Customers()
  • // display the ids to the screen
  • // Customers object is no longer
    needed.
  • //
  • cust null
  • // the rest of the application

19
Memory Management
  • What if you need to keep the cust object, but
    have limited use for the cusIdArray, after it is
    displayed ?

public class Customers // same
code as above public void resetCust()
cusIdArray null
public static void main(String args)
Customers cust new Customers() // display
the ids to the screen // Customers object is
no longer needed. // cust.resetCust()
// the rest of the application
The solution above works, but has a potential
negative implications. We may need to handle the
case that the array is needed but no longer
valid.
20
Objects and Equality
  • Java provides two different types reference
    types and primitives. In addition wrapper
    classes are provided for each primitive type.

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
21
Objects and Equality
  • References and primitives behave altogether
    differently and have different semantics.
  • int i 5
  • Integer j new Integer(10)
  • Both primitives and references are stored and
    manipulated on the Java operand stack.
  • Objects are stored in memory heap.

22
Objects and Equality
  • Using a primitive type eliminates the need to
    call new and create an object - this saves time
    and space.
  • Mixing primitives and objects can also create
    unexpected results with regard to assignment.

int a 1 int b 2 Point x
new Point(0,0) Point y new Point(1,1)
System.out.println(a is a)
System.out.println(b is b)
System.out.println(x is x)
System.out.println(y is y)
23
Objects and Equality
// performing assignment and setLocation
a b a x y
x.setLocation(5,5) System.out.println(a
is a) System.out.println(b is b)
System.out.println(x is x)
System.out.println(y is y)
24
Objects and Equality
  • The output for the code is
  • a is 1
  • b is 2
  • x is java.awt.Pointx0,y0
  • y is java.awt.Pointx1,y1
  • a is 3
  • b is 2
  • x is java.awt.Pointx5,y5
  • y is java.awt.Pointx5,y5

25
Objects and Equality
Point (0,0)
Point (0,0)
x
x
Point (1,1)
Point (5,5)
y
y
x y
x.setLocation(5,5)
26
Differentiate between and equals
  • What is the difference between the operator
    and the equals method? Isnt good enough?
  • Public class Test
  • public static void main(String args)
  • int a 10
  • int b 10
  • System.out.println(ab is
    (ab))
  • Integer ia new Integer(10)
  • Integer ib new Integer(10)
  • System.out.println(iaib is
    (iaib))

27
Differentiate between and equals
  • The output for this code is
  • ab is true
  • iaib is false
  • What went wrong?
  • a and b are of type int, and as such, they are
    of primitive
  • type.
  • ia and ib are object references and reference
    Integer
  • objects.

28
Differentiate between and equals
  • a and b have the same value of 10.
  • The object references, ia and ib are really
    references to
  • two different Java Integer Objects that have
    the value 10.
  • The values of ia and ib are not 10 but rather
    unique
  • values that represent the two objects.

The operator tests for equality. Is the thing
on the left-hand side of the the same as the
thing on the right-hand side?
How do you test to see whether the values
referenced by ia and ib are equal? This is where
you use the equals method.
Write a Comment
User Comments (0)
About PowerShow.com