The String Class in Java is one of the important classes in Java. The String Class is considered to be an Object in Java. Strings are also immutable. That is – the value in String object cannot be changed.
Strings are a series of characters. For example, this is a String in Java:
“Jayanthi”
Strings can be created in multiple ways in Java.
One direct way and simple way is to create it this way:
String s="hello";
Strings can also be created this way:
String s1=new String("Java");
The third way is this way:
char c[]={'J', 'a', 'y', 'a'};
String s2=new String(c);
There are still many more ways that we can create the String Object. For a detailed list of all the methods, do visit this link
Let us next see the most popular methods of the String Class:
char charAt(int index)
This method returns the character at the specified index.
2. boolean equals(Object obj)
This method compares the String to the Object specified and returns ‘true’ if they are same and ‘false’ if they are different
3. int indexOf(int ch)
This method returns the index of the first occurrence of the character specified
4. String substring(int firstindex, int endindex)
This method returns the group of characters specified by the ‘firstindex’ and the ‘endindex’. The ‘endindex’ is not included.
5. int length()
This method returns the length of the String
We can see more about these methods in the following program:
Important points when coding in Java or any other programming language:
- Put a lot of comments in your code so that you can understand it later
- Name the program in an easy to understand way like naming a palindrome program as ‘palin_program
- Give appropriate names to variables and methods and classes so that you can understand them later
These are just a few of the methods in the String class. There are more methods in the String class which are important which can be viewed from this link.
We have seen the String class in this post. Join me for yet another post for this wonderful A2Z journey! 🙂
I’m participating in #BlogchatterA2Z by BlogchatterÂ
Your pointers at the end, on naming the programs, writing comments etc, are very important for anyone starting off coding