Thursday, December 7, 2017

String in Java


String is an object that represents a sequence of characters. There are two ways of creating String,
1. by using String literal
2. by using new keyword
Strings created using “String Literal” are stored in “String Object Pool”. String Object Pool located in heap.
String Created using “new” keyword, stored String object in Heap memory (non pool) and create a literal in “String Object Pool”.




String is a final class in Java.

String is Immutable in Java hence can be safely used in multi threaded environment.
String Literals are used to get more memory efficient Because It don’t want create any object and if literal is already there it reuses it.

Why String objects are immutable in java?
Because String uses the concept of String literal. If String is mutable, When 5 variables refers to same String then Changing a variable value will affect to all the variables.

No comments:

Post a Comment

The Hypervisor

 The hypervisor is a piece of software that runs on top of hardware infrastructure that creates a virtualization platform. The hypervisor a...