![]() |
|
![]() |
| LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Member Join Date: Nov 2006
Posts: 32
Thanks: 7
Thanked 0 Times in 0 Posts
Rep Power: 4 | Hi friends, Please tell me the difference b/w String and StringBuffer with Example. Thanks Suma |
| | |
| | #2 (permalink) |
| Senior Member Join Date: Oct 2006
Posts: 424
Thanks: 10
Thanked 31 Times in 27 Posts
Rep Power: 13 | Re: String and StringBuffer hi suma... String object is immutable i.e.,itz contents cannot be modified.while StringBuffer objects can be modified. the methods which are used to manipulate the data are not directly available in String class.they are available in StringBuffer class. for example if we need to reverse a string we can use sb.reverse().we need not use the lengthy procedure to reverse a string. there are many other methods which can be used for manipulations of strings in String buffer class. |
| | |
| The Following User Says Thank You to jasmine84 For This Useful Post: | sumahals21 (07-12-06)
|
| | #3 (permalink) |
| Senior Member Join Date: Dec 2006
Posts: 695
Thanks: 3
Thanked 17 Times in 13 Posts
Rep Power: 11 | Re: String and StringBuffer Hi suma, Both String and StringBuffer are final classes. But String is immutable.Once an instance of it is created one cannot change its contents, though all operations are possible. For eg:to concate String s1="ABC" and String s2="DEF" we need another string String s3=s1+s2;which will have the value of "ABCDEF";One cannot append it to s1. But StringBuffer is mutable and one can change the contents of it.The same scenario in this case can be s1.append("DEF");s1 becomes "ABCDEF". |
| | |
| | #4 (permalink) |
| Junior Member Join Date: Dec 2006
Posts: 20
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3 | Re: String and StringBuffer String is immutable where as stringbuffer is mutable ex: if u declare one string object of size 10 .Now u want append that string with another one but the string is overflowed at that time jvm throws an excpetion bu using stringbuffer u can increse the sting object value(u can chnage that value dynamically) |
| | |
| | #5 (permalink) |
| Member Join Date: Aug 2006 Location: Parli-V Age: 25
Posts: 77
Thanks: 24
Thanked 9 Times in 8 Posts
Rep Power: 6 | Re: String and StringBuffer String are immutable means when you try any method with string so JVM creats new object in heap. Ex: String s="Hello"; So s is reference variable and it refers "Hello" in the heap; String s1=s.concat("Friend"); Three object are created in heap one is hello, secong is Friend and third is Hello Friend so Now S refers to Hello and s1 refers to Hello Friend menas Friends has no reference variable now means JVM creates new object on every instatnce Old string objetc is not modified it creates new object. but in case of string buffer it modifies the string object. |
| | |
| | #6 (permalink) |
| Member Join Date: Jan 2007 Age: 27
Posts: 48
Thanks: 1
Thanked 3 Times in 3 Posts
Rep Power: 3 | Re: String and StringBuffer Hi, String object is immutable that means eg. String s1=new String("abc"); for every object that is created jvm will assign internally hashcode to every object to differentiate one object from other it will be very clear from following code: we can use the hashCode() method available in java.lang package to know the hashcode generated by the jvm for each and every object. System.out.println("s1 first hashcode--->"+s1.hashCode()); then we can write s1="def"; System.out.println("s1 second hashcode---"+s1.hashCode()); Here two stmts will give DIFFERENT OUTPUTS bec'z internally instead of modifying the existing string object the jvm will creates the new String object where as in case of StringBuffer the object is mutable that means StringBuffer sb=new StringBuffer("abc"); System.out.println("sb first hashcode---"+sb.hashCode()); sb="def"; System.out.println("sb second hashcode--->"+sb.hashCode()); In this case both stmts will print the SAME OUTPUT bec'z internally no other object is created........ Last edited by uday321; 19-01-07 at 04:19 PM.. |
| | |
![]() |
| Tags |
| string , stringbuffer |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Practice Question in JAVA | sowmya571 | JAVA Technologies | 8 | 15-02-08 08:09 PM |
| Doubt in EJB deployment in IBM Websphere Application Server | sentil77 | JAVA Technologies | 2 | 19-10-06 02:04 PM |
| Hi everyone | sentil77 | Introduce yourself here | 5 | 10-10-06 01:03 PM |
| More Interview Questions Here... |