0% found this document useful (0 votes)
15 views

String in Java 080234

Uploaded by

serinarusselfroi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

String in Java 080234

Uploaded by

serinarusselfroi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

STRINGS in JAVA

Rizal Technological University – Mandaluyong


What are Strings in Java?

Strings are the type of objects that can store the Below is an example of a String in Java:
character values in Java, every character is
stored in 16 bits. A string acts the same as an
array of characters in Java.

Example:
String name = “COMPUTER";

2
Ways of Creating a String

There are two ways to create a string in Java: 2. USING NEW KEYWORD
 String Literal  String s = new String(“Welcome”);
 Using new Keyword  In such a case, JVM will create a new string
object in normal (non-pool) heap memory
Syntax: and the literal “Welcome” will be placed in
<String_Type> <string_variable> = the string constant pool. The variable s will
"<sequence_of_string>"; refer to the object in the heap (non-pool)

1. STRING LITERAL Example:


To make Java more memory efficient (because no new
objects are created if it exists already in the string String demoString = new String
constant pool). (“Computer Engineering”);

Example:
String demoString = “ComputerDepartment”; 3
Interfaces and Classes in Strings in Java

CharBuffer: This class implements the CharSequence interface. This class is used to allow character
buffers to be used in place of CharSequences. An example of such usage is the regular-expression
package java.util.regex.

String: It is a sequence of characters. In Java, objects of String are immutable which means a constant
and cannot be changed once created.

CharSequence Interface: CharSequence Interface is used for representing the sequence of


Characters in Java. Classes that are implemented using the CharSequence interface are mentioned
below and these provides much of functionality like substring, lastoccurence,
firstoccurence, concatenate , toupper, tolower etc.
 String
 StringBuffer
 StringBuilder
4
CharSequence Interface

1.String
String is an immutable class which means a constant and cannot be changed
once created and if wish to change , we need to create an new object and even
the functionality it provides like toupper, tolower, etc all these return a
new object , its not modify the original object. It is automatically thread safe.

Syntax

String str= “computer";


or
String str= new String(“computer")
5
CharSequence Interface

2. StringBuffer
StringBuffer is a peer class of String, it is mutable in nature and it is thread safe
class, we can use it when we have multi threaded environment and shared object
of string buffer i.e, used by multiple thread. As it is thread safe so there is extra
overhead, so it is mainly used for multithreaded program.

Syntax:
StringBuffer demoString = new stringBuffer(“ComputerDepartment");

6
CharSequence Interface

3. StringBuilder
StringBuilder in Java represents an alternative to String and StringBuffer
Class, as it creates a mutable sequence of characters and it is not thread safe. It is
used only within the thread , so there is no extra overhead , so it is mainly used
for single threaded program.

Syntax:

StringBuilder demoString = new StringBuilder();


demoString.append("GFG");

7
StringTokenizer

StringTokenizer class in Java is used to break a string into tokens.

Example:

8
CharSequence Interface

A StringTokenizer object internally maintains a current position within the string to be tokenized.
Some operations advance this current position past the characters processed. A token is returned by
taking a substring of the string that was used to create the StringTokenizer object.

StringJoiner is a class in java.util package which is used to construct a sequence of


characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with
a supplied suffix. Though this can also be done with the help of the StringBuilder class to append a
delimiter after each string, StringJoiner provides an easy way to do that without much code to write.

Syntax:
public StringJoiner(CharSequence delimiter)

Above we saw we can create a string by String Literal.

String demoString =”Welcome”;


9
Immutable vs. Explicitly Assigned String
in Java
In Java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once a string object is
created its data or state can’t be changed but a new string object is created.

Here “Computer” is not changed but a new object is created with “Computer Engineering”. That is why a string is
known as immutable.

As you can see in the given figure that two objects are created but s reference variable still refers to “Computer” and not to
“Computer Engineering”.
But if we explicitly assign it to the reference variable, it will refer to
the “Computer Engineering” object.

10
Memory Allotment of String

The string can also be declared using a new operator i.e.


Whenever a String Object is created as a literal, the
dynamically allocated. In case of String are
object will be created in the String constant pool. This
dynamically allocated they are assigned a new memory
allows JVM (Java Virtual Machine) to optimize the
location in the heap. This string will not be added to the
initialization of String literal.
String constant pool.
Example:
Example:
String demoString = “Computer";
String demoString = new String(“Computer");

If you want to store this string in the constant pool then you will need to “intern” it.

Example:

String internedString = demoString.intern();


// this will add the string to string constant pool. 11
It is preferred to use String literals as it allows JVM to optimize memory allocation.
Memory Allotment of String

An example that shows how to declare a String

Note: String objects are stored in a special memory area known as string constant pool.
12
Machine Problems - String

Create a Java application in GUI and Console:

1. That will identify the length of the given string


Sample Output:
The string length of “Computer@Department” is: 19
Filename: MP_String1_GUI.java & MP_String1_Con.java

2. That will replace a specified character with another character.


Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown fox jumps over the lazy fog.
Filename: MP_String2_GUI.java & MP_String2_Con.java

3. That converts all characters in a string to lowercase.


Sample Output:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
Filename: MP_String2_GUI.java & MP_String2_Con.java
13

You might also like