Strings
The String class provided by java allows use of strings, which are objects not primitive type.The string literal refers to any alphanumeric enclosed in double quotation mark. Like, "Value"
Strings in java are immutable. Once created they cannot be altered and hence any alterations will lead to creation of new string object.
A String, once created, cannot be changed. None of the preceding methods modify the String, although several create a new String Statements like this create new Strings:
varString = varString + anotherChar;
Creating a few extra Strings in a program is no big deal but creating a lot of Strings can be very costly.
Lets see an example,
String s1 = "Sample"
String s2 = new String("Sample")
String s3 = "Sample"
The difference between the three statements is that, s1 and s3 are pointing to the same memory location i.e. the string pool. s2 is pointing to a memory location on the heap. Using a new operator creates a memory location on the heap. Concatenating s1 and s3 leads to creation of a new string in the pool.
Strings can contain any character, but some of them are "escaped" in order to write them in a literal. Like,
\" stands for the double-quote (") character
\n stands for the newline character
\\ stands for the backslash (\)character
Each of these is written as a two-character sequence, but represents a single character in the string.
Here List of methods of String Class :
The String class provided by java allows use of strings, which are objects not primitive type.The string literal refers to any alphanumeric enclosed in double quotation mark. Like, "Value"
Strings in java are immutable. Once created they cannot be altered and hence any alterations will lead to creation of new string object.
A String, once created, cannot be changed. None of the preceding methods modify the String, although several create a new String Statements like this create new Strings:
varString = varString + anotherChar;
Creating a few extra Strings in a program is no big deal but creating a lot of Strings can be very costly.
Lets see an example,
String s1 = "Sample"
String s2 = new String("Sample")
String s3 = "Sample"
The difference between the three statements is that, s1 and s3 are pointing to the same memory location i.e. the string pool. s2 is pointing to a memory location on the heap. Using a new operator creates a memory location on the heap. Concatenating s1 and s3 leads to creation of a new string in the pool.
\" stands for the double-quote (") character
\n stands for the newline character
\\ stands for the backslash (\)character
Each of these is written as a two-character sequence, but represents a single character in the string.
Here List of methods of String Class :
No comments:
Post a Comment