Java Unit 3
Java Unit 3
PREPARED BY: PATEL MANESH - M.Sc(CA & IT) Contact: 90165 17796 1
PATEL MANESH
The eight classes of java.lang package are known as wrapper classes in java.
PRIMITIVE WRAPPER
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 2
PATEL MANESH
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 3
PATEL MANESH
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
Output:
}
---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 4
PATEL MANESH
Creating Strings
The most direct way to create a string is to write −
Example
Hello.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 5
PATEL MANESH
String Length
The length() method which returns the number of characters contained in
the string object.
Example
Concatenating Strings
The String class includes a method for concatenating two strings −
string1.concat (string2);
This returns a new string that is string1 with string2 added to it at the end.
You can also use the concat() method with string literals, as in −
Which results in −
"Hello, world!"
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 6
PATEL MANESH
String toLowerCase()
Converts all of the characters in this String to lower case using the rules of
the default locale.
String toString()
Converts all of the characters in this String to upper case using the rules
of the default locale.
int length()
Returns the index within this string of the first occurrence of the specified
character.
Returns the index within this string of the first occurrence of the specified
substring.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 7
PATEL MANESH
class Test {
public static void main(String M[]) {
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 8
PATEL MANESH
StringBuffer class
StringBuffer class is used to create a mutable or changeable string object
i.e its state can be changed after it is created.
It represents grow able and writable character sequence.
As we know that String objects are immutable, so if we do a lot of changes
with String objects, we will end up with a lot of memory leak.
So StringBuffer class is used when we have to make lot of modifications
to our string.
StringBuffer defines 4 constructors.
They are
1. StringBuffer ( )
2. StringBuffer ( int size )
3. StringBuffer ( String str )
4. StringBuffer ( charSequence [ ]ch )
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 9
PATEL MANESH
append()
This method will concatenate the string representation of any type of data
to the end of the invoking StringBuffer object.
OUTPUT = ManeshPatel
insert()
This method inserts one string into another.
Here are few forms of insert() method.
Example
StringBuffer str = new StringBuffer ("Manesh");
str.insert (6, “ Patel”);
System.out.println (str);
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 10
PATEL MANESH
reverse()
This method reverses the characters within a StringBuffer object.
OUTPUT = 54321
replace()
This method replaces the string from specified start index to the end index.
capacity()
This method returns the current capacity of StringBuffer object.
OUTPUT = 16
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 11
PATEL MANESH
setLength()
import java.lang.*;
// length of stringbuffer
System.out.println("length = " + buff.length());
Output
buffer1 = ManeshPatel
length = 11
buffer2 = Manesh
length = 6
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 12
PATEL MANESH
charAt()
The java.lang.StringBuffer.charAt() method returns the char value in this
sequence at the specified index.
Example
import java.lang.*;
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 13
PATEL MANESH
setCharAt()
The java.lang.StringBuffer.setCharAt() method sets the character at the
specified index to ch.
This sequence is altered to represent a new character sequence that is
identical to the old character sequence, except that it contains the character
ch at position index.
import java.lang.*;
// character at index 3
System.out.println ("Character at index 3 = " + buff.charAt (3));
// character at index 3
System.out.println ("Character at index 3 = " + buff.charAt (3));
}
}
Buffer = MANESH
Character at index 3 = E
After Set, buffer = MANiSH
Character at index 3 = i
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 14
PATEL MANESH
Excepting Handing
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 15
PATEL MANESH
1) Checked Exception
2) Unchecked Exception
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 16
PATEL MANESH
Keyword
try The "try" keyword is used to specify a block where we should
place exception code. The try block must be followed by either
catch or finally. It means, we can't use try block alone.
catch The "catch" block is used to handle the exception. It must be
preceded by try block which means we can't use catch block
alone.
finally The "finally" block is used to execute the important code
of the program. It is executed whether an exception is
handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It doesn't
throw an exception. It specifies that there may occur an
exception in the method. It is always used with method
signature.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 17
PATEL MANESH
Java Exceptions
1) A scenario where ArithmeticException occurs
String s=null;
System.out.println (s.length()); //NullPointerException
String s="abc";
int i=Integer.parseInt(s); //NumberFormatException
a[10]=50; //ArrayIndexOutOfBoundsException
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 18
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 19
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 20
PATEL MANESH
2. These exceptions are direct They are the direct subclasses of the
subclasses of exception but not RuntimeException class.
extended from RuntimeException
class.
3. The code gives a compilation The code compiles without any
error in the case when a method error. These exceptions are the
throws a checked exception. results of user-created errors in
programming logic.
4. These exceptions mostly occur These exceptions occur mostly due
when the probability of failure is to programming mistakes.
too high.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 22
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 23
PATEL MANESH
4) Throw is used within the method. Throws is used with the method
signature.
5)
You cannot throw multiple
exceptions. You can declare multiple exceptions
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 25
PATEL MANESH
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 27