1. What is a String in Java
String can be defined as a sequence of characters i.e ‘Hello World” can be called String because it is made by sequence of characters.
In Java, a string is an object that represents a sequence of characters. It is a fundamental data type used to store and manipulate textual data. Strings in Java are instances of the java.lang.String class, which is a part of the Java Standard Library.
2.How to Declare String in java
In java String can be declare by two ways
- By new Keyword
- By String literal
By new Keywork
Java String is created by using a keyword “new”. With new keyword an Object of type String class is created inside memory.
String s=new String (“Hello World”) in this line of code new keywork create object of class name String and Pass “Hello World” String to its constructor, a constructor create object in side heap memory and reference of that object is saved inside variable name s of type String.
In above picture new keyword create String class object in heap memory area having initialized value is “Hellow World” and its memory address is stored inside String variable name s which is created on Stack Memory Area.
String s= “Hello World” in this line of code a String “Hello World” is created on String pool area inside Heap Memory Area and its reference is saved inside variable name s of type String.
In the above picture “Hello World” String is created inside String Pool Area of Heap Memory Area and its Reference is Stored inside variable name s of Type String which is created on Stack Memory Area.
String is immutable while StringBuffer and StringBuilder is mutable, when we say immutable than it’s mean we can not modify the Content of the Variables at run time.
When this line String s=”Hello World”; execute a “Hello World” String created in String pool area inside Heap Memory Area and reference is saved inside variable name s whose type is string, after this when s=”how are you”; line execute by compiler than a new string “how are you” is created on String pool area and its reference is saved inside s variable, now s variable is pointing out on newly created string “how are you” and old string “Hello World” is dereferenced and will clean out by garbage collector routine.
In mutable every time when you modify the string there is no need to crate new string on Heap memory area, it’s just modified old content with new once.
When StringBuffer sb=new StringBuffer(“Hello “); line is executed a String “Hello “ is created on Heap Memory Area and reference is stored inside variable sb of type StringBuffer, now after that when sb.append(“World”) line
executed than String “World” is concatenated with String Hello on Heap Memory Area.
4.How to concatenate String in java
- When we create String with new keywork using classes StringBuffer or StringBuilder than we use append method of these class to concatenate the two strings.
- When we create string using string literal then there are 2 methods to concatenate string
when we concatenate String using + operator or concate() function a new string “Hellow World” is created inside String pool Area and its reference is saved inside variable name s3 of type String created on Stack Memory Area.
5.What is the String class’s equals () method
equals () method belong to String class, which is used to compare the content of two string variables.
In the above code s1 variable and s2 variable both containing String Hello and equals() method compare the contents of both variables in side if condition which is true because both contains String Hello, so if condition become true and “s1 contents are equal to s2” message printout on the console.