The String Data Type in Java Programming Language

In my previous blog, I wrote about the history of Java Programming Language. After that, I'm going to write about Strings in Java programming language.

The String is a sequence of characters. The char type represents only one character. To represent a string of characters, use the data type called String.

The String class is a pre-defined class in the Java library, just like other classes System and Scanner. However, unlike primitive types, the String type is considered a reference type. In Java, any class can be used as a reference type for a variable. When a variable is declared with a reference type, it becomes a reference variable for that reference object.

Example:- String message = "Welcome to Java";

Here, the message is a reference variable that references a string object with the contents Welcome to Java.

Simple Methods for String Objects

length() --> Returns the number of characters in this string.

charAt(index) --> Returns the character at the specified index from this string.

concat(s1) --> Returns a new string that concatenates this string with string s1.

toUpperCase() --> Returns a new string with all letters in uppercase.

toLowerCase() --> Returns a new string with all letters in lowercase

trim() --> Returns a new string with whitespace characters trimmed on both sides.

Strings in Java are objects, and the methods of a string can only be called from a specific string instance. These methods are known as instance methods. On the other hand, a non-instance method is called a static method, which can be called without using an object. All the methods in the Math class are static methods and are not linked to a specific object instance. You can call an instance method using the syntax: referenceVariable.methodName(arguments), where the method can have many arguments or none. For example, the charAt(index) method has one argument, while some methods have no arguments. You can call a static method using the syntax: ClassName.methodName(arguments). For instance, you can call the pow method in the Math class using Math.pow(2, 2.5).

Reading a String from the Console

To read a string from the console, you can use the next() method on a Scanner object. This method reads a string that ends with a whitespace character. Alternatively, you can use the nextLine() method to read an entire line of text. This method reads a string that ends with the Enter key pressed.

Comparing Strings

Comparison Methods for String Object

equals(s1) --> Returns true if this string is equal to string s1.

equalsIgnoreCase(s1)--> Returns true if this string is equal to string s1; it is case insensitive.

compareTo(s1) --> Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1.

compareToIgnoreCase(s1) --> Same as compareTo except that the comparison is case insensitive.

startsWith(prefix) --> Returns true if this string starts with the specified prefix.

endsWith(suffix) --> Returns true if this string ends with the specified suffix.

contains(s1) --> Returns true if s1 is a substring in this string.

Note:

  1. To avoid input errors, do not use nextLine() after nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or next().

  2. Syntax errors will occur if you compare strings by using relational operators >, >=, <, or <=. Instead, you have to use s1.compareTo(s2).

  3. The equals() method is used to compare two strings and returns true if they are equal, and false if they are not. On the other hand, the compareTo() method is used to order strings lexicographically and returns a value of 0 if the two strings are equal, a positive integer if the first string is greater than the second one, and a negative integer if the first string is less than the second one.

Obtaining Substrings

You can obtain a single character from a string using the charAt method. You can also obtain a substring from a string using the substring method in the String class.

The String class contains the methods for obtaining substrings.

substring(beginIndex) --> Returns this string’s substring that begins with the character at the specified beginIndex and extends to the end of the string.

substring(beginIndex, endIndex) --> Returns this string’s substring that begins at the specified beginIndex and extends to the character at index endIndex – 1.

IfbeginIndexisendIndex,substring(beginIndex, endIndex)returns an empty string with length 0. IfbeginIndex > endIndex,it would be a runtime error.

Finding a Character or a Substring in a String

The String class provides several versions of indexOf and lastIndexOf methods to find a character or a substring in a string.

index(ch) --> Returns the index of the first occurrence of ch in the string. Returns -1 if not matched.

indexOf(ch, fromIndex) --> Returns the index of the first occurrence of ch after fromIndex in the string. Returns -1 if not matched.

indexOf(s) --> Returns the index of the first occurrence of string s in this string. Returns -1 if not matched.

indexOf(s, fromIndex) --> Returns the index of the first occurrence of string s in this string after fromIndex. Returns -1 if not matched.

lastIndexOf(ch) --> Returns the index of the last occurrence of ch in the string. Returns -1 if not matched.

lastIndexOf(ch, fromIndex) --> Returns the index of the last occurrence of ch before fromIndex in this string. Returns -1 if not matched.

lastIndexOf(s) --> Returns the index of the last occurrence of string s. Returns -1 if not matched.

lastIndexOf(s, fromIndex) --> Returns the index of the last occurrence of string s before fromIndex. Returns -1 if not matched.

Conversion between Strings and Numbers

You can convert a numeric string into a number. To convert a string into an int value, use the Integer.parseInt method, as follows:

int intValue = Integer.parseInt(intString);

To convert a string into a double value, use the Double.parseDouble method, as follows: double doubleValue = Double.parseDouble(doubleString);

In this blog post, I have explained the basic concept of String Data Type. I have discussed topics such as simple methods for String Objects, how to read a String from the console, and how to compare, obtain and find a character or a substring in a String. Additionally, I have included a note on important concepts and, I have also discussed conversion between Strings and Numbers.

Thanks