String handling &
Wrapper classes
String Handling:
There are four string related classes to handle strings.
1) java.lang.String
2) java.lang.StringBuffer
3) java.lang.StringBuilder
4) java.util.StringTokenizer
Every string literal itself an object of String class in
Java.
Example:
“Welcome” is an object of String class.
String s1=”Welcome”;=> This statement creates one object
in a string constant pool.
String s2=new String(“Hello”); => This statement creates
two string objects (one object created in a string constant pool & one more
object created outside the pool)
Note: String constant pool does not create duplicates.
Example:
class Demo
{
public
static void main(String args[])
{
String
s1=new String("hello");
String
s2=new String("hello");
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
}
}
Note: equals() method of String class compares the contents
of String objects whereas == operator compares the hash codes.
Differences between String, StringBuffer & StringBuilder
String StringBuffer StringBuilder
1) The object of String 1)
The object of StringBuffer 1) The
object of
class is immutable class is mutable. StringBuilder class is mutable.
2) Methods of String 2)
Methods of StringBuffer 2) Methods
of
class are not class are
synchronized
StringBuilder class
synchronized are
not synchronized
Immutable object: It
means the value of an object cannot be changed.
Synchronization: It is a mechanism that allows to access a
shared resource only one thread at a time.
StringTokenizer Class:
It allows an
application to break a string into tokens(words).
Example:
"Welcome
to Java" is a one string and it has 3 tokens(words).
Program to count the
no. of words in a given String
import
java.util.*;
class Demo
{
public static void
main(String args[])
{
String s="Welcome to Java";
StringTokenizer st=new StringTokenizer(s);
int n=st.countTokens();
System.out.println(n);
}
}
Program to iterate word
by word in a given string:
import
java.util.*;
class Demo
{
public static void
main(String args[])
{
String s="Welcome to Java";
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
Command
Line Arguments:
The
arguments that are passed at the command prompt are called command line
arguments.
Command line
arguments are received by main method only.
The
arguments are received string format only.
Examples:
1) class Demo
{
public static void
main(String args[])
{
for(String s : args)
{
System.out.println(s);
}
}
}
2) class Demo
{
public static void main(String
args[])
{
System.out.println(args[0]+args[1]);
}
}
To run the
above application:
C:\> java
Demo Taj Mahal
Output:
TajMahal
C:\> java
Demo 10 20
Output: 1020
because command line arguments are received as a string format only.
To convert
string format to other formats we need wrapper classes.
Wrapper
Classes:
Each of
Java’s 8 primitive data types has a class and those classes are called wrapper
classes because they wrap the data into an object.
List of primitive data
types:
1) byte 2) short 3) int 4) long 5) float 6)
double 7) char 8) boolean
List of wrapper
classes:
1) Byte 2) Short 3) Integer 4) Long 5) Float 6)
Double 7) Character 8) Boolean
Example:
class Demo
{
public static void main(String args[])
{
int
x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
System.out.println(x+y);
}
}
To run the
above application:
C:\> java
Demo 10 20
Output: 30
To covert primitive
type to reference type
int a=5;
Integer
i=new Integer(a);
To covert reference
type to primitive type:
Integer
i=new Integer(5);
int
x=i.intValue();
Auto
boxing:
The process
of converting primitive type to the corresponding reference type is known as
auto boxing.
Example:
int x=5;
Integer i=x;
=> It is called as auto boxing
Auto
unboxing:
The process
of converting reference type to the corresponding primitive type is known as
auto unboxing.
Example:
Integer
i=new Integer(5);
int x=i;
=> it is called as auto unboxing.
Both auto
boxing and auto unboxing features are introduced in JDK 1.5 verion in 2004.
No comments:
Post a Comment