String In Java

String can be defined as a sequence of characters i.e ‘Hello World” can be called String because it is made by sequence of characters.

In Java, a string is an object that represents a sequence of characters. It is a fundamental data type used to store and manipulate textual data. Strings in Java are instances of the java.lang.String class, which is a part of the Java Standard Library.

How to Declare String in java?

In java String can be declare by two ways

  1. By new Keyword
  2. By String literal

  1. By new Keywork

Java String is created by using a keyword “new”. With new keyword an Object of type String class is created inside memory.

                                                                                 public class STRING {

                                                               public static void main(String[] args) {

                                                               String s=new String(“Hello World”);

                                                               System.out.println(s);

                                                               }

                                                               run:

                                                               Hello World

                                                               BUILD SUCCESSFUL (total time: 0 seconds)

Explanation:

String s=new String (“Hello World”) in this line of code new keywork create object of class name String and Pass “Hello World” String to its constructor, a constructor create object in side heap memory and reference of that object is saved inside variable name s of type String.

Programming Assignment help 

In above picture new keyword create String class object in heap memory area having initialized value is “Hellow World” and its memory address is stored inside String variable name s which is created on Stack Memory Area.

By String literal

ublic class STRING {

                                                             public static void main(String[] args) {

                                                             String s=“Hello World”;

                                                             System.out.println(s);

                                                             }

                                                             run:

                                                             Hello World

                                                             BUILD SUCCESSFUL (total time: 0 seconds)

Explanation:

String s= “Hello World” in this line of code a String “Hello World” is created on String pool area inside Heap Memory Area and its reference is saved inside variable name s of type String.