How Can We Get Last Characters Of A String In Java?

Introduction

In this article, we will learn about String in Java Programming and how we can get the last characters of the String in Java with example programs.

What is String in Java?

In the Java Programming language, Strings are used to represent a sequence of characters. The string class is used to create the strings in Java.

The string is an object, which represents a sequence of char values. Java platform provides the String class to create the strings.

Java provides various methods to perform the different operations with Strings like "Strings. length()", "String.charAt()", "String.toCharArray()", etc. An array of characters works similarly to a Java string.

Let’s see an example, given below.

public class ExampleStringasCharArray {  
  
    public static void main(String args[]) {  
  
        char[] c = {'c', 's', 'h', 'a', 'r', 'p','c', 'o', 'r', 'n', 'e', 'r'};  
        String s = new String(c);  
  
        System.out.println(s);  
  
    }  
} 

The above code prints the string as the output. The above code generates the following output.

Output Image1

In String

public class ExampleString {  
    public static void main(String args[]) {  
  
        String s1 = "CsharpCorner";  
        String s2 = "CsharpCorner Live";  
  
        System.out.println("First String : " + s1);  
        System.out.println("Second String : " + s2);  
  
    }  
} 

The above code prints the string as the output. The above code generates the following output.

Output Image2

How to create a String object in Java?

In Java, there are two ways to create the object in String.

String Literal

String literal is created by the double quotes(" ") in Java Programming. The example program of string literal is listed below.

public class LiteralStringExample {  
  
    public static void main(String args[]){  
  
        String s = "Csharp Corner";  
        System.out.println("String Literal : " +s);  
    }  
} 

The above program generates the following output.

Output Image3

Explanation

In the example, shown above, each time, when we create a string literal, JVM checks the string constant pool first. If the string is already present in the pool, a reference to the pooled instance is returned. if the string is not present in the pool, a new string instance is created and placed in the pool.

The code example of the String instance is listed below.

public class StringInstanceExample {  
    public static void main(String args[]) {  
  
        String s = "Csharp Corner";  
        String s1 = "Csharp Corner";  
  
        System.out.println("String Literal : " + s);  
        System.out.println("String Literal instance : " + s1);  
  
    }  
} 

The above code generates the following output.

Output Image4

Explanation

In the example, shown above, only one object will be created. First, JVM checks and it will not find any string object with the value "Csharp Corner" in the string constant pool, so it will create a new object. Afterward, it will find the string with the value "Csharp Corner" in the pool. It will not create a new object but it will return the reference to the same object.

Let’s understand with the figure, given below,

String object stored image

String objects are stored in a special memory area, which is called a string constant pool in Java. Java uses the concept of string literal because it makes Java more memory efficient or saves memory.

By new keyword

In the example, JVM will create a new string object in normal heap memory, and the literal "Csharp" will be positioned in the string constant pool. The variable s1 will refer to the object in heap memory. The example program for creating a string using a new keyword is listed below.

public class NewKeywordString {  
  
    public static void main(String args[]) {  
        //creating string by java string literal  
  
        String str1 = "Csharp";  
  
        char a[] = {'W', 'e', 'l', 'c', 'o', 'm', 'e'};  
  
        //converting char array to string  
        String str2 = new String(a);  
  
        //creating java string by new keyword  
        String str3 = new String("Csharp Corner");  
  
        System.out.println(str1);  
        System.out.println(str2);  
        System.out.println(str3);  
    }  
} 

The above program generates the following output.

Output Image5

ExplanatioIn the example, shown above, we create the string by Java string literal and by new keyword.

Note. For a detailed description of String Class and methods of String class please Click Here.

How can we get the last character of a String in Java?

Now, we will learn how can we get the last character of String in Java Programming. In Java Programming, there are different ways to retrieve the last character of the String,

By call charAt() Method

If we want to get the last character of the String in Java, we can perform the following operation by calling the "String.chatAt(length-1)" method of the String class. For example, if we have a string as str="CsharpCorner", then we will get the last character of the string by "str.charAt(11)". This method will print the letter "r" as the output. Here is the example program of the charAt() method of String in Java Programming.

public class CharAtExample {  
    public static void main(String arg[]) {  
  
        String str= new String("Csharpcorner");  
        System.out.println("Last character of the String : "+str.charAt(str.length()-1));  
  
    }  
} 

Explanation. In the above code example, first, we create a String object named "str". We store the value of the string as "CsharpCorner". When we print the last character of the string we use the "charAt()" method and the value of the last character is length-1 because the indexing of the string starts from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. If the index argument for the method chatAt() is negative or more than the length of the String then the charAt() method throws "ArrayIndexOutOfBoundsException" The code example of the exception throws by the charAt() method is listed below.

public class ExceptionCharAtExample {  
  
    public static void main(String args[]) {  
  
        String str = "C#Corner";  
        char[] chrs = str.toCharArray();  
  
        char firstChar = chrs[0];  
        System.out.println("First letter : " + firstChar);  
  
        //Exception  
        char exception = chrs[-1];  
        System.out.println("First letter : " + exception);  
        System.out.println("First letter : " + firstChar);  
  
    }  
  
} 

Explanation. In the above example program, first, we create a string named "str". Then we print the first letter of the String from passing the index 0. Next, we provide the index "-1" that prints an exception ". The above program generates the following output.

Output Image6

Another example program for retrieving the last character of the String is in Java Programming. The code example is listed below.

public class LastCharacterExample {  
  
    public static void main(String args[]) {  
  
        String str = new String("Welcome");  
        System.out.println("String : " + str);  
  
        int n = str.length();  
        System.out.println("Length of the String : " + n);  
  
        char last = str.charAt(n - 1);  
        System.out.println("Last Character : " + last);  
          
    }  
  
} 

The above program generates the following output.

Output Image7

Explanation. In the above example program, first, we created a String object as "Welcome". Then we found the length of the String and stored that value in an int variable "n". At last, we printed the value of "n-1" as the output and we found the "e" character as the last character of the String.

Summary

In this article, we learned about the String class in Java Programming, how to create a string, and how to get the last character of the string with different example programs.