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.
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.
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)
str.endsWith(string)
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/
The core classes in the java.lang.* package (e.g. String, Integer, double, Boolean, etc.) are all declared final.
String class is useful to accept inputs from commands prompt as string arguments
String is the immutable class that means the object f that class never be changed. String is the Sequence of character.
yes
define class string
A string is a specific class that is used for dealing with text data
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.
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)
str.endsWith(string)
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/
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.
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
The core classes in the java.lang.* package (e.g. String, Integer, double, Boolean, etc.) are all declared final.