Title: Title Slid
1Title Slid
CSC 444
Java Programming Classes Strings
By Ralph B. Bisland,
Jr.
2Classes
- A class is a construct that Java uses to define
data types and their associated methods. (Well,
kind of anyway) (state variables and behavior) - Any data type that is not part of the core data
types (int, long, float, double, char, byte,
short and boolean) must be defined as a class. - Classes may be user-defined (to be covered
later), part of the Core API, or secured from
some third party.
3Hierarchy Of Classes
- The Java core API has a series of predefined
classes and subclasses stored in packages.
Examples are - applet
- io
- lang
- util
- All Java classes are subtypes of the class
Object. Anything else extends Object.
4Storage Of Primatives
- Primatives are stored in a location in
memory. int x 5
5
x
5Storage Of Classes
- When a class is declared, the variable contains a
pointer to a location containing the pointer
variables and/or methods. Until the class is
initialized with a value(s), the value of the
pointer is NULL. MyClass mc
NULL
mc
6The String Class
- A string is a group of Unicode characters stored
contiguously. - A string literal is delimited by double
quotes. ABC \u0041\u0042\u0043 - The String class is a predefined class which is a
subclass of java.lang. - The type declaration for a string is String.
- String is a class, not a core data type, and is
treated differently from a core data type.
7Partial String Class Definition
- public final class String extends Object
-
- //constructors
- public String ()
- public String (String value) throws
- NullPointerException
- . . .
- // class methods
- public static String valueOf (char c)
- public static String valueOf (int i)
- Public static String valueOf (float f)
8Partial String Class Def(ctd)
. . . // instance methods public String
concat (String str) throws NullPointerException
public int length () public String
toUpperCase ()
9Three Types Of Methods
- Note from the partial String class definition, a
class may contain up to three different types of
methods. - Constructors
- Class Methods
- Instance Methods
- Each method has its own unique purpose.
10Constructors
- Constructor methods always have the same name as
the class in which they are defined. - The purpose of a constructor is to initialize the
value of the object to a value (or no value). - It would seem that to declare the existence of a
variable of type String, you should specify the
type (String) followed by an identifier. St
ring alphabet
11Constructors (ctd)
- This technique does not allocate memory for the
variable alphabet so it does not work.
12Why Not?
What happens when we declare primitives? char
letter A
A
letter
This is not what happens when we declare a class.
When we declare a class we get a pointer that
points to an object of the type. String
alphabet
Null
alphabet
Primitives are stored by value. Objects are
stored by reference. Alphabet is an instance of
the class String.
13Why Not?
- Primitives are stored by value, classes are
stored by reference. - The variable alphabet is an occurrence of the
class String. We must create an instance of the
class String which alphabet points to.
14Initializing Strings
To initialize a string (which is really an
object), you should use one of the constructor
methods. String a new String () String b
new String (abcde)
Null
a
abcde
b
15Important Note
Since the string data type is commonly used,
Java provides a shortcut technique for
initializing a string. The reserved word new
may be omitted and an object of type String can
be declared as follows String alpha abcde
16Assigning Strings
Assigning one string into another does not
produce replicate data, only replicate
pointers. String str abcde String
anotherString anotherString str
abcde
str
anotherString
17Changing Strings
- Strings are immutable.
- Once a string is assigned a value, individual
values in the string can not be changed. - You may change the complete string by assigning
it a new value.
18StringTest.java
orca cat StringTest.java class StringTest
public static void main (String args)
String s "Ralph Bisland"
System.out.println (The value of s is "
s) s "Dottie Bisland"
System.out.println ("The value of s is now "
s) orca javac StringTest.java orca
java StringTest The value of s is Ralph
Bisland The value of s is now Dottie
Bisland orca
19Class Methods
- Class methods can be used on all data in a class.
- Class methods are called by stating the name of
the method preceded by the class name. - Example, converting an integer to a string, use
the class method String.valueOf(). int
value 20 String int1 new String ()
int1 String.valueOf(value)
20Method Modifiers
- public Implies the method can be used anywhere
the class can be used. - static The method can not be invoked by an
object. A static method is a class method and is
invoked by using the name of an object.
21Instance Methods
- Used to describe the characteristics and
operations you might associate with an object
such as a string of characters. - Examples
- length Returns the current length of the string.
- concat Concatenates two strings.
- Applies to a specific string (an instance of a
String) and not to all strings. - Instance method do not contain the modifier
static.
22Examples
- String s abcde
- int i
- i s.length() // i contains 5
- s s.concat (01234)
- // s contains abcde01234
- i s.length () // i contains 10
23Notes
- Once an object of a particular class has been
declared, you are only allowed to perform
operations upon objects of that class by using
instance methods defined by the class. - A class containing a constructor and instance
methods may be thought of as a data type.
24Passing Strings To Methods
- Strings are passed to methods by value.
- However if you change the value of the string
within the method, the string is changed. You
change what the pointer points to, not the
pointer. - Strings are immutable within methods.
- Methods can return a value of type String.
25Wrapper Classes
- Primitive data types (int, long, float, double,
char, short, byte and boolean) have corresponding
classes containing methods useful when dealing
with data of the specified type. - The classes are Character, Integer, Long, Float,
Double, Short, Byte and Boolean. Note they all
capitalized. - These classes are known as wrapper classes since
they wrap the data into a class.
26Partial Listing Of The Integer WrapperClass
public final class Integer extends number
public static final int MIN_VALUE 0x80000000
public static final int MAX_VALUE 0x7ffffff
public Integer (int value) public Integer
(String s) throws NumberFormatException
public int intValue . . .
27Explanations
MIN_VALUE and MAX_VALUE are constants that
contain the minimum and maximum integer
values. Integer can be used as a
constructor. Integer (String s) converts a
string to an integer. Note the
exception. intValue converts an Integer object
to an int.
28Why Have Wrapper Classes?
Wrapper classes allow us to convert a primitive
to an object. int primitiveValue 36 Integer
newValue newValue new Integer(primitiveValue)
or you can do it in one statement Integer
newValue new Integer (primitiveValue) To
assign a value of class Integer into an int, you
must use the instance method intValue. primitiveV
alue newValue.intValue()
29Memory Allocation
int primitiveValue 36
36
primitiveValue
Integer newValue new Integer (primitiveValue)
36
newValue
30More On Class Hierarchies
- Definition of String class public final class
String extends Object - The keyword extends implies that all methods
and data defined in the class Object is inherited
by the class String. - Object is the superclass and String is the
subclass.
Object
Superclass
String
Subclass
31Important Concept
When one class inherits from another class
anywhere in the hierarchy, an object of any
subclass in the hierarchy is also a legal
superclass object.
32Important Concepts (ctd)
- Two important concepts regarding object
assignment and parameter passing. - An object of a subclass may be assigned to an
object of its superclass without any data type
violation. Object objectDatum new
Object() String stringDatum new String
(ABC) objectDatum stringDatum //
Legal - An object of a subclass may be passed as an
argument to a method that requires a parameter of
its superclass. More on this later