Java strings

Strings are objects. By defining Strings you have access to different standard methods to manipulate.

You can create Strings in two ways. The common and most used way is the second one:

  1. String str = new String (“I feel good”);
  2. String str = “I feel good”;


Strings are immutable

A string cannot be changed once it has been instantiated, for example:

String str = “Hi”;
str = “great”;

It seems that the first str is the same as the second one, but in fact they are two different objects. Strings are immutable, because that way is faster than making it dynamic.


Methods of String

The String object has many methods which can be used to return information a bout the string or to change it. The following methods are very important for every programmers. That is why we pay more attention to them by giving some exercises below.


Method Description
length() Returns an int of the number of characters
toUpperCase () Returns a new String with with all uppercase letters
toLowerCase () Returns a new String with all lowercase letters
equals () Returns true if the Strings have the same length and same characters (case sensitive)
equalsIgnoreCase () Same as equals(), but not case sensitive
charAt () Retrun the character at the index
indexOf () Returns the index of the first or last occurrence of a character of substring
lastIndexOf ()
substring () Returns a substring from a String
trim() Returns a new String with leading and trailing white space removed



String str = “Welcome To!”;
String str2 = “Welcome To!”;
String str3 = “Welcome to!” ;

  • str.equals(str2) returns “true”.
  • str.equals(str3) returns “false”. Case sensitive
  • str.equalsIgnoreCase(str3) returns “true”. They are the same. The method ignores case sensitivity.
  • str.length() returns “24”.
  • str.toUpperCase() returns “WELCOME TO MEGISOFT.COM!”.
  • str.toLowerCase() returns “welcome to!”.
  • str.charAt(8) returns “T”.
  • str.indexOf(‘m’) returns “5”. indexOf starts with 0.
  • str.lastIndexOf(‘e’) returns “12”. The index of the last ‘e’ is 12.
  • str.substring(11) returns “!”.

If you want to return only “Megisoft from the string, then you need to pass two parameters to the substring method

  • str.substring(11, 19) returns only “Megisoft” without “.com”.
  • The trim() method removes white spaces from the string
  • String str4 = ”     Megisoft     “;
  • str4.trim() returns “Megisoft”.


Exercise 1

Which one of the following options removes only the link spam “” from the comment?

String str = “Please visit my website”

Select the correct answer.



Exercise 2 : Java String Objects

What happens when you try to compile and run the following program?

class MyString {

	public static void main(String[] args){
		String str1 = "str";
		String str2 = new String("str");
		if(str1 == str2) {
		if(str1.equals(str2)) {
		else {

Select the correct answer.

Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!


Add a Comment
  1. I never more understand about Java String. Because appear more many number
    In METHOD OF STRING. Can you explain the number 24 coming for?

    Ex:str.length() returns “24”.
    str.toUpperCase() returns “WELCOME TO MEGISOFT.COM!”.
    str.toLowerCase() returns “welcome to!”.
    str.charAt(8) returns “T”.
    str.indexOf(‘m’) returns “5”. indexOf starts with 0.
    str.lastIndexOf(‘e’) returns “12”. The index of the last ‘e’ is 12.

    1. The string str represents String str = “Welcome To!”; and the text contains 24 symbols including white spaces!

  2. Your content is great, I am addicted to your quizzes 🙂

  3. Awesome website Sar, I love your method!

  4. Hi Sar! Can I find the answers of the DZone quizzes here?

    1. All the quizzes that I publish on DZone are exclusively designed for them. You can find all the answers if you visit my page on

Leave a Reply