Interview question for SDET 2023?
SDET Interview Question and Answers.
Jenkins Interview Questions and Answers.
Appium Interview Questions and Answers
Selenium Interview Questions and answers.
GIT Interview Questions and Answer
Pre-requisite: How to install JDK and set Java Home path in Windows and MacOs ?
Ans: Click here for Steps
In Java, a string is a sequence of characters. It is a data type used to
store text-based information, such as names, addresses, and messages.
Strings are represented by the class "String" in Java.
Why String is Immutable?
In Java, a string is immutable, meaning its value cannot be changed once it is created. Once a string object is created, its value cannot be altered.
The reason for this is to ensure that string objects are thread-safe and to improve performance. Since strings are widely used in Java, immutable strings help to prevent errors that can occur due to changes made to the string value by different threads simultaneously. This ensures that the value of the string remains constant throughout the program execution.
Also, immutable strings can be optimized by the Java Virtual Machine (JVM) for memory usage. Since immutable strings cannot be changed, the JVM can reuse existing string objects, rather than creating new ones each time a modification is made. This can result in better performance and reduced memory usage.
In summary, immutability of strings in Java is a design decision made for the purposes of thread-safety, performance, and memory optimization.
String Manipulation
public class StringManipulation {
public static void main(String[] args) {
// Strings are immutable
String str3 = "java";
str3.concat("oops");
System.out.println(str3); // java
// The result should be assigned to a new reference variable (or same
// variable) can be reused.
String concat = str3.concat("value2");
System.out.println(concat); // value1value2
// -----------------------String methods-------------------------------------
String str = "abcdefghijk";
// char charAt(int paramInt)
System.out.println(str.charAt(2)); // prints a char - c
// String concat(String paramString)
System.out.println(str.concat("lmn"));// abcdefghijklmn
System.out.println("ABC".equalsIgnoreCase("abc"));// true
System.out.println("ABCDEFGH".length());// 8
// String replace(char paramChar1, char paramChar2)
System.out.println("012301230123".replace('0', '4'));// 412341234123
// String replace(CharSequence paramCharSequence1, CharSequence
// paramCharSequence2)
System.out.println("012301230123".replace("01", "45"));// 452345234523
// All characters from index paramInt
// String substring(int paramInt)
System.out.println("abcdefghij".substring(3)); // defghij
// 0123456789
// All characters from index 3 to 6
System.out.println("abcdefghij".substring(3, 7)); // defg
// 0123456789
System.out.println("ABCDEFGHIJ".toLowerCase()); // abcdefghij
System.out.println("abcdefghij".toUpperCase()); // ABCDEFGHIJ
System.out.println("abcdefghij".toString()); // abcdefghij
// trim removes leading and trailings spaces
System.out.println(" abcd ".trim()); // abcd
}
}
What are the two ways to create String?
There are two ways to create a string in Java:
- Using String literals - This is the most common and simple way of creating a string in Java. A string literal is a sequence of characters enclosed in double quotes. For example:
String str1 = "Hello World";
String str2 = "Java is awesome";
In this method, a new string object is created each time a string literal is assigned to a variable.
- Using String constructor - Another way of creating a string in Java is by using the constructor of the String class. This constructor takes a character array as its argument and creates a new string object with the same character sequence. For example:
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
In this method, we create a character array and pass it as an argument to the String constructor. This creates a new string object with the same character sequence as the character array.
It is important to note that the second method of creating a string using a constructor is less common than the first method, as it is more verbose and requires more steps.
*******************************************************************
****************************************