What are Strings in Java?

In this tutorial, we will look into What are Strings in Java. We will learn and understand what strings are as well as learn how to implement them in a Java program.

What are Strings in Java?

A String is a sequence of characters that is used to represent a word or a sentence. A String is formed of characters that can be a mixture of alphabets, integers, or special characters. Each String must be enclosed between a pair of double-quotes.

What are Strings in Java

In Java, a String is an object which contains a sequence of characters and functions that can be used on that string.
Example of a String: “hello”, “I am a human”, “One 2 Three”, etc.

Syntax

<String_Data_Type> <variable_name> = "<sequence of characters>";
or
Sting <variable_name> = new String(<Sequence of characters>);

Examples

String s1 = "This is UseMyNotes";
String s2 = new String("This is UseMyNotes");

Program

class Program {
    public static void main(String agmts[]) {
        String name = "Use My Notes";
        System.out.println("This is" + name);
    }
}

/* Output
This is Use My Notes
*/

String methods

There are built-in String methods available in a String object that can come to use, some of these methods are

The charAt(int index) method

This method is used to get the character at a specified index of the given string. The index value passed should be a non-negative integer number.
Returns: A character of the specified position.

Example:

String s = "ABCDEFG";
char c = s.charAt(4);
System.out.println(c);
//E

The equals(Object o) method

This method can be used to compare two strings to check whether they are equal or not.
Returns: true is both the strings are equal, else returns false.

Example:

String s1= "hello";
String s2 = "hello";
String s3 = "xyz";
Boolean c1 = s1.compare(s2);
Boolean c2 = s1.compare(s3);
System.out.println(c1);
System.out.println(c2);
// true
//false

The length() method

This method is used to get the length of the string.
Returns: Length of the given String in int.

Example:

String s1= "this is a sample line";
int len = s.length();
// len = 21

The isEmpty()

This method is used to check whether the size of the string is 0 or not.
Returns: true if the string has 1 or more characters, else returns false.

Example:

String s1= "welcome";
String s2 = "";
Boolean c1 = s1.isEmpty()
Boolean c2 = s1.isEmpty();

System.out.println(c1);
//false

System.out.println(c2);
//true

The toUpperCase() method

This method is used to convert a given string to its uppercase form. It can only convert alphabets into uppercase alphabets because it doesn’t make sense to convert special symbols into uppercase form.
Returns: The string the upper case string type.

Example:

String s1= "usemynotes";
String upper = s1.toUpperCase();
System.out.println(upper);
// USEMYNOTES

The toLowerCase() method

This method is used to convert a given string to its lowercase form. Just like toUpperCase() method, it can only convert alphabets into uppercase alphabets because it doesn’t make sense to convert special symbols to uppercase form.
Returns: The lowercase form of the given String.

Example:

String s1= "USEMYNOTES";
String lower = s1.toLowerCase();
System.out.println(lower);
// usemynotes

String Conversion

A String can be converted to another primitive data type in Java. For conversion, Java has some built-in methods that are useful for this kind of conversion. Some basic conversions that can be formed using built-in methods are:
String to Integer
A String can be converted to Integer (int) data type if it has only numerical characters in it. No other types of characters are allowed in that string during conversion.
To convert a String to int, we generally use the Integer.parseInt(String) method. This method takes a String as a parameter and returns the integer equivalent of the given String.
Example:

String numString = "42523";
int n = Integer.parseInt(numString);

String to Long

A String can also be converted to a Long data type just like the way we converted String to Integer. For the conversion, we generally use the Long.parseLong(String) method. This method takes a String as an input (which should only contain numerical characters) and returns the Long equivalent of the String.

Example:

String numString = "64535";
Long n = Long.parseLong(numString);

String to Float

A String can be converted to Float data type using the built-in Float.parseFloat(String) method. This method takes a String as an argument and returns the Float equivalent of the String given.

Example:

String numString = "3668.53";
float n = Float.parseFloat(numString);

String to Double

Just how we convert a String to a Float, we can also convert it into a Double data type. For the conversion, we generally use the Double.parseDouble(String) method. It takes a String as an argument and returns the Double equivalent of the String.

Example:

String numString = "3763.44";
double n = Double.parseDouble(numString);