LECTURE 3 CLASSES
AND OBJECTS
Prepared by Dr. Donghoon Kwon
The String Class
• A variable holds either a primitive type or a reference to
an object
• Java has no primitive data type that holds a series of
characters.
• A class name can be used as a type to declare an object
reference variable
• The String class from the Java standard library is used
for this purpose.
• In order to be useful, a variable must be created to
reference a String object.
String number;
Primitive vs. Reference Variables
• Primitive variables actually contain the value that they
have been assigned.
number = 25;
• The value 25 will be stored in the memory location
associated with the variable number.
• Objects are not stored in variables, however. Objects
are referenced by variables.
String Objects
• A variable can be assigned a String literal.
String value = "Hello";
• Strings are the only objects that can be created in
this way.
• A variable can be created using the new keyword.
String value = new String("Hello");
• This is the method that all other objects must use when
they are created.
String Methods
• Since String is a class, objects that are
instances of it have methods.
• One of those methods is the length method.
stringSize = value.length();
• This statement runs the length method on the
object pointed to by the value variable.
String Methods (Cont’)
1. startsWith() method
• Checks whether a string starts with the specified character(s).
Returns either true or false
2. endsWith() method
• Check whether a string ends with the specified character(s).
Returns either true or false
3. equals() method
• Compares two strings, and returns true if the strings are equal.
Otherwise, it returns false
4. indexOf() method
• Returns the position of the first occurrence of specified character(s)
in a string
String Methods (Cont’)
5. lastindexOf() method
• Returns the position of the last occurrence of specified character(s)
in a string
6. length() method
• Returns the length of a string
7. replace() method
• Searches a string for a specified character, and returns a new
string where the specified character(s) are replaced.
8. replaceAll() method
• Returns a string replacing all the sequence of characters matching
regex and replacement string.
String Methods (Cont’)
9. split() method
• Breaks a given string around matches of the given regular
expression. After splitting, this method returns a string array.
There are other string methods available.
String Methods (Cont’)
• Exercise #1: Make a program in Java shown in Figure
below using the following string methods: toUpperCase(),
replace(), substring(), and length().
Java API
• A class library is a collection of classes that we can use
when developing programs.
• The Java API is the standard class library that is part of
any Java development environment.
• The classes of the Java API are organized into packages
https://docs.oracle.com/en/java/javase/17/docs/api/allpacka
ges-index.html
Java API (Cont’)
• When you want to use a class from a package, you could
use its fully qualified name
java.util.Scanner
• Or you can import the class, and then use just the class
name:
import java.util.Scanner;
• To import all classes in a particular package, you can use
the * wildcard character:
import java.util.*;
Import Declarations
The Random Class
• The Random class is part of the java.util package
• It provides methods that generate pseudorandom
numbers
• A Random object performs complicated calculations based
on a seed value to produce a stream of seemingly
random values
The Random Class (Cont’)
The Random Class (Cont’)
The Random Class (Cont’)
• Exercise #2: Make a program in Java to generate random
numbers shown in Figure below.
The Math Class
• The Math class is part of the java.lang package
• The Math class contains methods that perform various
mathematical functions
• These include:
• absolute value
• square root
• exponentiation
• etc.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html
The Math Class (Cont’)
• The methods of the Math class are static methods (also
called class methods)
• Static methods can be invoked through the class name –
no object of the Math class is needed
value = Math.cos(90) + Math.sqrt(delta);
The Math Class (Cont’)
• Exercise #3: Make a program in Java to determine the
roots of a quadratic equation through user input.
−𝑏± 𝑏2 −4𝑎𝑐
• Given ax2 + bx + c = 0, 𝑥 =
2𝑎
Formatting Output
• It is often necessary to format values in certain ways so that they can
be presented properly
• The Java API contains classes that provide formatting capabilities
• The NumberFormat class allows you to format values as currency or
percentages
• The DecimalFormat class allows you to format values based on a
pattern
• Both are part of the java.text package
Formatting Output (Cont’)
• The NumberFormat class has static methods that return a
formatter object
getCurrencyInstance()
getPercentInstance()
• Each formatter object has a method called format that
returns a string with the specified information in the
appropriate format
Formatting Output (Cont’)
• Exercise #4: Make a program in Java shown in Figure
below using getCurrencyInstance() and
getPercentInstance() methods.
Wrapper Classes
• The java.lang package contains wrapper classes that
correspond to each primitive type:
Methods to get the value
Primitive Data Types Wrapper Classes associated with the
corresponding wrapper object
byte Byte byteValue()
short Short shortValue()
int Integer intValue()
long Long longValue()
float Float floatValue()
double Double doubleValue()
boolean Boolean booleanValue()
char Character charValue()
Wrapper Classes (Cont’)
• Example #1
Integer myInt = 3;
Float myFloat = 2.12f;
Double myDouble = 3.1415;
Character myChar = 'A';
Boolean myBool = true;
System.out.println(myInt);
System.out.println(myFloat);
System.out.println(myDouble);
System.out.println(myChar);
System.out.println(myBool);
Wrapper Classes (Cont’)
• Example #2
Integer myInt = 3;
Float myFloat = 2.12f;
Double myDouble = 3.1415;
Character myChar = 'A';
Boolean myBool = true;
System.out.println(myInt.intValue());
System.out.println(myFloat.floatValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
System.out.println(myBool.booleanValue());
Wrapper Classes (Cont’)
• Example #3
Integer myInt = new Integer(3);
Float myFloat = new Float(2.12f);
Double myDouble = new Double(3.1415);
Character myChar = new Character('A');
Boolean myBool = new Boolean(true);
System.out.println(myInt);
System.out.println(myFloat);
System.out.println(myDouble);
System.out.println(myChar);
System.out.println(myBool);
Autoboxing
• Autoboxing is the automatic conversion of a primitive
value to a corresponding wrapper object
Integer obj;
int num = 42;
obj = num;
• The assignment creates the appropriate Integer object
• The reverse conversion (called unboxing) also occurs
automatically as needed