Skip to main content

String in Java


    Strings are nothing but the sequence of characters that are widely used in the programs. A string is one of the default class in Java which had the capability of binding the characters to form a word. When JVM finds String name in the program it will create an object for the String class. 

    There is no need for a developer to create an object for the String class.

Variable with type String

class Display
{
  public static void main(String[] args)
   {
	String b = "This is a variable of type string"
	System.out.println(b)
   }
}

Method with String type

public class DisplayCheck {
	static String check = "String check";
	
	String display(String s) {
		return s;
	}
	
	public static void main(String[] args) {
		DisplayCheck d = new DisplayCheck();
		System.out.println(d.display(check));
	}
}
Below is some of the list of methods that can be used to do various actions on the String
  1. .length() - This is used to find the length of the string
  2. .concat() - Using this method it will be easy to join 2 string values
  3. .equals() - Used to check 2 strings are equal or not

public class DisplayCheck {
	static String check = "String check ";
	static String con = "Combine this to checked";
	
	String display(String s) {
		return s;
	}
	
	public static void main(String[] args) {
		DisplayCheck d = new DisplayCheck();
		System.out.println("Lenght of the string is " +d.display(check).length());
		System.out.println(d.display(check).concat(con));
	}
}
In the later part of the article, we will see advanced methods

Comments