DEV Community

Cover image for Basics of Java #6

Basics of Java #6

chathurashmini profile image Chathu Rashmini Updated on ・4 min read

Hi I think now you all are familiar with data types and variables. Therefore, I thought of focusing on Strings in this article.


As I have mentioned in the previous article, String is a reference data type. However, it is predefined by java. Therefore unlike other reference data types, we do not need to use new operator to allocate memory for String variables.

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

You can simple store a value to a String variable as shown above. Nothing is difficult. But there are more interesting things about Strings. Since it is a reference type, strings have their own predefined methods that they can access by using the dot operator. Let's go through some of them.

You may want to know whether a particular string starts with a particular letter. For an example assume you are keeping details of a set of employees and you want to fetch the details of the employees having names starting with 'E'. So how can you know that?

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

Yes, it is simple as that. But does the both println lines gave the same output?
alt 1_startsWith

As you can see they do not give same output. That is because the case sensitivity is under consideration here.

Just like that you also can check the ending letter as well.

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

You also can get the length of a string. Here length means 'number of characters' that a particular string has.

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

This will give the output as '13'. Notice that the whitespace in between 'Edward' and 'Cullen' has also been considered as a character.

Have you heard of the word 'index'? In strings the position of a particular character is said to be the index of that character in the given string. Though we start counting from '1' indexes in java starts with '0'.

Index 0 1 2 3 4 5 6 7 8 9 10 11 12
String E d w a r d C u l l e n

So to find the index of a particular character in a given string, you can use;

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

This will give the out put '9' and '-1'. Let's discuss why. As you can see there are two 'l's in the string roleName. However the method indexOf only gives us the index of the first occurence of the given character. That is why it gives '9' as the first output. The next letter I have given is 'j'. But you can clearly see that letter 'j' is not present anywhere in the roleName. Therefore the method returns '-1'. I think now you are clear with that. So let's proceed.

Can you guess the output of the following code lines without running them? Give it a try and see.

String roleName = "Edward Cullen";
System.out.println(roleName.replace("Edward", "Alice"));
System.out.println(roleName.replace("l", "d"));
Enter fullscreen mode Exit fullscreen mode

The first line will print "Alice Cullen". That is because the method replace() has replaced the word "Edward" with "Alice". So when you are using the method replace(), inside the parenthesis first you need to write the target word or letter that you want to replace and then write the word or letter of replacement. If I give it as a syntax; stringName.replace(target, replacement).
The second line will print "Edward Cudden". That is because the string roleName has two 'l's. So both of them gets replaced by a 'd'.
However has this replace() method actually replaced the original string roleName? No. By running the third line you will see the output as "Edward Cullen", which is the original string. So what happen here is that the method replace() creates new objects whenever it is called. It does not change the original string. The main reason is that strings in java are immutable (cannot change).

Let's see few more string methods. You can convert a particular string into uppercase (capitalizing) and lowercase as well. Simple use the bellow methods;

String roleName = "Edward Cullen";
Enter fullscreen mode Exit fullscreen mode

The outputs are "EDWARD CULLEN" and "edward cullen". Here also note that the original string will not get changed by any chance.

There's another very useful method called trim(). This is used to remove the unnecessary white spaces that might be at the beginning or the end of a given string. Look at the given example.
alt 2_trimMethod
You can clearly see that the extra white spaces in the original string has been removed by the trim() method.

I think this is enough for this article. However, the methods I have mentioned above are NOT the only methods that are available for Strings. Use dot operator and explore more! You will find it interesting :)


  1. Explore string methods and try to get the followings as outputs for the string that we used in above examples (roleName = "Edward Cullen").

    • ard Cullen
    • ard Cul
    • Edward CuLlen (Notice here the third output only the first 'l' is 'L')
  2. Check whether the substring "war" is present in the string "Edward Cullen".

  3. Get the index of the substring "Cullen" in the string "Edward Cullen".

  4. Given a string userName = "", check whether it is empty or not.

Discussion (2)

chathurashmini profile image
Chathu Rashmini Author

Yes, it's super cool!
And thanks for the comment :)

Forem Open with the Forem app