Datatype String

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.

Syntax

String string = "hello world";

// equivalent to
char data[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };
String str = new String(data);

Java String Methods

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 string
  • string.charAt(6) which returns the character at the 6th index of the string
  • string.indexOf('o') which returns the first index of the character ‘o’ in the string
  • string.substring(6) which returns a substring from the 6th index to the end of the string
  • string.toLowerCase() which returns the string in lowercase
  • string.toUpperCase() which returns the string in uppercase
  • str.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’
  • The concatenation of the two strings “string” and “str”, stored in the “concatenate” variable.
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);
	}
}
Output
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