Strings, which are
widely used in Java programming, are a sequence of characters. In the Java
programming language, strings are objects.
In contrast with C or
C++, in Java handling strings is different because:
·
in Java every char is a
16 bit Unicode value, and not 1 byte;
·
in Java, strings values
are managed by String objects(String pool)
·
in Java, the syntax
allows you to use Strings as primitive data types (you can use = operator to
initialize them)
·
in Java, Strings are
immutable objects, meaning that once are created, they can’t change their
value.
How to define and
initialize a String
String s = new
String(); S=”abcd”; (or) String s= new String(“abcd”); //Zillion Constructors
Or
String s=”abcd” ; this representation is called String Literal.
In above two String
initializations First Creates one String
object ,one references variable and point references variable to string object.
Every String Literial is
Storead in String Constant pool.
String Constant pool is
memory area created by jvm for efficient
use of memory for Strings.
When compiler Encounter a String
Literal , it Checks the String Constant pool to see if any identical String already Exists
. if any match is found ,the references to the
new String literal
Example:
String ss = ”abcd”;
String s=”abdc”; already present in String Constant
pool , now we create same String ss=”abcd”
object with abcd . so compiler checks for identiacal String in String
constant pool . if found it simple
create references variable ss and point
to String object “abdc”; (above)
One More important thing in String is String
are IMMUTABLES , means once it created does not change. String value
Constant.
Example:
String s=”abcdef”;
s.concat(“ghi”);
S.O.P(s); o/p : abdcdef
. because Strings are immutables.
In this case what happen is
String s=”abcdef”;
creates one references variable
and one string object in heap memory and pointing to S.
s.concat(“ghi”);
creates object
with “abcdefghi” in heap
memory (concat both Strings) ,but no references variable is
Assigned . so the String is remains same
(abcd). So Strings are immutables.
Example:
String s=”abcdef”;
s= s.concat(“ghi”);
S.O.P(s); o/p : abdcdef ghi .
s=s.concat(“ghi”); in this case , the concat objects is “abcdefghi” is create in head memory,and
s is pointed to this new object(because we assing s=s.concat(“ghi”) ).
Example:
String s="Java";
x.concat("Rules");
s.o.p("x="+x); ------->o/p x=Java
x=x.concat("Rules");
s.o.p("x="+x) ----------->o/p x=Java Rules
x.toUpperCase();
s.o.p("x="+x) ------------>o/p x=Java Rules
String &Memory:
One of the key goal of any good programming language is to make efficient us of memory . as application grow ,it's very common for String literals to occupy larger amount of programs memory,and there is often a lot of redundancy with in the program.
To make java more efficient ,the JVM sets aside a special area of memory called the "String constant pool". when the compiler encounters a String Literal it checks the pool to see if an identical string already exists .if a match is found ,the reference to the new literal is directed to existing string ,and no new String literal object is created(The existing String simple has an additional reference)
Note: String class is Final class does't override String class functionality.(String class methods)
Differences Between String Literal (String s="abcd")& String (new key word used to create String s=new String("abc"))
String s="abdc" ;
In this case , creates one String object and one reference variable. "abc" will goes to the pool and s is refer to it.
String s =new String("abcd"); // creates two objects and one references variable.
In this case ,because we used new keyword ,java will creates a new String object in normal(nonpool)memory,and s will refer to it.In addition ,the literal "abc" will be places in the pool.
Note: String are Immutable object in java.
Methods in String class :
1) char charAt(int index) 2)String concat(String str) 3)int length()
4)String replace(char oldChar, char newChar) 5)String trim() 6)boolean equalsIgnoreCase(String anotherString)
7)String substring(int beginIndex) 8)String toLowerCase(Locale locale) 9) String toUpperCase(Locale locale)
10)String toString() 11)String[] split(String regex)
String s="Java";
x.concat("Rules");
s.o.p("x="+x); ------->o/p x=Java
x=x.concat("Rules");
s.o.p("x="+x) ----------->o/p x=Java Rules
x.toUpperCase();
s.o.p("x="+x) ------------>o/p x=Java Rules
String &Memory:
One of the key goal of any good programming language is to make efficient us of memory . as application grow ,it's very common for String literals to occupy larger amount of programs memory,and there is often a lot of redundancy with in the program.
To make java more efficient ,the JVM sets aside a special area of memory called the "String constant pool". when the compiler encounters a String Literal it checks the pool to see if an identical string already exists .if a match is found ,the reference to the new literal is directed to existing string ,and no new String literal object is created(The existing String simple has an additional reference)
Note: String class is Final class does't override String class functionality.(String class methods)
Differences Between String Literal (String s="abcd")& String (new key word used to create String s=new String("abc"))
String s="abdc" ;
In this case , creates one String object and one reference variable. "abc" will goes to the pool and s is refer to it.
String s =new String("abcd"); // creates two objects and one references variable.
In this case ,because we used new keyword ,java will creates a new String object in normal(nonpool)memory,and s will refer to it.In addition ,the literal "abc" will be places in the pool.
Note: String are Immutable object in java.
Methods in String class :
1) char charAt(int index) 2)String concat(String str) 3)int length()
4)String replace(char oldChar, char newChar) 5)String trim() 6)boolean equalsIgnoreCase(String anotherString)
7)String substring(int beginIndex) 8)String toLowerCase(Locale locale) 9) String toUpperCase(Locale locale)
10)String toString() 11)String[] split(String regex)
More coming soon……………………………………………………..
0 comments:
Post a Comment