String in Java 080234
String in Java 080234
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)
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.
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
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:
7
StringTokenizer
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.
Syntax:
public StringJoiner(CharSequence delimiter)
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
If you want to store this string in the constant pool then you will need to “intern” it.
Example:
Note: String objects are stored in a special memory area known as string constant pool.
12
Machine Problems - String