String
• String is a class present in “java.lang” package.
• String is a non-primitive data type which is used to store the combination of character.
• It can take array of character and convert it into one String.
• As it is a non-primitive data type the size of string is not fixed.
Three are two ways to declare the string
1) With using new keyword.
2) Without using new Keyword.
1) With using new keyword. Heap area
String Ref. Variable = new String(“Data”); String pool area
Example
String a = new String(“Velocity”);
Velocity Non-constant
String b = new String(“Velocity”);
pool area
Velocity
2) Without using new keyword. (String literls)
Constant
String Ref. Variable = “Data”
pool area
Velocity
Example
String c = “Velocity”;
String d = “Velocity”;
Used for efficient memory use.
String a = new String(“Velocity”); Different address Heap area
String b = new String(“Velocity”); String pool area
Velocity Non-constant
String c = “Velocity”; pool area
Velocity
String d = “Velocity”;
Constant
All Cases pool area
1) c == d True Same address Velocity
2) a == b False
3) c == a False
4) d == a False
5) c == b False
6) d == b False
IMP == Comparing addresses & .equals() method comparing actual data
• All global Variables (Static and non-static) are stored in heap area only.
• All primitive locale variables are stored in stack memory.
• Only methods are stored in static pool area or Heap area depend on method is static or non-static.
String popular methods
1) .equal(“ ”); 14) .intern(); Shifting non-constant pool area to
2) .equalIgnoreCase(“ ”); constant pool area.
3) .length();
4) .toUpperCase();
5) .toLowerCase();
6) .indexof(‘G’) return int It gives index of first character.
7) .replace(‘ ’ , ‘G’);
8) .replace(“String” , “String”);
9) .endsWith(“def”); return Boolean
10) .startWith(“abc”) return Boolean
11) .concat(“classes”); String
12) .split(“ ”); char array
13) .contains(“abc”); Return type Boolean
1) .equal(“ ”) 3) .length()
Eg. String a = velocity; Eg. String a = velocity;
If( a.equals(“velocity”))
{ int k = a.length();
syso(“True”);
} syso(k); 8
Return type Boolean Return type int
2) .equalIgnoreCase(“ ”) 4) .toUpperCase();
Eg. String a =VELOCITY; Eg. String a = velocity;
If( a.equalIgnoreCase(“velocity”))
{ int k = a .toUpperCase();
syso(“True”);
} syso(k); VELOCITY
Return type Boolean Return type String
5) .toLowerCase(); 7) .indexOf();
Eg. String a = Velocity; Eg. String a = Velocity;
String k = a .toLowerCase(); int k = a .indexOf(“city”);
syso(k); velocity syso(k); 4
Return type String It gives index of first character of string
Return type int
6) .indexOf();
8) .indexOf(char, int);
Eg. String a = Velocity;
Eg. String a = “Velocity classes is in katraj”;
int k = a .indexOf(‘o’);
int k = a .indexOf(‘s’, 16 );
syso(k); 3
syso(k); 18
Index starts from zero
It gives index of given character after mentioned index
Return type int
Return type int
9) .replace(‘ ’, ‘x’) 11) .endsWith(String);
Eg. String a = “velocity is in katraj”; Eg. String a = “velocity is in katraj”;
String k = a.replace(‘ ’, ‘x’); boolean k = a.endsWith(“raj”);
syso(k); velocityxisxinxkatraj syso(k); true
It will replace every space with x
Return type boolean
Return type String
12) .startsWith(String);
10) .replace( String, String)
Eg. String a = “velocity is in katraj”;
Eg. String a = “velocity is in katraj”;
boolean k = a.endsWith(“city”);
String k = a.replace(“city”, “town”);
syso(k); false
syso(k); velotown is in katraj
It will replace every given by town Return type boolean
Return type String
13) .concat(String); 15) .contains (String);
Eg. String a = “velocity”; Eg. String a = “velocity is in katraj”;
String k = a.concat(“ is in katraj”); boolean k = a.contains(“is in”);
syso(k); velocity is in katraj
syso(k); true
It will attach two string
Return type boolean
Return type String
14) .split(String); 15) .intern();
Eg. String a = “velocity is in katraj”; Eg. String a = new String(“velocity”);
String[] k = a.split(“ ”). String b = a.intern();
syso(k[2]); in String k = “velocity”;
syso(b == k); true
Return type array of String
Return type String
String
Stringis immutable
Immutable string means once string is created after that we can not modify or change it.
1)
If we have changed s2
String s1 = “Velocity”
String s2 = “Velocity” S2 = “Classes”
S1
S1 Velocity
Velocity S2
S2
Classes
2) 3)
String s1 = “Velocity” String s1 = “Velocity”
S1 S1 Velocity
Velocity
s1 = “Classes” s1.concat(“Classes”)
Velocity Velocity
S1 S1
Classes Velocity Classes
3)
Reasons why string made immutable
String s1 = “Velocity”
1) Security (Used widely as HashMap key)
S1 Velocity 2) Safe for multithreading
s1 = s1.concat(“Classes”)
Velocity
S1
Velocity Classes
Exceptions in java
Throwable (C)
Error (C) Exception (C)
(java.lang.Error) (java.lang.Exception)
Eg. StrackOverFlowError
VirtualMachineError
UnChecked Exception Checked Exception
Run time exception compile time exception
(Handled by try and (Handled by throws
catch block) keyword)
Eg. Arithmetic exception Eg. InterruptedException
NullPointerException IO Exception
ArryOutofBondException FileNotFoundException