The difference between string and stringBuffer class in java is that the length of the string is fixed in string class whereas the length of the string is not fixed in string buffer class.
String Class vs. StringBuffer Class in Java
Java is an object-oriented programming language that uses both compiler and interpreter. Mostly all software’s are made in Java programming language. Java code can be written on Windows, Linux, and Mac OS. The syntax of C and C++ programming language is quite the same. Java produces browser to run programs that help in making a graphical user interface. Java programming language is in used and trend these days. To write Java code, a programmer needs software development kit (SDK) that includes a compiler, interpreter that is not required in C++. This article we about the difference between string and string buffer class in java. The basic difference between string and string buffer class in java is that the length of the string is fixed whereas the length of stringbuffer is not fixed. Classes that operate on strings are string and string buffer. String buffer class leads to string class. The length of the string class is fixed whereas the length of the string in string buffer class is not fixed. The object of the string buffer class is growable. The object in string class is immutable whereas object in string buffer class is mutable.
What is String Class?
There are many classes in java one such class is string class. The string in string class is of fixed length. Objects that are in string class are immutable. The biggest drawback of string class is that you cannot change the object in the string class. There is a string constant pool in string class the objects are store in these string constant pool. System out println(:hello this is our solution”) this is used to create an object of type string. You have to use this every time you create a string in the string class. Hello this our solution is the string in the string class. To concatenate two strings String str = String(“our”); str concat(“Solution”) system out println(str); this is used. This will concatenate our solution and show as an output. By this object of the string is also created. There are many methods in string class such as char charAT(int index) that returns the character at the given index. Boolean equals(Object obj) compares the string with the specified string it true if string matches. Boolean equalsIgnoreCase(Object obj) is a method that works as the equal method it does a case insensitive comparision. Int compareTo(string) compares the two strings based on the Unicode.
What is String Buffer Class?
More functionality is provided to the string using string buffer. Unlike buffer class string buffer is mutable that means you can change the object in a string buffer. The length of the string in string buffer is growable. Characters and substrings are inserted in string buffer class. Space is specified in buffer class that is 16 additional characters, and the length is not specified as well. Append method is used to specified string with the string like append(char), append(Boolean), append(int), appenf(float), append(double). Insert(int) method is used to insert the string. Replace(int startIndex, int endIndex, string str) is used to replace string. Delete(int startIndex, int endIndex) is used to delete the string.
- Length of string is fixed in string class whereas the length of the string is not fixed in string buffer class.
- There can be no modification in string class whereas there can be a modification in string buffer class.
- String in string class is mutable whereas string in string buffer class is immutable
- String class consumes more memory whereas string buffer class consumes less memory