03 Java Syntax Basics Part2
03 Java Syntax Basics Part2
Unit objectives
Aftercompleting this unit, you should be
able to:
Create and initialize objects
Use the identity (==) operator
Identify and use primitive wrapper classes
Outline Java’s implementation of Strings, and
work with Strings
Explain the difference between the String and
StringBuffrer classes
4
2025/3/8
Identity
The == relational operator
When this operator is used on objects, it tests for
exact object identity
Checks whether two variables reference the same
object
When this operator is used on primitive types, it
checks for equal values
6
2025/3/8
Wrapper classes
Primitives
have no associated methods; there is no
behavior associated with primitive data types
Each primitive data type has a corresponding
class, called a wrapper
Each wrapper object simply stores a single primitive
variable and offers methods with which to process it
Wrapper classes are included as part of the base
Java API
9
2025/3/8
Strings
TheString type is a class, and not a
primitive data type
A String literal is made up of any number of
characters between double quotes:
10
Concatenating strings
The + operator concatenates Strings:
String a = "This" + " is a " + "String";
There are more efficient ways to concatenate
Strings (this will be discussed later)
11
2025/3/8
String messages
Strings are objects; objects respond to
messages
Use the dot (.) operator to send a message
String is a class, with methods (more later)
12
Comparing strings
Several messages can be sent to a String to test for
equivalence with another String
oneString.equals(anotherString)
Tests for equivalence
Returns true or false
oneString.equalsIgnoreCase(anotherString)
Case insensitive test for equivalence
Returns true or false
oneString == anotherString is problematic
13
2025/3/8
StringBuffer
The StringBuffer class provides a more
efficient mechanism for building strings
String concatenation can get very expensive
String concatenation is converted by most
compilers - into a StringBuffer implementation
Ifbuilding a simple String, just concatenate; if
building a String through a loop, use a
StringBuffer
14
Unit summary
In this unit, you should have learned to:
Create and initialize objects
Use the identity (==) operator
Identify and use primitive wrapper classes
Outline Java’s implementation of Strings, and
work with Strings
Explain the difference between the String and
StringBuffer classes
28