Q- What is string in java?
Q- Why string immutable in java?
Q- How many way to create string object in java?
Q- What is the use of intern() method of string in java? or What does intern() method do?
Q- What is String Pool?
Q- Is String thread safe in java?
Q- How many objects will be created in below code ?
Answer : Total 3 object.
Two objects created at line-1. code. one in heap and second in the string pool.
One object created at line-2. code. because java always create new object while using new keyword.
but as "Hello Java" is already present in string pool so it will reuse the same from string pool and don't create new on in string pool again.
Related Tutorial
Q- Why string immutable in java?
Q- How many way to create string object in java?
Q- What is the use of intern() method of string in java? or What does intern() method do?
Q- What is String Pool?
Q- Is String thread safe in java?
Q- How many objects will be created in below code ?
- String str1 = new String("Hello Java");
- String str2 = new String("Hello Java");
Answer : Total 3 object.
Two objects created at line-1. code. one in heap and second in the string pool.
One object created at line-2. code. because java always create new object while using new keyword.
but as "Hello Java" is already present in string pool so it will reuse the same from string pool and don't create new on in string pool again.
Related Tutorial
- New features in java 7
- New features in java 8
- Java interview questions part-1
- Java Interview Questions Part-2
- Java Interview Questions Part-3
- Java Interview Questions Part-4
- Java Interview Questions Part-5
- Hibernate interview questions part-1
- Spring boot interview questions part-1
- Spring Interview Questions
- Exception handling in java
- Immutable Class In Java
No comments:
Post a Comment