Introduction to Java String – II

HI! Welcome to the 2nd tutorial of the String operations. In this lesson we are going to learn some quick skills needed for you to write a standard java program.

These are really important and please DO NOT SKIP this lesson. If you do it then you won’t be able to understand most of the things in the upcoming lessons. So stay focused to the subject 🙂

Now you can of course make a variable of type String.

See the S in String is capital. You should always start String with a capital S. You can assign almost anything within “ ” marks, in to a String.

You can use the variable to get an output.



bill gates


Always remember you have to declare and assign a value to the variable before calling the print statement because, the computer runs each statement from top to bottom.

If you don’t do that, it will try to print a value that doesn’t exist at the moment, which is, of course, an ERROR!!

That means, if you try to do;

without writing

first, you will get an error.

Or if you try

without writing

in between the two statements, then, an error will occur…

OK. Now I have to tell you another cool fact. You can play with Strings using special methods. Just do this;



What have we done here?? Assigning “bill gates” to the variable name is ok… but in the next statement, we see a new feature. name.toUpperCase().

This is an order that we do to the String variable name. We want all words in the name variable to be capital.

For that, we use a String feature called .toUpperCase() (Note the .dot before “t”).

This feature is called a “method”. We will do about methods extensively in the course but for now, get an idea of it by the following diagram…

A method is something like this;

Method is a kind of magic box or a black box where we put something to it and get something useful. In the above example,

The output “BILL GATES” given out from the method is assigned to a new String variable called upperCaseName.

Actually you don’t need to define a new variable for that. You may directly put that method to the basic output function.

Now here’s another special point:

We can assign the String output to the same variable.

If you feel this a little complicated, never mind 🙂 We’ll certainly do this in detail later.

The right hand side, that is name.toUpperCase(); is going to operate first.

That is, the black box will operate first and give the output string “BILL GATES”.

Step 1:

Then what happens is that output will be assigned to the name variable in the left hand side.

Step 2:



Alright. Now there are many methods for you to experiment. Once you put a dot mark after the name, you may see how many methods are available for you to do some cool stuff…

I’ll give some of them here…

  • toLowerCase() – can you predict what will happen?? Yeh it will change any capital letter in the String to a simple letter and finally you will get all simple String.
  • length() – will return the length of the variable.

Note that this method will return an integer. NOT a String. OK?

  • indexOf(“at”) – returns an integer of the index of the character at the occurrence of the substring “at” in varible of “bill gates”.

In the example, the index is the number relevant to each letter of the word as follows.

b0 ,      i – 1 ,      l2 ,      l3,      (space) -4,      g 5,      a6,      t7,      e8,      s 9

Note that the indexing is starting from 0, NOT 1.

That’s the usual form and, guys, you MUST remember that.

If the substring is not available in the name, -1 is returned.



  • equals(name2) – This method will check whether String of name equals the String of name2 and returns a boolean value, that is, either true or false



obvious 🙂 Isn’t it??

  • equalsIgnoreCase(name2) – From the name itself you can get the idea of what this method does. It just ignores the capital simple difference between two variables and check whether the two Strings are equal.



  • charAt(n) – returns the character at n th index.



If the index is out of range, error will occur and the program will crash 🙁



Exception in thread “main” java.lang.StringIndexOutOfBoundsException:
String index out of range: 500

OK guz. That’s all for the Strings (for now ;). Please experiment with those methods and get familiarize with them!!

In the next tutorial, we are going to review some math. But they are NOT hard at all! Let’s meet in the next tutorial. Bye guys!!