0% found this document useful (0 votes)
37 views7 pages

Oop - Lesson 6

Java String objects represent a sequence of characters and can be created using either string literals or the String class. String literals are created using double quotes and stored in a string constant pool for efficiency, while the String class uses the new keyword to create strings in the normal heap memory. The string constant pool helps Java be more memory efficient by storing only one instance of identical strings.

Uploaded by

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

Oop - Lesson 6

Java String objects represent a sequence of characters and can be created using either string literals or the String class. String literals are created using double quotes and stored in a string constant pool for efficiency, while the String class uses the new keyword to create strings in the normal heap memory. The string constant pool helps Java be more memory efficient by storing only one instance of identical strings.

Uploaded by

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

Lesson 6

z
Java String
z

Java String
 an object that represents sequence of char values.

 An array of characters works same as Java string

 java.lang.String class is used to create a string object

char[] ch = {'j','a','v','a','t',’s',’t','i','n',’g'};

String s = new String(ch);

is same as

String s = "javatstring";
z

String Literal

new keyword

How to create a string object?


z

String Literal
created by using double quotes

String s = “javastring";

String s1 = “javastring";

String s2 = “javastring";

Each time you create a string literal, the JVM checks the "string
constant pool" first. If the string already exists in the pool, a reference
to the pooled instance is returned. If the string doesn't exist in the pool,
a new string instance is created and placed in the pool.
String objects are stored in a special memory
area known as the "string constant pool".
Why Java uses the concept
of String literal?

To make Java more memory


efficient
z

By new keyword
created by using the new keyword

String s = new String(“javastring“);

JVM will create a new string object in normal (non-pool) heap


memory, and the literal “javastring" will be placed in the string
constant pool. The variable s will refer to the object in a heap (non-
pool)

You might also like