Strings Of Java – Usege And Example


Strings are a crucial part of any programming language, including the string of Java. We use Strings to represent and manipulate text-based data, which is a large portion of the data we work with on a daily basis.

Let’s illustrate this with a real-world example:

Imagine you’re creating a social media application. Users need to enter their username, password, bio, and posts – all of which are textual data and would naturally be represented as Strings in Java.

Let’s say a user, Roshan, signs up for the application. He enters his username, Roshan123. Behind the scenes, in your Java-based server, you’d create a String object to store this.

What Make Up The String Of Java?

A String is a class in Java which represents a sequence of characters. It’s essentially the way we handle and manipulate text in Java.

Strings in Java are unique because, unlike other objects, they are immutable. This means that once a String object is created, it cannot be changed. Instead, each time you manipulate a String, Java creates a new String object.

Let’s look at a basic String creation example:

String greeting = "Hello, world!";

This line of code creates a new String object with the value of “Hello, world!”.

What Do String Of Java Methods Includes?

String methods are part of the method types in Java that we can use to perform certain operations on Strings. Here are a few common ones:

  • length(): Returns the number of characters in the string.
String greeting = "Hello, world!";
int length = greeting.length();
System.out.println(length); // Outputs 13
  • toUpperCase() and toLowerCase(): Converts all of the characters in the string to upper case or lower case, respectively.
String message = "Hello, world!";
System.out.println(message.toUpperCase()); // Outputs "HELLO, WORLD!"
System.out.println(message.toLowerCase()); // Outputs "hello, world!"
  • equals() and equalsIgnoreCase(): The equals() method checks if two strings are exactly equal, including case. The equalsIgnoreCase() method does the same, but ignores case.
String str1 = "Hello";
String str2 = "hello";

System.out.println(str1.equals(str2)); // Outputs false
System.out.println(str1.equalsIgnoreCase(str2)); // Outputs true

What Is A Substring In Java?

A substring is a subsets of a String. In Java, we can extract a substring from a String using the substring() method. This method comes in two forms:

  • substring(int beginIndex): This returns a substring from the specified beginIndex till the end of the string.
  • substring(int beginIndex, int endIndex): This returns a substring from beginIndex (inclusive) to endIndex (exclusive).

Here’s an example:

String sentence = "Java is fun";
String subs1 = sentence.substring(5); // Returns "is fun"
String subs2 = sentence.substring(5, 7); // Returns "is"

System.out.println(subs1); // Outputs "is fun"
System.out.println(subs2); // Outputs "is"

In the above example, note that string indexing starts from 0, similar to arrays of Java.


The String class is a fundamental part of Java. The built-in methods such as length(), toUpperCase(), toLowerCase(), equals(), equalsIgnoreCase(), and substring() are immensely helpful in handling and manipulating text in Java.

Remember, practice is crucial to mastering any concept in programming. So, I encourage you to write and run the above examples, experiment with the methods, and even try to create your own methods.

In the next lesson, we will learn about more advanced topics, such as StringBuilder and StringBuffer classes in Java. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *