Skip to main content

Difference between String ,StringBuffer & StringBuilder?



Run this simple java program and create scenerio of String object creation in your mind.You will see huge difference than expectation.

                               I would recommend to first try on your own then follow the solution .
As you execute the above line of codes in your Java Runtime Environment .As compiler
read the lines .In  case of 
                                   String a= new String("Hello");
       As it will allow compiler to create a new string object .It is always mandatory for 
compiler to allocate a memory space in heap whenever new keyword is used.So String
class object created successfully in heap with reference variable a.And a very important point 
need to be noted down that here  "new String("Hello');"  It will make a literal object in String 
pool with value "Hello".So total number of object created equals two.so if you print value of a
using System.out.println(a);  It will print Hello as reference variable for object a contain same 
value. Similarly it happens with  the next line  
                                     String b= new String("Java");
it also creates the same scenerio as String referenced above.But in case of next following line 
                                     String c="HelloJava";
In this case there will be a literal stored in constant pool.It also rise three most important question.

Que:What is difference between  constant pool and heap?
Que:What makes String immutable & why?
Que:How JVM utilize the need of constant pool?

As next lines of code executed it work as follow.In the given case when evaluation for
                                   System.out.print("c==a+b");

Here a & b are object of type String .While c is an string literal which contain value "HelloJava".
So when + operator work between two different object .It does not evaluate result equal to any 
String literal which is stored in Constant pool. So for "c==a+b" it will result to boolean result 
which comes out to be false.So only false will be printed.
In the following next line while evaluating  c=="Hello"+"Java";
It will work as operator overloading between two String literals.And as describe "Hello" &
"Java" both are stored in Constant bool which concat and result equals to c.
So true will pe printed  in System.out.print(c=="Hello"+"Java");

StringBuffer is faster than String when performing simple concatenations. In String
 manipulation code, character strings are routinely concatenated. 
      
         






Comments

  1. Toptal Software Developers Community. can be a good place to work

    ReplyDelete

Post a Comment

Popular posts from this blog

LOVE THE WAY SINGLETON PATTERN CAN BE DESIGNED ! WOWW...

To implement Singleton pattern, we have different approaches : 1.Eager initialization: In this method the instance of clss is created at loading time .As whenever in java there is a requirement of species at loading time we first remember of Static keyword. package com.questprogram.singleton; public class EagerInitializedSingleton { private static final EagerInitializedSingleton instance = new EagerInitializedSingleton(); //private constructor to avoid client applications to use constructor private EagerInitializedSingleton(){} public static EagerInitializedSingleton getInstance(){ return instance; } } 2. Static block initialization Static Block initialization implementation is similar to eager initialization, except that instance of class is created in the static block that provides option for exception handling. package com.questprogram.singleton; public class StaticBlockSingleton { private static StaticBlockSingleton in

Before Starting MicroServices just walk through Orchestration !! Matters a lot

What is this Orchestration : Orchestration is the traditional way of handling interactions between different services in Service-Oriented Architecture (SOA). With orchestration process, there is typically one controller that acts as the “orchestrator” of the overall service interactions. This typically follows a request/response type pattern. For example, if three services needed to be called in a particular order, the orchestrator makes a call to each one, waiting for a response before calling the next. 

10 points to know about volatile variable in JAVA?

Volatile variable have some specific uses.It's better to know all the major reason  why java adopt such variables .Please found the below 10 points which need to be noticed: 1.Volatile variable always get allocated in the main memory.To allocate any variable in main memory volatile keyword is used. 2.Precisely volatile variable are always read and write directly from main memory.It doesn't perform any operation from CPU 's cache. 3.Volatile variable after JAVA 5 guranatees the visibilty of changes to variable across multiple threads. 4.Volatile also provide atomicity in some cases e.g .while reading double and long data types. 5.In case of volatile long and  volatile double data types which are of 64 bytes the read operation is atomic. 6. If you know that a long field is accessed by more than one thread e.g. a counter or anything, you better make it volatile. Why? because reading to a long variable is not atomic in Java and done in two steps, If one thread is w