answersLogoWhite

0

What is string class?

Updated: 8/10/2023
User Avatar

Wiki User

13y ago

Best Answer

Strings are probably one of the most commonly used java data-types. They can hold almost anything that are enclosed within a pair of double quotes and hence are very versatile and powerful. This chapter covers the String class.

Strings Are Immutable Objects

Handling "strings" of characters is a fundamental aspect of most programming languages. In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits, a rich, international set of characters is easily represented in Unicode.

In Java, strings are objects. Just like other objects, you can create an instance of a String with the new keyword, as follows:

String s = new String();

This line of code creates a new object of class String, and assigns it to the reference variable s. So far, String objects seem just like other objects. Now, let's give the String a value:

s = "abc";

As you might expect, the String class has about a zillion constructors, so you can use a more efficient shortcut:

String s = new String("abc");

And just because you'll use strings all the time, you can even say this:

String s = "abc";

There are some subtle differences between these options that we'll discuss later, but what they have in common is that they all create a new String object, with a value of "abc", and assign it to a reference variable s. Now let's say that you want a second reference to the String object referred to by s:

String s2 = s; //refer s2 to the same String as s

String objects seem to be behaving just like other objects, so how is it that they are Immutable? Once you have assigned a String a value, that value can never change-it's immutable, frozen solid, won't change. The good news is that while the String object is immutable, its reference variable is not, so to continue with our previous example:

s = s.concat(" efg");

// the concat() method 'appends' a literal to the end

Didn't I just say that Strings are immutable? Yes, I perfectly did. But, here the stuff within the double quotes that is passed as argument to the concat method gets appended to the end of the String s. How did this happen?

The VM took the value of String s (which was "abc"), and added or rather appended " efg" onto the end, giving us the value "abc efg". Since Strings are immutable, the VM couldn't stuff this new value into the old String referenced by s, so it created a new String object, gave it the value "abc efg", and made s refer to it. At this point in our example, we have two String objects: the first one we created, with the value "abc", and the second one with the value "abc efg". Technically there are now three String objects, because the literal argument to concat, " efg", is itself a new String object. But we have references only to "abc" (referenced by s2) and "abc efg" (referenced by s).

Note, however, that the original "abc" String didn't change; only the reference variable s was changed, so that it would refer to a different String.

To wrap up, the original variable 's' in which we had "abc" as value would be abandoned and a new value "abc efg" would get assigned to it as soon as the s.concat(" efg") line of code is executed.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is string class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is string class features in java?

String class is useful to accept inputs from commands prompt as string arguments


What is the difference among string and string buffer and string builder?

String is the immutable class that means the object f that class never be changed. String is the Sequence of character.


Is String are instances of the class string?

yes


Define class string and implement all the functions related to strings?

define class string


What is the difference between string and class?

A string is a specific class that is used for dealing with text data


How objects are predefind types?

An object is created from a class, like a house made from a blueprint. The object will therefore be of the type of its class. For instance, a String object will be of type String, which is defined by the String class.


How many constructors are there for the string class?

The String class has multiple Constructors. Some of them are: 1. String - new String(String val) 2. Character Array - new String(char[] array) 3. Character Array with index positions - new String(char[] array. int start, int end)


What method of the string class would help you check if a string ends with a certain sequence of characters?

str.endsWith(string)


C plus plus library functions for string operations?

You can use "string" class in C++ for string operations or you may use c style string functions as well. #include <string> String class in C++ provides all basic function to operate on strings. you may details descriptin at http://www.cplusplus.com/reference/string/string/


What is string buffer in java?

StringBuffer is java class available in java.lang package which provides mutable String object where String is immutable class. The methods of this class like reverse(), append(),insert() gives facility to insert data of the same object.


What is the user of stringbufer class explain how it is different from a string class?

A StringBuffer is similar to a String with a few differences:String objects are immutable while StringBuffer objects can be modifiedIf your code involves string manipulation, string buffer is faster than strings


Is String class a final class?

The core classes in the java.lang.* package (e.g. String, Integer, double, Boolean, etc.) are all declared final.