String type and Literals
The String class represents character strings.
A quoted string constant can be assigned to a String variable.
public class Main{
public static void main(String[] argv){
String str = "this is a test";
System.out.println(str);
}
}
The output:
this is a test
String literals in Java are specified by enclosing a sequence of characters between a pair of double quotes.
The escape sequences are used to enter impossible-to-enter-directly strings.
"\"" is for the double-quote character.
"\n" for the newline string.
For octal notation, use the backslash followed by the three-digit number. For example, "\141" is the letter "a".
For hexadecimal, you enter a backslash-u (\u), then exactly four hexadecimal digits. For example, "\u0061" is the ISO-Latin-1 "a" because the top byte is zero. "\ua432" is a Japanese Katakana character.
Escape Sequence | Description |
---|---|
\ddd | Octal character (ddd) |
\uxxxx | Hexadecimal Unicode character (xxxx) |
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\r | Carriage return |
\n | New line |
\f | Form feed |
\t | Tab |
\b | Backspace |
Examples of string literals are
"Hello World"
"two\nlines"
"\"This is in quotes\""
Examples of string literals are:
public class Main {
public static void main(String[] argv) {
String s = "java2s.com";
System.out.println("s is " + s);
s = "two\nlines";
System.out.println("s is " + s);
s = "\"quotes\"";
System.out.println("s is " + s);
}
}
The output generated by this program is shown here:
s is java2s.com
s is two
lines
s is "quotes"
Java strings must begin and end on the same line.
public class Main {
public static void main(String[] argv){
String s = "line 1
line 2
";
}
}
If you try to comiple this program, the compiler will generate the following error message.
D:\>javac Main.java
Main.java:3: unclosed string literal
String s = "line 1
^
Main.java:4: not a statement
line 2
^
Main.java:4: ';' expected
line 2
^
Main.java:5: unclosed string literal
";
^
4 errors
In Java strings are actually object types.
Arrays of strings
You can define String array as you define the int array. For example:
public class Main {
public static void main(String args[]) {
String str[] = {"one", "two", "three","java2s.com"};
for (int i = 0; i < str.length; i++)
System.out.println("str[" + i + "]: " + str[i]);
}
}
Here is the output from this program:
str[0]: one
str[1]: two
str[2]: three
str[3]: java2s.com
Java Book
Essential Classes
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String