CSC 313 Java Wrapper Classes, Arrays, Strings, StringBuffer - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CSC 313 Java Wrapper Classes, Arrays, Strings, StringBuffer

Description:

Lecture Notes. by Linda M. Hicks. CSC 313 Java. Wrapper Classes, Arrays, Strings, StringBuffer ... myString = 'The quick brown fox' ' jumps over the lazy dog' ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 27
Provided by: gar59
Category:

less

Transcript and Presenter's Notes

Title: CSC 313 Java Wrapper Classes, Arrays, Strings, StringBuffer


1
CSC 313 JavaWrapper Classes, Arrays, Strings,
StringBuffer
2
Wrapper classes
  • Java provides wrapper classes such as Integer
    and Boolean for primitive types such as int and
    boolean, respectively.
  • Wrapper classes have various uses, including data
    conversion.

3
Wrapper Classes for Primitive Types
4
Wrapper Class Methods
  • All of these wrapper classes directly or
    indirectly inherit from Object.
  • Therefore standard methods are available such
    as
  • toString
  • Clone
  • equals

5
Constants Utility Methods
  • The Java platform includes constants and utility
    methods related to the primitive types in wrapper
    classes. For example
  • Integer.MAX_VALUE
  • Integer.MIN_VALUE
  • Character.isDigit
  • Character.isSpace

6
Wrapper classes
  • The code segment
  • String num 123
  • int n Integer.parseInt( num )
  • illustrates data conversion with the Integer
    wrapper class.
  • Wrapper classes also can be used to store
    primitive types in object-only constructs such
    as Vectors and Hashtables.

7
Arrays in Java
  • Primitive types such as int and class types such
    as String can be aggregated in arrays.
  • Arrays are fixed size. Once storage for an array
    has been allocated, the array cannot grow or
    shrink in size.

8
Arrays in Java
  • The code segment int nums new int 100
  • and.. int nums nums new int 100
  • Both examples declare an array of int named nums
    and allocates storage for 100 ints.
  • The arrays cells are initialized to 0, the
    default value for an int

9
Arrays in Java
10
Initializing Arrays
  • If you create arrays with objects that already
    exist, you do not have to use the new keyword.
  • You can initialize an array with your own values
    when you declare it, thereby defining how many
    elements it will have. // An anonymous
    array of 7 elementsint primes  2, 3, 5, 7, 11
    , 13, 17   
  •  An anonymous array is an array object that has
    no name.

11
Initializing Arrays
  • If you specify initializing values for an array,
    you must include values for all the elements. If
    you only want to set some of the array elements
    to values explicitly, you should use an
    assignment statement for each element. For
    example
  • int primes  new int100
  • primes0  2
  • primes1  3
  • You can also initialize an array with an existing
    array. For example, you could declare the
    following array variables,
  • long even  2L, 4L, 6L, 8L, 10L
  • long value  even

12
Copying Arrays
  • You've created two array variables, but you only
    have one array.
  • Both arrays refer to the same set of elements
    and you can access the elements of the array
    through either variable name-for example, even2
    refers to the same variable as value2.

13
Copying Arrays
  • Because arrays are reference types, you cannot
    copy the contents of one array into another with
    the assignment operator.
  • Use the utility method arraycopy, provided by the
    System class in the package java.lang. Syntax
  • void arraycopy (Object source, int SourcePos,
    Object destination, int
    destinationPos, int number)
  • System.arraycopy (even2,0,value2,0, 5)
  • The arraycopy method can be used to copy a whole
    array or just a part of an array.
  • The method fails if the source and destination
    arrays, do not already exist or do not have
    enough space to perform the copy.

See testarrays.java
14
Array Indexes in Java
  • Array elements are accessed through an index
    expression. The code segment
  • int nums new int 100
  • int i 0
  • while ( i lt nums.length )
  • nums i i 1
  • Illustrates loading array nums with numbers 1 to
    100. Note first array position is subscript 0
    as in C.

15
Length Data Member in Arrays
  • Every array has a length data member that
    specifies the number of elements in the array.
    As illustrated by the following code segment
  • int nums new int 100
  • int i 0
  • while ( i lt nums.length )
  • nums i i 1 i i 1

16
Reusing Array Variables
  • An array variable is actually an object which
    can reference different arrays at different
    points in your program. Suppose you've declared
    and defined the variable primes as before
  • int primes
  • primes  new int10      // Allocate an array of
     10 integer elements
  • This produces an array of ten elements of
    type int. A bit later in your program you may
    want the array variable primes to refer to a
    larger array, with 50 elements say. You would
    simply write
  • primes  new int50      // Allocate an array of
     50 integer elements 
  • When this statement is executed, the previous
    array of 10 elements is discarded, along with the
    data values you may have stored in it, as
    illustrated here.

17
Reusing Array Variables

18
Array Utilities in Java
  • Java also provides utility classes for filling,
    searching, sorting, and performing other standard
    operations on arrays. If nums refers to an array
    of integers, the statement
  • Arrays.sort( nums )
  • sorts the array in ascending order.

19
Arrays of Arrays
  • float temperature  new float10365

20
Strings
  • Java provides two standard classes, String and
    StringBuffer, for string processing.
  • Strings are immutable but
  • StringBuffers are not.
  • A double quoted string such as hi is a
    reference to a String object.

21
String Objects
  • A String variable is simply an object of the
    class String. You declare a String variable and
    initialize it with a declaration such as
  • String myString  "My inaugural string"
  • This declares the variable myString as type
    String, and initializes it with the value "My
    inaugural string". You can store another string
    in a String variable, once you've declared it, by
    using an assignment. For example
  • myString  "Strings can be knotty"

22
String Objects are Immutable

23
Operations on Strings
  • Joining Strings use the operatormyString  "Th
    e quick brown fox"  " jumps over the lazy dog"
  • Use System's arraycopy method to efficiently
    copy data from one array into another.

24
The toString () method
  • Every class has a toString()instance method that
    returns an appropriate string representation. For
    example, the statement
  • System.out.println( new Date() )
  • prints a Date as a human-readable string.
  • toString() is an example of a polymorphic method.

25
Standard utility classes
  • The java.util package has an assortment of
    utility classes such as Date for representing
    dates, Random for generating random numbers of
    various types, Vector for dynamically sized
    aggregates of class types, StringTokenizer for
    tokenizing strings, and so on.

26
End of Array Lecture!
Write a Comment
User Comments (0)
About PowerShow.com