The Java programming language is the result of decades of work by many different developers. Its basic language constructs were created to make writing programs easier and more concise. A few of the language’s constructs were specifically designed to help programmers handle strings.
A string is a sequence of characters. It can be a sequence of words or a sequence of digits. A string in Java is represented as a character array whose length is known. This array contains the characters that make up the string. Variables are subject to the concept of visibility. After they have been created, they are visible from the main method, and when the main method ends, they are dealt with. They don’t have a life beyond the main method, and aren’t accessible from outside of main.
There are two ways to get back to a string after conversion from a string to a double-precision floating-point value.
Convert a String into a java.lang.String
There are many ways to convert a string into a java.lang.String. The main ways are to convert a String into a java.lang.String object. You can do this by simply passing a String object to the String constructor. The following code will print the string “Hello World”.
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string:");
String s = input.nextLine();
System.out.println(s);
}
}
The string is converted to a String object when passed to the String constructor. The String constructor is called as shown below:
import java.util.Scanner;
class HelloWorld {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string:");
String s = input.nextLine();
System.out.println(s);
}
}
The java.lang.String class is an abstract class and is the base class for all string classes. It provides several methods that are used to access and manipulate strings.
A java.lang.String is immutable in nature. It cannot be changed after creation. The java.lang.String class provides a set of static methods to convert a String into a java.lang.String and vice versa. The java.lang.String class provides many useful methods. Some of them are as follows:
- public String getChars() This method returns the character array of the string.
- public char[] toCharArray() This method returns the character array of the string.
- public int length() This method returns the length of the string.
- public char charAt(int index) This method returns the character at the specified position.
- public char charAt(int index) This method returns the character at the specified position.
- public String substring(int beginIndex, int endIndex) This method returns the substring of the string.
- public String toLowerCase() This method converts the string to lower case.
- public String toUpperCase() This method converts the string to upper case.
- public String trim() This method removes the leading and trailing white spaces of the string.
- public boolean equalsIgnoreCase(String str) This method returns true if the two strings are equal ignoring case.
- public boolean equals(Object o) This method returns true if the two objects are equal.
- public int compareTo(String str) This method compares the two strings and returns 0 if they are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second.
- public int hashCode() This method returns a hash code for the string.
- public String toString() This method returns the string representation of the object.
- public char[] toCharArray() This method returns the character array of the string.
- public String substring(int beginIndex, int endIndex) This method returns the substring of the string.
- public String toLowerCase() This method converts the string to lower case.
- public String toUpperCase() This method converts the string to upper case.
- public String trim() This method removes the leading and trailing white spaces of the string.
- public String substring(int beginIndex, int endIndex) This method returns the substring of the string.
- public boolean equals(Object o) This method returns true if the two objects are equal.
- public int compareTo(String str) This method compares the two strings and returns 0 if they are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second.
- public String toString() This method returns the string representation of the object.
- public String toLowerCase() This method converts the string to lower case.
- public String toUpperCase() This method converts the string to upper case.
- public String trim() This method removes the leading and trailing white spaces of the string.
- public boolean equals(Object o) This method returns true if the two objects are equal.
- public int compareTo(String str) This method compares the two strings and returns 0 if they are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second.
- public String substring(int beginIndex, int endIndex) This method returns the substring of the string.
- public String toLowerCase() This method converts the string to lower case.
- public String toUpperCase() This method converts the string to upper case.
- public String trim() This method removes the leading and trailing white spaces of the string.
- public boolean equals(Object o) This method returns true if the two objects are equal.
- public int compareTo(String str) This method compares the two strings and returns 0 if they are equal, -1 if the first string is less than the second, and 1 if the first string is greater than the second.
- public String toString() This method returns the string representation of the object.
- public String toLowerCase() This method converts the string to lower case.
- public String toUpperCase() This method converts the string to upper case.
General Pattern for String Conversions
public class StringConversion {
public static void main (String[] args) {
double pi;
String strval;
pi = Double.parseDouble("3.14");
System.out.println(strval = String.valueOf(pi));
System.out.println(Double.toString(pi));
}
}