If you remember, we had used String as data type earlier. Actually String is a class. It is used to store sequence of characters. Java String class provides a lot of options to perform operations on strings such as compare, concat, split, find length, replace, etc.
See the codes below:
In first code, we created array and then created object with that variable. In Second code, we directly created object of String class with actual data.
Sounds confusing? Don't worry we will learn array, class, object and method in detail later.
And in third code, it is easy right, same as we learnt before. We have too many methods to do operation in String. We will talk about that here.
String Length
var.length()
With the help of this method, we can find the length of our String.
Uppercase and Lowercase
var.toUpperCase() and var.toLowerCase()
With the help of this method, we can change case of our String.
Joining Strings
var1.concat(var2) and var1 + " " + var2
With the help of this method, we can join two srtings. Note that this will not update existing variable. It will be joined for that operation only. It is also called String Concatenation.
" " for space in between two Strings.
These are some basic String methods. We have many more String methods in java. Click here to read them in details.
Test it yourself
Code:
Output:
Type Casting in Java Previous Next Math in Java
