What is String in Java?
What is String in Java?
Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
Creating Strings
The normal way to create a string in Java is to simply write:-
String s = “Hello World”
;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way to create a string is:-
String s = new String (“Hello World”);
String Builder:
The char value, which is a one-character string, is represented by a String class object. We can't use an ASCII value in this case.
Syntax:
StringBuilder str = new StringBuilder();
str.append("GFG");
String Buffer:-
StringBuffer is a String companion class that provides a lot of the same functionality as strings. StringBuffer shows growable and writable character sequences, whereas string represents fixed-length, immutable character sequences.
Syntax:
StringBuffer s = new StringBuffer("Hello World");
String Tokenizer:-
The StringTokenizer class in Java is used to tokenize a string.
String Joiner:-
String Joiner is a java.util package class that is used to build a sequence of characters divided by a delimiter and optionally starting with a transferred prefix and finishing with a transferred suffix.
Syntax:
public StringJoiner(CharSequence delimiter);
As we study in the above part, we have sufficient knowledge about replacing a character in a string
So, there are three ways to replace character in a string:
String Builder:
In contrast to the String Class, the StringBuilder class includes a built-in function for this — setCharAt (). By calling this function and giving the character and the index as parameters, you can replace the character at a given index.
What is Switch in Java?
A switch statement makes it possible to compare a variable to a list of values. Each value is referred to as a case, and each case is compared to the variable that is being turned on.
Syntax:
switch(expression) {
case value :
break;
case value :
break;
default :
}
String in Switch:
- In Java, we may utilize a switch statement with Strings. You should keep the following considerations in your mind while doing that.
- A NullPointerException will be produced if the argument in the switch cases is null during Run-time.
- If the data you're interacting with is also Strings, it's best to utilize String values in a switch statement.
- The case is taken into account when comparing Strings in a switch statement. i.e., the String you provided and the String in the case must be similar and in the same upper or lower case.
Example:
public class SwitchStringExample {
public static void main(String[] args) {
printColorUsingSwitch("Green");
printColorUsingIf("Green");
printColorUsingSwitch("Green");
printColorUsingSwitch(null);
}
private static void printColorUsingIf(String color) {
if (color.equals("yellow")) {
System.out.println("yellow");
} else if (color.equals("Green")) {
System.out.println("Green");
} else {
System.out.println("INVALID COLOR ");
}
}
private static void printColorUsingSwitch(String color) {
switch (color) {
case "yellow":
System.out.println("yellow");
break;
case "green":
System.out.println("Green");
break;
default:
System.out.println("INVALID COLOR ");
}
}
}
OUTPUT:
Green
Green
INVALID CODE
KEY POINTS:
- By eliminating several if-else-if linked conditions, the Java switch case string makes code better understandable.
- The output of the above example indicates that the Java switch case string is case-sensitive.
- To prevent a NullPointerException, the Java Switch case employs the String.equals() function to match the provided value with case values, therefore make sure to include a NULL check.
- In a Switch statement, the Java compiler produces more effective byte code for String than chained if-else-if expressions.
Example 2:
public class GFG {
public static void main(String[] args)
{
String str = "Hello";
switch (str) {
// Case 1
case "World":
System.out.println("World");
break;
// Case 2
case "Hello":
System.out.println("Hello");
break;
// Case 3
case "UMAR":
System.out.println("UMAR");
break;
// Case 4
// Default case
default:
System.out.println("no match");
}
}
}
OUTPUT:
Hello
Example 3:
public class GFG {
public static void main(String[] args)
{
String str = "";
switch (str) {
// Case 1
case "one":
System.out.println("FIRST");
break;
// Case 2
case "two":
System.out.println("SECOND");
break;
// Case 3
case "three":
// Print statement corresponding case
System.out.println("THIRD");
break;
// Case 4
// Default case
default:
// Print statement corresponding case
System.out.println("NO MATCH");
}
}
}
OUTPUT:
NO MATCH