We’ll cover the following
- Strings
- Declaring and initializing strings
- Example
- Java strings combination
- Note
- Java Converting primitives to strings
- Converting strings to primitives
- String Literals
A String is a sequence of text characters, such as the message “Hello, World!”.
Java String is not a primitive type. Instead, Java strings are a reference type defined by the Java API String class.
The Java language does have some built-in features for working with strings.
In some cases, these features make strings appear to be primitive types rather than reference types.
Declaring and initializing strings
Strings are declared and initialized much like primitive types.
The only difference is that the word String is capitalized, unlike the keywords for the primitive types, such as int and double.
String isn’t a keyword. It’s the name of the Java API class that provides for string objects.
Example
The following statements define and initialize a string variable:
String s;
s = "Hello, World!";
Here a variable named s of type String is declared and initialized with the string literal “Hello, World!”.
Notice that string literals are enclosed in quotation marks, not apostrophes(single quote).
Apostrophes are used for character literals, which are different from string literals.
Like any variable declaration, a string declaration can include an initializer.
Thus you can declare and initialize a string variable in one statement, like this:
String s = "Hello, World!";
String typed class variables and instance variables are automatically initialized to empty strings, but local variables aren’t.
To initialize a local string variable to an empty string, use a statement like this:
String s = "";
Java strings combination
Combine two strings by using the plus sign (+) as a concatenation operator.
The following statement combines the value of two string variables to create a third string:
String hello = "Hello, ";
String world = "World!";
String greeting = hello + world;
The final value of the greeting variable is “Hello, World!”.
When Java concatenates strings, it doesn’t insert any blank spaces between the strings.
Note
Thus, to combine two strings and have a space appear between them, make sure that the first string ends with a space or the second string begins with a space.
Alternatively, you can concatenate a string literal along with the string variables. For example:
String hello = "Hello";
String world = "World!";
String greeting = hello + ", " + world;
Here the comma and the space that appear between the words Hello and World are inserted as a string literal.
Concatenation is one of the most commonly used string-handling techniques.
Java Converting primitives to strings
How can Java concatenate the string literal “The value of i is ” with the integer value of i in this statement?:
System.out.println("The value of i is " + i);
The answer is that Java automatically converts primitive values to string values whenever you use a primitive value in a concatenation.
Java can confuse you about when the numbers are converted to strings in the course of evaluating the complete expression.
int i = 2;
System.out.println(i + i + " equals four.");
This prints the following on the console:
4 equals four.
Here, the first plus sign indicates the addition of two int variables rather than concatenation.
For the second plus sign, the resulting int answer is converted to a string and concatenated with ” equals four.”
You can explicitly convert a primitive value to a string by using the toString method of the primitive type’s wrapper class.
To convert the int variable x to a string, for example, you use this statement:
String s = Integer.toString(x);
Converting strings to primitives
Converting a string value to a primitive is a little more complex, because it doesn’t always work.
If a string contains the value 10, for example, you can easily convert it to an integer. But if the string contains “123demo”, you can’t.
To convert a string to a primitive type, you use a parse method of the appropriate wrapper class, as listed in the following table.
Wrapper Parse Method Example
Integer parseInt(String) int x = Integer.parseInt("100");
Short parseShort(String) short x = Short.parseShort("100");
Long parseLong(String) long x = Long.parseLong("100");
Byte parseByte(String) byte x = Byte.parseByte("100");
Float parseByte(String) float x = Float.parseFloat("19.95");
Double parseByte(String) double x=Double.parseDouble("19.95");
Character (none) (none)
Boolean parseBoolean(String) boolean x = Boolean.parseBoolean ("true");
To convert a string value to an integer, you use statements like this:
String s = "10";
int x = Integer.parseInt(s);
Note that there is no parse method to convert a String to a Character.
String Literals
String literals in Java are specified like they are in most other languages—by enclosing
a sequence of characters between a pair of double quotes. Examples of string literals are
“Hello World”
“two\nlines”
“codingtimes”
“\”This is in quotes\””
The escape sequences and octal/hexadecimal notations that were defined for character literals work the same way inside of string literals. One important thing to note about Java strings is that they must begin and end on the same line. There is no line-continuation escape sequence as there is in other languages.
String Character Escape Sequences
Escape Sequence | Description |
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal Unicode character (xxxx) |
\’ | Single quote |
\” | Double quote |
\\ | Backslash |
\r | Carriage return |
\n | New line (also known as line feed) |
\f | Form feed |
\t | Tab |
\b | Backspace |
That’s it!
You have successfully completed the post. Do Share : )
Peace Out!
Also Read – Java Boolean type
Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy
[…] Also Read – Java String and String Literals […]
[…] Java String and String Literals […]