Strings in Java

Strings, which are generally utilized as a part of Java, for writing computer programs, are a grouping of characters. In the Java programming language, strings are like everything else, objects. The Java platform provides the String class to make and control strings.

Instantiating Strings

The most appropriate approach to make a string is to use the following statement:

String mystring = “Hi world!”;

At whatever point it experiences a string exacting in your code, the compiler makes a String object with its value for this situation, “Hi world!’.

Similarly as with other objects, you can make Strings by utilizing a constructor and a new keyword. The String class has eleven constructors that permit you to give the starting estimation of the string utilizing diverse sources, for example, a cluster of characters.

public class myStringdemo{
public static void main(string args[]){
char[] myarray = { ‘h’, ‘i’, ‘.’};
String mystring = new String(myarray);
System.out.println( mystring );

This would deliver the accompanying result:

Note: The String class is changeless, so that once it is made a String object, its type can’t be changed. In the event that there is a need to make a great deal of alterations to Strings of characters, then you ought to utilize String Buffer & String Builder Classes.

Determining String Length

Routines used to get data about an object are known as accessor methods. One accessor technique that you can use with strings is the length() function, which furnishes a proportional payback of characters contained in the string item. This function can be utilized in the following manner:

public class mystring {
public static void main(string args[]) {
String newstr = “I am hungry!”;
int strlen = newstr.length();
System.out.println( “length = ” + strlen ); }

This would create the accompanying result:
length = 12

How to Concatenate Strings

The String class incorporates a function for connecting two strings:

This returns another string that is mystring1 with mystring2 added to it toward the end. You can likewise utilize the concat() system with string literals, as in:

“My name is “.concat(“mary”);

Strings are all the more usually concatenated with the + administrator, as in:

“Hi,” + ” world” + “!”

which brings about:

“Hi, world!”

Sample Implementation:

public class MyString {

public static void main(string args[]) {

String mystr = “Sorry”;

System.out.println(“I ” + “am ” + mystr);


This would deliver the accompanying result:
I am Sorry

For any query or issue, feel free to discuss on