Popular Feed
Subscribe to:
Post Comments (Atom)
Search This Blog
Popular Posts
recent posts
recentposts1
recent comments
recentcomments
What is String in Java?
It's a very important question and most of the interviewers will ask about String. String class represents character Strings.It’s not a primitive data type like int and long. String is a Class in java and defined in the java.lang package. String is immutable and final in Java and JVM uses String Pool to store all the String objects. Below is the syntax of string class.
public final class String extends Object
implements Serializable, Comparable<String>, CharSequence
What are ways to create a String Object?
We can create a String object using a new operator or we can use double quotes to create a
String object. Below are the ways we can create string objects.
String str = new String("abc");
When we use the new operator, JVM creates the String object but doesn't store it into the String Pool.
str .intern();
We can use intern() method to store the String object into String pool or return the reference
if there is already a String with equal value present in the pool.
String str1 = "abc";
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with the same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
String Pool in Java
The Java String Pool avoids creating unnecessary String objects as it reuses the existing String objects.
Therefore the Java String Pool has a significant impact on improving the memory and the application
performance/ utilization of Java String objects.
what happens when we use different ways to create Strings.
String Pool is possible only because String is immutable in Java. The String pool is also an example of
Flyweight design pattern. For more details visit below.
Make String uppercase or lowercase?
Below is the code to make string uppercase.
String s1="hello";
s1.toUpperCase();
Make String lowercase
s1.toLowerCase();
How to compare two Strings in a java program?
Comparing literals String
String s1="hello";
String s2="hello";
System.out.println(s1==s2); // true
In the above code if we created two literals string and if we compare it on the basis of the “=”
operator then both the output will be true. Because both the string pointing to the same object
which is inside the string constant pool.
Comparing String objects
String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1==s2); // false
System.out.println(s1.equals(s2)); // true
In the above code if we created two string objects , now the objects are created inside heap memory
not in a string constant pool. So for that we need to use equals() method to check both strings are
equal or not.
Difference between String, StringBuffer and StringBuilder?
Why is String immutable in Java?
String pool facility without making string immutable , it's not possible at all.
Since String is immutable, it is safe for multithreading. A single String instance can be shared across different threads. This avoids the use of synchronization for thread safety. Strings are implicitly thread-safe.
Strings are used in java classloader and immutability provides security that the correct class is getting loaded by the Classloader.
Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for the key in a Map and its processing is faster than other HashMap key objects. This is why String is the most widely used as HashMap keys.
Since Strings are very popular as HashMap keys, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap.
How to Split String in java?
Below is the statement to split the string.
String s = "javadeveloper";
System.out.println(Arrays.toString(s.split("")));
What does String intern() method do?
Find the below statements
String s = new String("javadeveloper");
s.intern();
On the first line we are creating string object inside heap memory.
Second line while calling the intern method we are telling jvm to create that same string in the string pool also so that we can use the same string in future.
How to create an Immutable class in Java?
Class should be final, so that no other classes can extend it.
All your fields final, so that they’re initialized only once inside the constructor
and never modified afterward.
Don’t expose setter methods.
When exposing methods which modify the state of the class, you must always return a
new instance of the class.
For more details
How to create an Immutable class in Java?Related posts
No comments: