What is WrapperClass
• A Wrapper class is a class whose object
wraps or contains primitive data types.
• When we create an object to a wrapper
class, it contains a field and in this field,
we can store primitive data types. In other
words, we can wrap a primitive value into
a wrapper class object.
2.
Wrapper Classes forconverting
simple types
Simple Type Wrapper Class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
3.
Converting Primitive Numbersto Object
Numbers, using Constructor Methods
Constructor Calling Conversion Action
Integer intobj = new Integer(i); // Primitive integer to Integer object
Float floatobj = new Float(f); // Primitive float to Float object
Double doubleobj = new Double(d); // Primitive double to Double object
Long longobj = new Long(l); // Primitive long to Long object
Converting Object Numberto Primitive Numbers
Method Calling Conversion Action
int i = IntVal.intValue(); //Object to primitive
integer
Float f = FloatVal.floatValue(); //Object to primitive long
Double d = DoubleVal.doubleValue(); // Object to primitive double
Long l = LongVal.longValue(); // Object to primitive
double
Converting primitive Numbersto Strings, using
toString( ) method
Method Calling Conversion Action
str1 = Integer.toString(i); //Primitive integer to string
str2 = Float.toString(f); // Primitive float to string
str3 = Double.toString(d); // Primitive double to string
str4 = Long.toString(l); // Primitive long to string
8.
Cont.
int i =5;
Integer intobj = new Integer(i); // conversion of primitive int into
// Integer class object
String str = new String( );
str = intobj.toString(); // conversion of Integer into String
Or
int i = 5;
String str = new String();
str = Integer.toString();
9.
Converting String Objectsto Numeric Objects,
using the Static method ValueOf()
Method Calling Conversion Action
DoubleVal = Double.valueOf(str); // converts string to Double object
FloatVal = Float.valueOf(str); // converts string to Float object
IntVal = Integer.valueOf(str); // converts string to Integer object
LongVal = Long.valueOf(str); // converts string to Long object
10.
Converting Numeric Stringto Primitive Numbers,
using Parsing Methods
int i = Integer.parseInt(str); // converts string to primitive integer
long l = Long.parseLong(str) ; // converts string to primitive long
These methods throw a NumberFormatException if
the value of the str does not represent an integer.
11.
Strings in Java
•Strings represent a sequence of characters.
• The easiest way to represent a sequence of
characters in Java is by using a character array.
• In Java, strings are class objects and implemented
using two classes: String and StringBuffer.
• Declaration of string object:
String fname = new String(“Anil”);
• We can also create and use arrays that contain
strings, e.g:
String nameArray[ ] = new String[5];
12.
Method call Taskperformed
s2= s1.toLowerCase Converts string s1 to all lowercase
s2 = s1.toUpperCase Converts the string s1 to all Uppercase
s2 = s1.replace(‘x’, ‘y’); Replace all appearances of x with y
s2 = s1.trim(); Remove white spaces at the beginning
and end of the string s1
s1.equals(s2) Returns true if s1 is equal to s2
s1.equalsIgnoreCase(s2) Returns true is s1 = s2, ignoring the case
of characters.
s1.length() Gives the length of s1
s1.charAt(n) Gives nth character of s1
s1.compareTo(s2) Returns negative if s1 < s2, positive if s1
> s2, and zero if s1 is equal to s2.
s1.concat(s2) Concatenates s1 and s2.
Methods in String class
13.
Method call Taskperformed
s1.substring(n) Gives substring starting from nth character
s1.substring(n, m) Gives substring starting from nth character up to
mth
(not including mth
)
String.valueOf(p) Creates a string object of the parameter p (simple
type or object)
p.toString() Creates a string representation of the object p.
s1.indexOf(‘x’) Gives the position of the first occurrences of ‘x’
in the string s1.
s1.indexOf(‘x’, n) Gives the position of ‘x’ that occurs after nth
position in the string s1
String.valueOf(variable) Converts the parameter value of string
representation
14.
Example, String functions
classStringCheck
{
public static void main(String args[ ])
{
String s1 = new String("James");
String s2 = new String("Gosling");
System.out.print("n string s1 = "+ s1);
System.out.print("n string s2 =" + s2);
String s3 = s1.toLowerCase();
System.out.print("n After converting string " + s1 + " into lower case, i.e. s3 = " + s3);
String s4 = s3.toUpperCase();
System.out.print("n After converting string " + s3 + " into upper case, i.e. s4 = " + s4);
String s5 = s1.replace('J', 'G');
System.out.print("n After replacing the character J of " + s1 + " with G: " + s5);
Boolean b = s1.equals(s2);
System.out.print("n strings " + s1 + " and " + s2 + " are equal: " + b);
String s11 = new String("dennis");
String s22 = new String("DENNIS");
boolean b11 = s11.equals(s22);
System.out.print("n strings " + s11 + " and " + s22 + " are equal: " + b11);
15.
Cont.
boolean b22 =s11.equalsIgnoreCase(s22);
System.out.print("n After ignoring the case, strings " + s11 + " and " + s22 + " are
equal: " + b22);
char c = s1.charAt(4);
System.out.print("n The character at 4th position of " + s1 + " is: " + c );
int l = s1.length();
System.out.print("n The length of the string " + s1 + " is: " + l);
String s6 = s1.concat(s2);
System.out.print("nThe strings " +s1+ " and" +s2+ " after concatenation is:" + s6);
String s7 = s1.substring(3);
System.out.print("n The substring of " + s1 + " after 3rd position is: " + s7);
String s8 = s2.substring(2, 6);
System.out.print("n The substring of "+s2+" starting from 2nd postion to position”);
System.out.print(“ntless than 4th is: " + s8);
int i = s1.indexOf('a');
System.out.print("n Index of 'a' in the string " + s1 + " is: " + i);
}
}
StringBuffer class
• Stringclass creates strings of fixed lengths.
• StringBuffer class on other hand creates strings
of flexible length that can be modified in terms of
both length and content.
• One can insert characters and substrings in the
middle of string, or append another string to the
end.
18.
Commonly used StringBuffermethods
Method call Task performed
s1.setCharAt(n, ‘x’) Modifies the nth character of x
s1.append(s2) Appends the string s2 to s1 at the end
s1.insert(n, s2) Inserts the string s2 at the position n of the string
s1
s1.setLength(n) Sets the length of the string s1 to n. if n<s1.length()
s1 is truncated. If n > s1.length() zeros are added
to s1.