Data types specify whether the data are numbers (integer, double float), characters (char), strings (string), truth values (boolean), or other.
Strings consist of words, characters, digits, or a combination thereof, delimited from program code by double apostrophes.
The length of a String is the number of characters it consists of. The individual characters of a string of length n are numbered from 0 to n-1. As an alternative to strings, you could also work with arrays of characters.
The data type String in Java is not a simple data type like int or double or boolean. It is a class of its own. A variable of type string therefore does not contain the string itself, but it contains a reference to an object of the class string.
String string = "hello world";
// equivalent to
char data[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };
String str = new String(data);
This code example demonstrates the use of String data type and some of its commonly used functions.
The program first defines a string variable “string” with the value “hello world”.
Then it creates a character array “data” with the characters ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ and converts it to a string object “str” using the constructor new String(data)
.
It then creates another string “concatenate” by concatenating the two strings “str” and “string”.
In the System.out.print
statement, the program showcases various functions that can be performed on strings, such as:
string.length()
which returns the length of the stringstring.charAt(6)
which returns the character at the 6th index of the stringstring.indexOf('o')
which returns the first index of the character ‘o’ in the stringstring.substring(6)
which returns a substring from the 6th index to the end of the stringstring.toLowerCase()
which returns the string in lowercasestring.toUpperCase()
which returns the string in uppercasestr.concat(" world")
which concatenates the string ” world” to the end of the string “str”str.replace('e', 'a')
which replaces all occurrences of the character ‘e’ in the string “str” with the character ‘a’import java.util.Scanner;
public class DataTypeString {
public static void main(String[] args) {
String string = "hello world";
char data[] = { 'h', 'e', 'l', 'l', 'o'};
String str = new String(data);
String concatenate = str + string;
System.out.print("string.length(): " + string.length() + "\n" +
"string.charAt(6): " + string.charAt(6) + "\n" +
"string.trim(): " + string.indexOf('o') + "\n" +
"string.substring(): " + string.substring(6) + "\n" +
"string.toLowerCase(): " + string.toLowerCase() + "\n" +
"string.toUpperCase(): " + string.toUpperCase() + "\n" +
"str.concat(): " + str.concat(" world") + "\n" +
"str.replace('e', 'a'): " + str.replace('e', 'a') + "\n" +
"string + str: " + concatenate);
}
}
string.length(): 11
string.charAt(6): w
string.trim(): 4
string.substring(): world
string.toLowerCase(): hello world
string.toUpperCase(): HELLO WORLD
str.concat(): hello world
str.replace('e', 'a'): hallo
string + str: hellohello world