0% found this document useful (0 votes)
3 views6 pages

03 Java Syntax Basics Part2

This document outlines the objectives and key concepts of a unit focused on Java programming, including creating and initializing objects, using the identity operator, and understanding primitive wrapper classes. It explains the differences between String and StringBuffer classes, as well as how to manipulate and compare strings in Java. The unit aims to equip learners with essential skills for working with objects and strings in Java.

Uploaded by

yuyutong20060724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

03 Java Syntax Basics Part2

This document outlines the objectives and key concepts of a unit focused on Java programming, including creating and initializing objects, using the identity operator, and understanding primitive wrapper classes. It explains the differences between String and StringBuffer classes, as well as how to manipulate and compare strings in Java. The unit aims to equip learners with essential skills for working with objects and strings in Java.

Uploaded by

yuyutong20060724
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2025/3/8

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

Objects and messages


 Objectsprovide more complex behavior
than primitive data types
 Objects respond to messages
 The dot "." operator is used to send a message
to an object

4
2025/3/8

Declaring and initializing objects


 Just like primitives and arrays, objects must be declared
before they can be used
 The declaration requires the type of the object
 The type is the class of the object

 Use = for assignment (including initialization)


 Initialization of an object often uses the new operator
 The new operator is used if you want to create a new object
 An object can be initialized to null
 Arrays of objects are declared just like arrays of primitives
 Arrays of objects default to initialization with null
 Examples:

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

Using wrapper classes

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:

 String objects can be initialized in other


ways:

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)

 Primitivetypes used in a call to println are


automatically converted to Strings
 System.out.println("answer = " + 1 + 2 + 3);
 System.out.println("answer = " + (1+2+3));
 Do you get the same output from the above
examples?

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

You might also like