Skip to main content

Command Palette

Search for a command to run...

String

Constructors and memory location

Updated
2 min readView as Markdown
String

The string is a built-in CLASS available in Java but it is almost used as a data type.

Simply says String is a collection of characters.

Example: String Str = "My name is Mahadev Deshmukh";

Here, String = class, Str = Referal which stores Object, "Java Program" = Object and Double quotes are known as String Literals.

There are two ways to create a String object:

a] By string literal b] By new keyword

So where this string value will be stored?

  1. If you write syntax as String str = "My name is Mahadev Deshmuk"; then it will save in the Pool area.
  2. If you write syntax as String str = new string("My name is Mahadev Deshmukh"); then it will save in the heap area as NEW creates a new object and Object always stores in the heap area.

there is a third situation also explained in the below image:

Interning-of-String-in-Java.jpg

You can learn more about this concept from [here].(https://www.geeksforgeeks.org/interning-of-string/)

Now, Let's look at the different String writing methods.

Constructors are methods or functions used for creating a string object and below are the 3 types of constructors:

  1. String (char [ ]) An array of characters is taken and converted into a string.

  2. Byte Array, String (byte [ ]) An array of bytes is taken and converted into a string.

  3. String str1 = new String ("Java"); A string literal is taken in this method and a new string is created using this method.

Important Note: In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once a string object is created its data or state can't be changed but a new string object is created.

More from this blog

Stack Mahadev's Blog

24 posts