Header Ads

Why String class is not like other classes in java

In Java, You can create a String object as:  String str = "abc"; &  String str = new String("abc");Why cant a button object be created as : Button bt = "abc" Why is it compulsory to create a button object as: Button bt = new Button("abc"); Why is this not compulsory in String's case?

Well, This is question which is asked almost in every forum. The answered solutions are good for interview. Like String is an immutable class so this type of assignment is possible
String str = "abc"; &  String str = new String("abc");
But here let us look the internal logic why Java is giving such flexibility to the user. How this is beneficial for us.
Java is a language where from client side window ,client side page to server side catching, database query to item display,Even for simple data display ..everywhere, there is string. String is used extensively.
Now if we use so much of String in our program we need to think the memory allocation for the same.
minimum memory usage of a Java String ::
Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)
so for a null string the required memory will be =8*5=40 byte!!!!!!!!!!!!!!!
Now think about your page or GUI ...how many Strings (objects) are there.
every object has at least 8 bytes of housekeeping data, and arrays 12 bytes, and will be padded to a multiple of 16 bytes (in 32-bit versions of Hotspot);
a Java String actually consists of more than one object;
a Java char takes up two bytes, even if you're using them to store boring old ASCII values that would fit into a single byte;
a Java String contains some extra variables that you might not have considered-----From javamex

So if java would have given that facility for string in java then it would have a real memory problem for all of us. Again so much of object needs to be handled.
They made our life easy...How lets see.....

They introduced two ways while working with String
1.String pool
2.String Object
How do they work---

A JVM has the Pool. All the String Object which are created by assignment stored in the pool. This pool is present in the heap. So whenever any assignment is done for String first it check in the String Pool whether that String is already exist or not... This is done by calling intern() method present in the String class. If it find the same String then it return the same reference else it create new one. But with new Operator everytime it creates the new Object.
Important to not that you are NOT calling a java.lang.String constructor when you type String s =
"xyz"; for example:
String x = "xyz";
String y = "xyz";
refer to the same object in the pool.

String x1 = new String ("xyz");
String x2 = new String ("xyz");
refer to two different objects.
 By this mechanism Java is saving memory . Hence your application performance is becoming good. But how many buttons will be there in a page?? Will they be so many as String?????
Hence java has given it for string not for button.


Powered by Blogger.