String in Java 9 is a data type just like integer and floating point, but it is used to represent text rather than numbers or a single character.
String variable is comprised of a set of characters that can also contain spaces, numbers and special characters.
For instance, the word “tennis” and the phrase “I play tennis” are an example of string variable. Even a number “5445” could be considered a string in Java, if specified correctly.
In Java, strings must be enclosed in quotation marks for the data to be recognized as a string and not a number or variable name.
Here are some examples of string variables in Java 9:
String a = "tennis"; String b = "I play tennis"; String c = "12345"; String d = "Can we use question marks in strings? Yes!";
If you are not sure how to run Java code, here’s a quick how to write and run Java code.
In Java strings are objects. To declare a string variable in Java, you have to use the keyword ‘String’ followed by the name of your variable. Following line of code will declare a string variable:
Next we assign some value to that string variable:
myVariable = "This is a string";
Here “This is a string” is a string literal – a series of characters that are enclosed in double quotes. It’s mandatory to enclose a string in double or single quotes in order to tell the compiler that it’s a string.
You may place any character inside a Java string, but some of them will require special treatment.
One example is double or single quotes. We use quotes to tell the compiler about the start and the end of the string.
If we use a double quote inside our string wrapped in double-quotes without any special treatment, it won’t compile and give a compilation error “String literal is not properly closed by a double-quote” or “Syntax error on token …, ; expected”:
The characters that require special treatment should be escaped with a backslash: “\”. Java string with quotes inside is such a case. We need to escape double quotes in Java if we have more than 2 of them.
Here’s how to put quotation marks in a string with single and double quotes and a backslash in Java:
String doubleQuotes = "A quotation mark \" inside should be escaped with '\' "; String backslash = "A backslash can be escaped with backslash: \\ ";
Rule of thumb 1: Use escape character only when needed.
Rule of thumb 2: Escape single quotes
\' in a string wrapped in single quotes
' and escape double quotes
\" in a string wrapped in
There are only two special characters in Java strings that must be escaped or the program won’t compile: double quotes and a backslash itself.
There are more characters that can be escaped with a backslash in Java and that will have a totally different behavior as a result.
Here’s a list of Java 9 special characters in strings:
|\’||Single quotes. Only need to escape if a string is wrapped in Single quote as well.|
In order to find a number of characters contained in a string there is length() method.
String finalHour = "We are going"; int Length = finalHour.length();
After these two lines of code will be executed, variable Length will have a value equal to 12. There are 10 characters and two spaces in this string.
There are a number of things you can do with strings in Java: you can split them, format, concatenate, compare, replace, join, trim and so on.
Linking together several strings in Java is called concatenation, there is a method concat().
Let’s say we have two string variables:
String val1= "How are you?"; String val2= "I am Fine";
You may use concat() method either with string variables or directly with hard coded strings:
val1.concat(val2); "Where are you?".concat(" I am Fine");
Output will be a new string “How are you? I am fine”.
Strings can also be concatenated with the + operator. Here is a simple example of joining two strings:
String city = "New York"; String weather = "Hot"; String result = city + " - " + weather; System.out.println(result);
Output of the above code will be: “New York – Hot”.
Senior Software Engineer developing all kinds of stuff.