GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Java Strings - GeeksforGeeks Java StringsLast Updated : 16 Oct, 2026 A String in Java is an object used to store a sequence of characters enclosed in double quotes. It uses UTF-16 encoding and provides methods for handling text data.
Example:
![]()
Output Geeks Explanation: The program creates a String object containing Ways Of Creating a Java StringThere are two ways to create a string in Java: 1. String literal (Static Memory)A string literal is created by assigning a sequence of characters directly to a String variable using double quotes. Java stores string literals in the String Constant Pool, allowing identical string values to share the same object. Example:
2. Using new keyword (Heap Memory)Using the new keyword creates a new object in heap memory, even if the same string already exists in the pool.
Example:
Interfaces and Classes in StringsCharSequence InterfaceThe CharSequence interface represents a sequence of characters in Java. It provides common methods such as length(), charAt(), subSequence(), and toString() for working with character sequences. Common classes that implement CharSequence include:
Immutable StringIn 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.
Output Hello Explanation: In the above example, the String.concat() does not modify the original String object. When str.concat(" World") is executed:
![]() How Strings are Stored in Java MemoryString literalWhen a String is created using a string literal, the corresponding String object is stored in the String Pool, which is part of the heap. The reference variable refers to that pooled String object. Example 1: Using String literals to assigning char sequence value.
![]() Example 2: When we initialize the same char sequence using string literals.
![]() Using new KeywordStrings can also be created using the new keyword, which allocates a new object in heap memory. However, the string literal inside it is still stored in the String Constant Pool (if not already present). Example 1: Using new keyword to assign a char sequence to a String object.
![]() The Example 2: Using .intern() to add a string object in string constant pool.
When a String is created using a string literal, the corresponding String object is stored in the String Pool. When String PoolThe String Pool is a special area in heap memory where Java stores string literals. It helps save memory by reusing existing string objects when the same literal is used multiple times. For example: String s1 = "Geeks"; Here, Java can reuse the same "Geeks" string from the String Pool for both references. For detailed information about String Pool memory management and its behavior across Java versions, refer to the dedicated String Pool Java article. Let us have a look at the concept with a Java program and visualize the actual JVM memory structure:
Output TAT TAT TAT TAT Explanation: ![]()
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |