Exceptions_Strings_Files
Exceptions_Strings_Files
stack
s1 s2
heap
0x200
hello
0x100
hello
system.out.println(s3==s4); true
“palle”
0x300==0x300
Strings and immutability
Strings are considered as immutable entities(whose
content is not changeable).
lhs rhs
stack s
0X800
0X600 String
0x600
P a l l e t e c h
0 1 2 3 4 5 6 7 8
0x800
p a l l e t e c h
0 1 2 3 4 5 6 7 8
we can see
methodname,method
parameters,method return type
scroll it
String disadvantages
disadvantages:
⮚ String objects are immuatble. if you try to
modify a String object, it will create one more
String object.
note: programmer should not use String class
objects for modification purpose.
⮚ more you try to modify, more String objects
will be created and Strings are not meant for
modification more memory will be wasted
String builder
⮚ String builder is a built in class and its code is written by
sun microsystems programmers
⮚ using String builder we can modify the String data
hence it is considered as mutable.
package
- class1
StringBuilderClass Class 3
//5(no of characters)
//21(total 16+5 characters)
Stack s
0x100 StringBuilder
Heap
0x100 -
p a l l e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Object of StringBuilder
StringBuilder internals
lhs rhs
stack sb
0x100 StringBuilder
heap
0x100
output: -
palletech
p a l l et e c h
0 1 2 3 4 5 6 7 8
advantage:
⮚using sb we can modify String data
⮚since sb are mutable, new Strings are not
created in heap and hence memory object of StringBuilder
wastage is reduced
difference between String and StringBuilder
String StringBuilder
⮚ String objects are immutable(not ⮚ StringBuilder objects are
modifiable in same location) mutable(Modifiable in same location)
⮚ String objects are stored either in ⮚ StringBuilder objects are stored in
heap or SC pool heap always
⮚ String objects are fixed sized ⮚ StringBuilder objects are dynamically
entities .Not growable once growable,in same location.
created. ⮚ Internally stored as arrays.
⮚ Internally stored as arrays.
arrays ⮚ Not synchronized.not thread
⮚ Thread safe . you can use String safe.Dont use StringBuilder objects
objects with multiple threads. with multiple threads.it is not safe.your
⮚ use String objects if you don’t data may get corrupted.
want to do many modifications ⮚ Use StringBuilder for modification
purpose.(if you want to do too many
modifications)
String Builder class methods
StringBuilder class methods:
⮚ all methods will modify the data in same location.
⮚ new objects will not be created unlike String class.
*Based on your requirement you can use predefined String Builder class methods
we can see
methodname,method
parameters,method return type
scroll it
StringBuffer
⮚ it is a predefined class of java.
⮚ StringBuffer is exactly same as StringBuilder ,but with only
one difference.
⮚ StringBuilder is not synchronized for multiple threads.
⮚ StringBuffer is synchronized for multiple threads.
exceptions/run
compile time errors are runs your program time errors
detected by compiler and displays o/p detected by jvm
while running
your applications
exception internals
once exception occurs it will not execute next lines it terminates the
program
how to handle exceptions
ArthimaticException
------ ------
------
------ ------ ------
------
------ ------
------ ------
------
------
------
------
------ ------
------ ------
------
------
------
------
types of exceptions
exceptions
unchecked exceptions
FileNotFoundExceptions
}
} if you use throws clause in main() ,your program will
crash
public static void main(String[] args)
{
CheckExep ce=new CheckExep();
try
{
ce.readMe("C:/Users/Admin/Desktop/read.txt");
}
catch (FileNotFoundException e)
{
System.out.println("File Doesnt exit");
e.printStackTrace();
}
}
order of exceptions to handle
most generic
object throwable exception
ioexception
error - filenotfoundexception
- socketexception
sql exception
most specific
example for order of exceptions to handle
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class ExmTrow
{
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter("C:/Users/Admin/Desktop/read.txt");
}
catch(FileNotFoundException f)
{
System.out.println("File Not Found"); Correct Order
f.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
finally block
a. file connections
b. database connections .. etc
public class FinallyBlock
finally block example
{
public static void main(String[] args)
{
opening connection
FileWriter out = null;
try
{
out = new FileWriter("C:/Users/Admin/Desktop/write.txt");
}
catch (FileNotFoundException e)
{ caching FileNotFoundExpectation
e.printStackTrace();
}
catch (IOException e)
{ caching IOExpectation
e.printStackTrace();
}
note :-
finally block will be executed even if programmer uses
break/continue/return statements.
files – important classes
bw.close();
closing connections
fw.close();
files – reading data
File f = null;
FileReader fr = null; Creates a file if it doesn’t exists
BufferedReader br = null;
f = new File("d:/palle.txt");
opening BufferReader to read the data
from file
fr = new FileReader(f);
br = new BufferedReader(fr); reading data line bye line
String str = br.readLine();
while (str != null)
printing data from file by using while
{ loop
System.out.println(str);
str = br.readLine();
}