How to Create Java String?

Friends, in today’s post, we will read about String in java and also see string types, so let’s start :-

Introduction to string in java

String is a sequence of characters such as “java string in english” is a string. In java, string is implemented like objects, this has been done so that operations can be performed on the string.Operation like joining or comparing 2 strings etc. can be performed on strings, in java, string is a class, you can create a normal string variable, and you can also create an object of string class, and both Simultaneously, the operation can be performed.

string my string=”this is a string “; //variable
string my string=”new string(“this is a string “); //object

There is a difference between using string as a variable and as an object, when you use string as an object, once the object is created, you cannot change it.
For example, look at the above example, I have created an object named my string, but now no change can be done in its text “this is a string” but when you use string as a variable, changes from it can be done. java provides some methods in which you can make modifications in the string object, but whenever you do this, a new string object is generated, there is no change in the original string object, all these operations have been given below.

string length

The number of characters in any string is the length of that string. To find the length of any string in java, you can use the length() method, for this you call the length method on that string.

string mystring=”this is a string “; //variable
string mystring=new string(“this is a string”); //object

Concatenating string

When a third string is formed by joining any 2 strings, it is called string concatenating in java, you can do this by the + operator and also through the concat() method, as an example of both these methods. About is given below.

string mystring=”this is a string”;
string yourstring=”this is yourstring”;
string ourstring=”mystring+yourstring //with + operator
ourstring =mystring.concat(yourstring); //with concat() method
system.out.println(ourstring);

string indexing with charAt() – This method is used to extract any one character from the string, the indexing of the string is similar to the arrays, the first character is at index 0.