Java Tutorials
Java Methods
Java Classes
Java File Handling
Strings are used to store text.
String
variable contains a set of characters surrounded by two quotes:
Create a variable of type String
and assign it a value:
String greeting = "Hello";
String in Java is actually an object, containing methods that can perform certain functions in a character unit. For example, the length of a character unit can be found in the form of a length()
:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
There are many character unit modes available, for example toUpperCase()
and toLowerCase()
:
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs "hello world"
The indexOf()
method returns the index (location) for the first occurrence of a specific text in a series (including a white space):
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
Java calculates positions from zero.
0 is the first place in the series, 1 is the second, 2 is the third ...
Operator +
can be used between wires to connect them. This is called concatenation:
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
Note that we have added blank text ("") to create space between firstName and printed surname.
You can also use the concat()
method to connect two strings:
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
Because character units have to be written between quotes, Java will not fully understand this series, and will produce an error:
String txt = "We are the so-called "Vikings" from the north.";
The solution to avoiding this problem is to use the backslash escape character.
The escape backslash (\
) converts special characters into the characters of a character unit:
Escape character | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
The sequence \"
includes double quotes in the series:
String txt = "We are the so-called \"Vikings\" from the north.";
The sequence \'
includes one quote in the character unit:
String txt = "It\'s alright.";
Sequence \\
includes one backslash in the series:
String txt = "The character \\ is called backslash.";
Six other escape sequences are allowed in Java:
Code | Result | |
---|---|---|
\n | New Line | |
\r | Carriage Return | |
\t | Tab | |
\b | Backspace | |
\f | Form Feed |
WARNING!
Java uses +
operators in both integration and integration
Numbers added. The wires are connected.
If you add two numbers, the result will be a number:
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer/number)
If you add two strings, the result will be a combination of characters:
String x = "10";
String y = "20";
String z = x + y; // z will be 1020 (a String)
If you add a number and a character unit, the result will be a combination of the character unit:
String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)