Java String Introduction

Java String Introduction

Strings are mostly used in Java Java sequence of characters is known as String. In Java every string is an instance of String class.String class available in Java.lang package. So String class by default available to every class.Here one important is in java String Objects are immutable objects which means once we create a String object ,we cannot modified it.

How to create Strings?

In the Java Strings are objects.There are Two ways we can create String Objects.

  1. new Keyword
  2. String literal
new keyword:

by using new operaror we can create instance of string. This is like creating an Object to any class. Like following

String str = new String(“java2study”);//two instances will be created

In this way of creating of String Instances There Two Objects will be created. One instance is heap memory and another is String constant pool(SCP).

String literal

here String literal means string Constants .so any data enclosed with in double quotes(“ “) is known as String literal. By using String literal we can create an instance of String will be created like below.

String str = “java2study”;//one instance will be created.

In This process only one instance will be created that will be String Constant Pool(SCP).