Search This Blog

Wednesday, July 1, 2015

Difference Between String , StringBuilder And StringBuffer Classes With Example : Java

Today we are going to understand the difference between String , StringBuilder and StringBuffer . As you will find that there are minor differences between the above mentioned classes.

String

String is immutable  ( once created can not be changed )object  . The object created as a String is stored in the  Constant String Pool  . 
Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously.
String  once assigned can not be changed.

String  demo = " hello " ;
// The above object is stored in constant string pool and its value can not be modified.


demo="Bye" ;     //new "Bye" string is created in constant pool and referenced by the demo variable            
 // "hello" string still exists in string constant pool and its value is not overrided but we lost reference to the  "hello"string  

StringBuffer

StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap . StringBuffer  has the same methods as the StringBuilder , but each method in StringBuffer is synchronizedthat is StringBuffer is thread safe . 

Due to this it does not allow  two threads to simultaneously access the same method . Each method can be accessed by one thread at a time .

But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus  StringBuilder is faster than the StringBuffer when calling the same methods of each class.

StringBuffer value can be changed , it means it can be assigned to the new value . Nowadays its a most common interview question ,the differences between the above classes .
String Buffer can be converted to the string by using 
toString() method.

StringBuffer demo1 = new StringBuffer("Hello") ;
// The above object stored in heap and its value can be changed .
demo1=new StringBuffer("Bye");
// Above statement is right as it modifies the value which is allowed in the StringBuffer

StringBuilder

StringBuilder  is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. 
StringBuilder is fast as it is not thread safe .  


StringBuilder demo2= new StringBuilder("Hello");
// The above object too is stored in the heap and its value can be modified
demo2=new StringBuilder("Bye"); 
// Above statement is right as it modifies the value which is allowed in the StringBuilder


----------------------------------------------------------------------------------
                                    String                    StringBuffer         StringBuilder
----------------------------------------------------------------------------------                 
Storage Area | Constant String Pool           Heap                       Heap 
Modifiable     |  No (immutable)            Yes( mutable )          Yes( mutable )
Thread Safe   |           Yes                                  Yes                              No
 Performance |         Fast                                Very slow                    Fast
-----------------------------------------------------------------------------------


Please mention in the comments in case you have any doubts related to the post: difference between string, stringbuffer  and stringbuilder.

Understand at, atq, atrm, batch Commands using 9 Examples

You can execute batch jobs in UNIX / Linux using any one of the three commands — at, batch or cron.
In this article, let us review how to schedule a job, view a job, and delete a job using at command.

You can schedule an at job in two different ways:
  • Schedule the job to be executed at a specific time. For example, July 3rd, 10AM
  • Schedule the job to be executed in relative time from now. For example, 5 hours from now.

1. Schedule an at job using specific date and time

Syntax:
$ at time date
For example, to schedule a job at 11 am on May 20, use the following at command.
$ at 11 am may 20

2. Schedule an at job using relative time

You can schedule a job to be executed using relative time from now.
Syntax:
$ at now + COUNT UNIT
For example, following job will be execute 1 minute from now.
$ at now + 1 min
The above example will read the commands from stdin, and it will execute the job after a minute. When you give something wrong in time format, you will get the error ‘Garbled time‘.
You can schedule a background job for 1 hour from now, (or) 1 day from now using the following at command:
$ at now + 1 hour

$ at now + 1 day
Similar to at command, you can also use crontab to execute jobs at a scheduled time. Refer to our earlier 15 cron command examples article.

3. View all the scheduled at jobs using atq

You can use atq command (or at -l), to display all the at command jobs that are scheduled or currently running.
The following atq command will list all the pending at jobs. The first number shown is the Job number, followed by the time in which the process is to be executed, and the user name.
$ atq
4 2010-04-20 11:00 a sathiya

4. Remove/Delete a scheduled at job using atrm

You can use atrm command (or at -d), to delete a particular job. For example, to delete the job number 4, use the following atrm command.
$ atrm 4

5. Execute a job only when system load average is < 1.5 using batch command

You can schedule a job using batch command, which will prompt for command input, which will be executed when the system load average is less than 1.5.
$ batch
At the successful completion of input, you will get job number. For listing and removing batch jobs you can use the at commands explained above.

6. Schedule at jobs from file using -f option

First create a text file that contains all the commands, or shell-scripts that you would like to be executed in the background using at command.
$ cat myjobs.txt
/home/sathiya/calculate-space.sh
/path/to/a/shell-script
/path/to/any/command/or/script
Using the -f option, you can make the at command to get the input from the file instead of stdin.
Following at command will execute all the jobs from the myjobs.txt 1 hour from now.
$ at -f myjobs.txt now + 1 hour

7. Allowing and Denying certain users from using at jobs

System administrator can control who can schedule an at job and who cannot using at.allow and at.deny files.
First, system checks for at.allow file. If at.allow exists, only the usernames specified in the at.allow file are allowed to use at command.
Next, (if at.allow doesn’t exist), system checks for at.deny file. If at.deny exist, the usernames specified in the at.deny file are not allowed to use the at command.
By default, most systems uses at.deny file to stop certain users from using the at command, such as www-data, guest, backup, man user.

8. Execute at command like nohup

Similar to the nohup command we discussed earlier, you can execute a command (or shell script) on the remote server using the at command and logout from the server.
$ at -f myjob now + 1 min

$ exit
Note: myjob will still be running even after you exit out of the server.

9. Additional at command time formats

You can use any one of the following at command date time formats:
$ at 10 am tomorrow

$ at 11:00 next month

$ at 22:00 today

$ at now + 1 week

$ at noon

My Profile

My photo
can be reached at 09916017317