Strings are integral component of any programming langage, representing sequences of Characters.The String
Class in Java Provides a roboust set of methods and functionalities for working with textual data effeciently.
In String
Class is a part of java.lang
package and is automatically imported into every Java program. This means that you can strings without explicity importing the ‘String’ Class.
String Creation in Java
In Java we can create String in two ways either by using String litterals or by using new
keyword.
Using String Literals
String str = "The Code Data";
In this case, only one Object is created in SCP (String Constant Pool), and “str” will always point to that Object.
Important Points Related to String Creation by Using String Literal in Java
- Object Creation in String Constant Pool is Optional. first, it will check if any object already exists in the string constant pool with the same value. if the object already exists then the existing object will be reused. if an object does not exist, only a new object will be created. This condition only applies to the String constant pool, not the heap area.
- The garbage collector is not allowed to access the String constant pool area.
- All String pool constant objects will be destroyed automatically at the time of JVM shutdown.
Using new Keyword
String str = new String("The Code Data");
In this case, two Object is created one in the Heap Area and another in SCP (String Constant Pool), and “str” will always point to the Heap Object.
Note-
Whenever we are using a new operator compulsory a new object will be created in the Heap area. Hence there may be a chance of existing two or more objects with the same value in the Heap area but it is not possible in the String constant pool.
Constructor of String Class
String str = new String();
This constructor will create an empty String object.
String str = new String(String literals);
This constructor will create a String object for a given String literal.
String str = new String(StringBuffer strBuf);
This constructor will create an equivalent String object for a given StringBuffer.
String str = new String(char[] chArray);
This constructor will create an equivalent String object for a given char array.
String str = new String(byte[] byteArray);
This constructor will create an equivalent String object for a given byte array.
Important Methods for String
chatAt()
public char charAt( @Range(from = 0, to = Integer.MAX_VALUE) int index )
This method will return the character present at the given index.
concat()
public String concat( @NotNull String str )
This method concatenates the given String at the end of the current String.
equals()
public boolean equals( @Nullable Object anObject )
This method is used to perform a String comparison where the case is important.
equalsIgnoreCase()
public boolean equalsIgnoreCase (@Nullable String anotherString )
This method is used to perform a String comparison where the case is not important.
substring()
public String substring( @Range(from = 0, to = Integer.MAX_VALUE) int beginIndex )
This method returns a substring from beginIndex to end of string.
public String substring( @Range(from = 0, to = Integer.MAX_VALUE) int beginIndex,
@Range(from = 0, to = Integer.MAX_VALUE) int endIndex )
This method return a substring from beginIndex to endIndex-1 index.
Java Substring Example
class StringMethod{
public static void main(String[] args) {
String s = "The Code Data";
String subString1= s.substring(4);
String subString2= s.substring(4,8);
System.out.println(subString1);
System.out.println(subString2);
}
}
length()
public int length()
This method returns no of character present in the string.
Note- lenght
is applicable for arrays but not for String Object while lenght()
is applicable for String Object but not for arrays.
replace()
public String replace( char oldChar,
char newChar )
replace method returns a string after replacing oldChar by newChar.
trim()
public String trim()
This method is used to remove blank spaces present at the beginning and end of the string but not to remove blank spaces at the middle.
Note – Because of run time operation if there is a change in the value then with those changes a new object will be created in the Heap Memory. If there is no change in value then the existing object will be reused and no new object will be created whether the object is present in Heap Memory or String Constant Pool (SCP) the rule is samethe .
Similar Java Tutorials
- Odd Even in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Adapter Class in Java
- Method signature in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
- Method Overriding in Java
- Method Overloading in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java
- Threads communication in Java
- Daemon Thread in Java
- Green Thread and Native Thread
- How to Stop a Thread
- Multithreading in Java
- Thread Group in Java
- ThreadGroup methods in Java
- Problems with synchronized keyword in java
- lock Interface in Java
- Reentrant Lock in Java
- Thread Pool in JavaDifference Between Runnable and Callable
- Callable and Future in Java
- Thread Local Class in Java
- Inner Classes in Java
- Method Local Inner Classes in Java
- Anonymous Inner Classes in Java
- Normal Java Class vs Anonymous Inner Class
- Static Nested Class
- java.lang Package
- toString() method in Java
- hashCode Method in Java
- equals Method in Java
1 thought on “String Class in Java”