ITC 1233 - OBJECT ORIENTED
PROGRAMMING
STRINGS IN JAVA
Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
DO YOU KNOW WHAT IS A STRING?
WHAT ARE THE DATA TYPES IN JAVA
YOU HAVE LEARNED?
JUST TO REFRESH YOUR MEMORY
REFRESHING: PRIMITIVE DATA TYPE
• The basic types of data.
Ex: int, float, double, boolean, char and also byte, long,
short.
• Mainly used to store values of the variable’s
data type.
REFRESHING: NON-PRIMITIVE DATA TYPE
• Any instantiable class
Ex: strings, arrays, objects
• Used to store the addresses of an object.
SO, WHAT WOULD BE THE MAIN
IMPORTANCE OF USING STRINGS?
STRINGS
• Strings are used for storing text. A string variable
contains a collection of characters surrounded by
double quotes.
• Strings in Java are objects backed internally by a
character array.
• Since arrays are immutable (cannot grow), Strings
are also immutable.
STRINGS CONTINUED;
Mainly there are two ways of creating a String
object in Java.
• Using String literal
• Using new keyword
STRING USING STRING LITERAL
In Java, Strings can be created by assigning a String
literal to a String instance.
After compiling this above line, what would be happen?
STRING USING STRING LITERAL CONTINUED;
When a new String is created using String literal,
• JVM looks if any other String is stored with the same
value.
• If found, it just returns the reference to that String object,
• Else it creates a new String object with the given value.
STRING USING STRING LITERAL CONTINUED;
After compiling the the above code segment, what would be
happened?
STRING USING STRING LITERAL CONTINUED;
• In the example given above, only one String object will be created
by the compiler.
• The same instance will be assigned to both the object
references.
• That means, if we have n number of String instances that have
the same value, in memory, there will be only one object and all
n number of object references will be pointing to the same
object.
STRING USING ‘NEW ’ KEYWORD
What would be the result after executing the two lines as
mentioned?
STRING USING ‘NEW ’ KEYWORD CONTINUED;
In this case, when we use the new keyword to create
Strings, the compiler will create two separate String
objects that has the same value.
STRING METHODS
STRING METHODS
• length – returns the length of the String
• charAt – returns the character located at the String’s specified index
• concat – concatenates two Strings
• substring – returns a part (or substring) of a String
• toLowerCase – converts the uppercase characters in the String to lowercase
• toUpperCase – converts the lowercase characters in the String to uppercase
• toCharArray – returns an array of characters from the String
• contains – checks whether the String contains a given text
• trim – removes any leading or trailing blank spaces from a String
•
ACTIVITY 01
• Create two strings and concatenate them.
• Find the length of a string.
• Extract a substring from a given string.
• Convert a string to uppercase and lowercase.
DRAWBACKS IN USING STRING
• Since String is immutable in Java, whenever we do
string manipulation like concatenation, substring
etc., it generates a new String and discards the older
String for garbage collection.
• These are heavy operations and generate a lot of
garbage in heap.
DRAWBACKS IN USING STRING CONTINUED;
• So, Java has provided two classes for string
manipulation
• StringBuffer class
• StringBuilder class
WHAT IS STRINGBUILDER CLASS?
StringBuilder is a class in the Java API that provides a mutable
sequence of characters. It is used for dynamic string
manipulation, such as building strings from many smaller
strings or appending new characters to an existing string.
WHAT IS STRINGBUFFER CLASS?
A string buffer is like a String , but can be modified. At any point
in time it contains some particular sequence of characters, but
the length and content of the sequence can be changed
through certain method calls.
STRINGBUILDER AND STRINGBUFFER
• StringBuffer and StringBuilder are mutable objects in java and provide
append, insert, delete, substring etc. methods for string manipulation.
• It is not recommended to use String in a multi-threaded environment.
• StringBuilder is similar to StringBuffer except thread safety and
synchronization.
• In a single-threaded environment, it is better to use String or
StringBuilder. In a multi-threaded environment, it is better to use
StringBuffer.
STRING VS. STRINGBUFFER VS. STRINGBUILDER
USAGE OF STRINGBUFFER AND STRINGBUILDER
• If a string is going to remain constant throughout the program, then
use an object of the String class because a String object is
immutable.
• If a string can change (example: lots of logic and operations in the
construction of the string) and will only be accessed from a single
thread, using a StringBuilder is good enough.
• If a string can change, and will be accessed from multiple threads,
use a StringBuffer because StringBuffer is synchronous so you have
thread-safety.
ANY QUESTIONS?