DEV Community

Divya Divya
Divya Divya

Posted on

String in java?

String:

  • String is a sequence of character used to store text.
  • String is class in Java.
  • String package name of Java.lan.
  • String are immutable(create once , cannot modified).
  • length is one of the String method.

Two way of creating String:

1. Literal String

A literal means writing the value directly inside quotes.
Reuses existing object if same value exists.It's Stored in String Pool.Memory efficient.

Example

string s="abc";
Enter fullscreen mode Exit fullscreen mode

2.Using new keyword

newkeyword is used to create a new object manually.
Creates a new object every time.It's Stored in Heap memory.Uses more Memory.

Example

String str=new string("xyz");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)