N. Achyut java,Uncategorized String In Java | Basic Java Tutorial

String In Java | Basic Java Tutorial

String In Java | Basic Java Tutorial

String in Java :It is basically an object that represents sequence of char values. An array of characters works same as Java string.

String in Java :

  • It’s Sequence of Unicode character
  • Defined in java.lang.package
  • String Literals are delited by double Quotes.
  • String are treated as objects

Syntax of string in java

<String_Type> <string_variable> = "<sequence_of_string> ";

Substring in Java:

  • It’s a method of the String class.
  • substring(int beginIndex, int endIndex)
  • It returns a new string that is substring of this string.

String Concatenation

  • Use “+” operator
  • Example :
    String str = ” Hello” + “World”;

String are Immutable

  • String are immutable ( cannot grow) .
  • Whenever a change to a String is made, an entirely new String is created.
  • Example :
    String a = “Hello”;
    String a = ” World”;
    a object point the world .

Empty Vs Null String

  • Empty string “” is a string with length 0.
  • String variable holds a special value null when it doesnot point to a string
  • Example :
    String str1 = “”;
    String str2 = null;

Program to Understand more about String in Java

package practice_13;
public class StringPractice 
{
    public static void main(String[] args) 
    {
        //Creating a string
        String str1 = "Welcome to Utshuk Tech!";
        System.out.println(str1);
        
        //Substring 
        System.out.println(str1.substring(0,7));
        
        //Concatenation of string
        String str2 ="Hello"+"World!";
        System.out.println(str2);
        
        //Some imp methods of string
        //1. .equals
        String str3 = "Hello";
        String str4="Hello";
        System.out.println(str3.equals(str4));
        
        // ==
        String str5 = new String("Hello");
        String str6 = new String("Hello");
        System.out.println(str5 == str6);
        System.out.println(str5.equalsIgnoreCase(str6));
        
        //2 .length()
        System.out.println(str5.length());
        
        //3. .toUpperCase
        System.out.println(str5.toUpperCase());
    }
    
}

Related Post