What is string?
String is traditionally a sequence of characters. In Java string is an object and it is widely used in Java. String class is defined in java.lang package and implicitly available for all program.
String can be created in two ways.
- By string literal
- by new keyword
String literal is created by double quote, for example-
String aStrign = “A String is Created”:
each time you create an new string, the JVM first check the constant pool whether it is already in constant pool. If is already in available, JVM doesn’t create new string, instead, it just refer the old string as it is already available.
So, if we create strings like-
String a = “A”;
String b = “A”
here, first we created a string which referenced to the reference value a. now if we need another string and if we create like 2nd line of the example, JVM will first check whether there is already a string object in the constant pool which contained “A” available or not, if it is already there, no new string will be created, instead, our new string is referenced by the old one.
Now what is object pool?
Object pool is another kind of data structure managed by Java virtual machine. We all know about constant who ever writes codes in his life time. Constant pool are used for frequent access and less memory consumptions. If a constant is already available in Constant pool, JVM doesn’t need to create a new object.
By new Keyword
String can be create by new keyword as all the other objects are created. Example –
String aString = new String(“Rokon”);
Here is something we need to know.
String is immutable object. Immutable means if a object once created, it will never get changed.
So when we create a string object, it will just never get changed. So now if we assign a new value to an string, the scenario is like, for example –
aString = “A new value is assigned”;
here, a new string object will be created with the value “A new value is assigned” and the object will be referenced to aString and the previous String will be ready for garbage collector.
String concatenation : String concatenation is easy in Java, what you need to do, is adding a plus-
for example –
String firstName ="Bazlur";
String lastName = "Rahman";
int age = 45;
String textToPrint = "My Name is "+ firstName + " " + lastName + " and my age is "+ age;
Output: My Name is Bazlur Rahman and my age is 45
here every concatenation,a new object is created and old one gets ready for gc.
So if we need a thousand of concatenation, JVM will create thousands of new object which required huge memory. To avoid this issue, there is two Class available, they are, StringBuilder and StringBuffer.
Mutability and immutability : A long time ago, once I chatted with my friend. I think its something worthy about.
(08:08:44 PM) Bazlur Rahman: hey
(08:08:53 PM) crusk8: hey
(08:08:59 PM) Bazlur Rahman: how are u ?
(08:09:39 PM) Bazlur Rahman: do u know difference the between mutable and immutable object ?
(08:12:20 PM) crusk8: I was just looking at the idea on wikipedia. looks like the idea for immutable objects is to create an object, set to what you want, use it, and then let it alone
(08:13:16 PM) Bazlur Rahman: i have some basic idea though … but question is when to use them?
(08:14:16 PM) Bazlur Rahman: mutable object can be changed using mutator, and immutable object cant be changed… this is the main motivation ..right ..
(08:15:19 PM) crusk8: yeah. I guess you could use an immutable objects to modify other objects, but, for safety, that immutable object itself couldn’t be modified
(08:17:30 PM) Bazlur Rahman: yup.. don’t you think, immutable object is responsible for less performance.. every time, new object is created, old object is dropped, reference is changed, garbage collector is called…
(08:18:27 PM) crusk8: yeah. if you’re going to use an immutable object. you should use it cases where that object will almost never change
(08:20:10 PM) Bazlur Rahman: but is it good practice using immutable object always? because it is thread safe, I don’t need to do locking, it is protected,
(08:20:49 PM) Bazlur Rahman: and sometime it has good performance… like we dont need to make defensive copy ..
(08:21:39 PM) crusk8: you probably wouldn’t need to do locking on that object. for languages like c++ it would be const protected
(08:21:55 PM) crusk8: you only need to do locking when that object tries to modify other objects
(08:24:05 PM) Bazlur Rahman: so whats the point of using mutable or immutable object. should I always immutable object ?
(08:26:39 PM) crusk8: mutable objects are ones that need to be modified a lot. immutable ones are that rare if ever need to be modified. an immutable object could act like a controller for the mutable objects
(08:26:58 PM) crusk8: it can change them but the mutable objects can’t change it
(08:29:47 PM) Bazlur Rahman: I’m still confused, in which situation, I should use immutable object..? for security issues ?
(08:30:45 PM) crusk8: yeah. you’re the limiting the ways that an object can modified. only through reallocating the object
(08:31:03 PM) crusk8: not through mutators
(08:34:12 PM) Bazlur Rahman: hmm but it has some complexity while implementing immutable object I think ..
(08:35:23 PM) crusk8: I’m not sure if immutable means that object can’t modify its own internal variables. while processing something internal settings can;t be changed
(08:35:43 PM) crusk8: or just that the outside world can’t change things directly
(08:36:12 PM) crusk8: I suppose only data could be local to the function
By the way,
Here I’m going to tell you some advantage and disadvantage of immutable object.
Protection : you can send and immutable object to any class without worrying about it being altered by that class and you never have to make defensive copy.
Thread safe: Its fully thread safe, any number of threads can access them simultaneously, without any synchronization.
Constructor injection : it requires constructor injection while you are going implement inversion of control. In immutable object, there is no way to use mutator.
Implementation complexity : its very much boring to implement immutable object.
Some worthy reading list: