MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Strings


  • A string is a sequence of characters.

  • Strings are objects that represent a char array. For example :

  • char[] str = {'S','T','R','I','N','G'};
    String s = new String(str);

    is same as :
    String s = "String";

  • Strings are immutable and cannot be changed.

  • java.lang.String class is used to create a String object.

  • The string is a class but can be used as a data type.

  • Syntax of strings in Java :
    String <String_name> = "<sequence_of_string>";

    Example :
     String str = "JavaTutorial";

    In the above example, str is a reference, and “JavaTutorial” is an object.

    Different ways to create a string in Java :


    In Java, strings can be created in two ways :

    1.By using string literal
    2.By using the new

    Creating String using String literal :


    String s1= "String literal"

    We use double quotes("") to create string using string literal. Before creating a new string instance, JVM verifies if the same string is already present in the string pool or not. If it is already present, then JVM returns a reference to the pooled instance otherwise, a new string instance is created.

    Creating String using new :


    String s=new String("Harry");

    When we create a string using "new", a new object is always created in the heap memory.
    See the examples given below to get a better understanding of String literal and String object :
    String str1 = "JavaTutorial";
    String str2 = "JavaTutorial"
    System.out.println(str1 == str2); 

    Output

    True

    Returns true because str1 and str2 are referencing the same object present in the string constant pool. Now, let's see the case of the String object :
    String str1 = new String("Keep coding"); 
    String str2 = new String("Keep coding""); 
    System.out.println(str1 == str2); 
    

    Output

    False

    Although the value of both the string object is the same, still false is displayed as output because str1 and str2 are two different string objects created in the heap. That's why it is not considered a good practice two compare two strings using the == operator. Always use the equals() method to compare two strings in Java.

    Different ways to print in Java :


    We can use the following ways to print in Java:

    System.out.print() // No newline at the end
    System.out.println() // Prints a new line at the end
    System.out.printf()
    System.out.format()
    System.out.printf("%c",ch)

  • %d for int
  • %f for float
  • %c for char
  • %s for string

  • Example


    package com.company;
    import java.util.Scanner;
    
    public class strings {
        public static void main(String[] args) {
             String name = new String("Java");
             String name = "Java";
             System.out.print("The name is: ");
             System.out.print(name);
            int a = 6;
            float b = 5.6454f;
            System.out.printf("The value of a is %d and value of b is %8.2f", a, b);
            System.out.format("The value of a is %d and value of b is %f", a, b);
            Scanner sc = new Scanner(System.in);
            String st = sc.next();
            System.out.println(st);
    
        }
    }
    

    Output

    The name is :
    Java
    The value of a is 6 and value of b is 5.65
    The value of a is 6 and value of b is 5.645400
    1
    1


    Conclusion

    In this page (written and validated by ) you learned about Java Strings . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java String Methods.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.