String Class: Prof. Irysh Paulo R. Tipay, MSCS
String Class: Prof. Irysh Paulo R. Tipay, MSCS
Introduction
What is a Class?
So far you know that.
- building blocks in Java
- contains main method
Introduction
Large applications in Java Contain a
collection of classes.
Two types
Built-in classes - predefined functionality
(System, String, Scanner)
User Defined - Created by programmer
Introduction
Collection of classes is called a package.
Collection of packages is called a library.
Introduction
As a programmer, you can create your
own package or library.
You can also use libraries or packages
created by other programmers.
User-defined Libraries/Package
Introduction
You can also use Built-in
libraries/packages in Java.
java.util
java.io
java.awt
java.lang
Introduction
Java.lang Library
-doesn't need to be explicitly imported, java
does it automatically
-included in all java programs
-provides access to the basic Java Classes
-contains String, System, Math.
String Class
Contains a sequence of characters
Enclosed in double quotes.
String is NOT a primitive data type
String is a class
Reference Variables
Variables created using the String Class are
called objects
Objects in Java are reference variables.
Reference variables do not store the
actual value but rather it holds the address
where the value is stored
var
1
letter
char letter =
b;
str
1000111
String str =
Hello;
1000111
Hello
String is Immutable
Objects of the String Class are
immutable.
String str = Hello;
str = Hi;
10111
Hi
str
1000111
10111
1000111
Garbage
Collectio
Hello
n
Declaring Strings
String str1 = Hello;
OR
String str1= new String(Hello);
str1
110011
1
1100111
Hello
str2
110011
1
str1
110000
1
111001
11
1100001
11100111
Hello
Hello
str1
110011
1
str1 == str2?
1100111
TRUE
Hello
str2
110011
1
str2
str1
110000
1
111001
11
1100001
11100111
Hello
Hello
FALSE
str2
str1
110000
1
111001
11
1100001
11100111
Hello
Hello
TRUE
Questions?