String in Java

String in java

Well, a String in java is a class, which represents a character string (generally speaking, the string is a set of characters). Strings are very widely used in java, and any string we create in a java program is an object of the String class. Once the string object is created, you cannot change its value. This refers to immutability.

Java String

However, we have something called StringBuilder which we are going to discuss later, which supports the mutable Strings. Later on, we are also going to discuss why Strings are immutable.

At times, there might be a need to store some String into the program, for any reason. This is where you can make use of the String class. We have also got many methods with which we can do different things, like converting the String to lowercase or uppercase or finding out the length of the String. But again I want to mention that the Strings are immutable in Java. Read that again.

By saying that Strings are immutable in java, I mean that we cannot change the value of the String once it is created. If we implement some methods, which help us to do some things like converting the string to uppercase or lowercase or something like that, instead of doing the operations on the same string, a new object will be created and the new value will be there(the new object is created with the effective changes).

The old string object will be as it is. This is what immutability is, and it is pretty much important to understand.

Creating a string in Java

Let’s now understand that what are the different ways to create String objects. Well, here we can also create a String object just as if we are creating a variable with a primitive data type, though String is not a primitive data type. Well, first of all,

let’s understand the syntax for creating the String object. I also want to mention that this resembles the way we created the Scanner object in the earlier examples, and also, when we are going to learn about classes and objects, we will be creating our own classes, and the objects for that classes, so they will also be created in a similar way. If not here, you will get a clearer picture of how we create objects of a particular class later in the course.

But for now, we are going to briefly mention in a light way, what are the different things we are going to see in the instruction which help us create and initialize the object. Mark the previous line, and read those bold words again.

Here is one of the example syntax for creating a String object.

String myString = new String(“This is my first String”);

Here, we have mentioned before that the string we create is going to be an object of the String class. That’s what we are doing here. The object reference is myString, and the string value here is – ” This is my first String”. The new is a keyword that is used to create an object of some particular class (String in our case). After that comes the constructor String(), which initializes the values of the new object(object referred to by myString in this case). Don’t worry. Some of the concepts may be related to Object-Oriented Programming, which we are going to cover later in this course. But for now, just keep in mind that the above shown is one way of creating a String object.

Since strings are used a lot in Java, we have special support for strings. We can create string objects just like we create the other variables of primitive type(but this does not mean that string is a primitive datatype. It is not. It is a referenced datatype)

Here is the example program for demonstrating different ways to create String objects.

package com.company;
public class Main {
public static void main(String[] args) {
// Lets create a string object… way 1: Similar to how we created a Scanner object.
// The basic thing is that we create object of some class like this only. we will see this in greater details ahead.
String myString = new String(“This is my first String”);
System.out.println(myString);

// Way 2: Just like we created variables till now.
String anotherString = “This is a newly created String”;
System.out.println(anotherString);

// or you can also try doing something like this –
String newString;
newString = “This is another String”;
System.out.println(newString);
}
}

Notice that we are writing String literals always into the double-quotes. This is important to mention so that we do not make any mistakes in this context ahead. We often write characters in single quotes, but the String literals are written into double quotes. Well, we are going to explore the Strings in java further from now.