Professional Documents
Culture Documents
Exploring Java - Lang & Java - Util: Accent e Technology Pvt. LTD
Exploring Java - Lang & Java - Util: Accent e Technology Pvt. LTD
Exploring Java - Lang & Java - Util: Accent e Technology Pvt. LTD
Wrapper Classes
Java uses simple types, such as int, byte, float and
char, for performance reasons.
At times, you will need to create an object
representation for one of these simple types.
To address this need, Java provides wrapper classes
that correspond to each of the simple types.
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue( )
long longValue( )
short shortValue( )
Wrapper - Example
class WrapperDemo
{
public static void main(String args[] )
{
Byte ob = new Byte((byte)125);
long obl = ob.longValue();
System.out.println("Byte " + ob + " is now long " + obl);
Double od = new Double(45.6783);
int odi = od.intValue();
System.out.println("Double " + od + " is now int " + odi);
Wrapper - Example
Float of = new Float(67.56);
short ofs = of.shortValue();
System.out.println("Float " + of + " is now short " + ofs);
Integer oi = new Integer(10000);
float oif = oi.floatValue();
System.out.println("Integer " + oi + " is now float " + oif);
Long ol = new Long(678345546);
double old = ol.doubleValue();
System.out.println("Long " + ol + " is now double " + old);
}
}
Conversion - Example
class ParseConversionDemo
{
public static void main(String args[ ])
{
String str = "100";
int num = 2008;
int i = Integer.parseInt (str);
System.out.println("String 100 is int " + i );
byte b = Byte.parseByte (str);
System.out.println("String 100 is byte " + b );
short s = Short.parseShort (str);
Conversion - Example
System.out.println("String 100 is short " + s );
long l = Long.parseLong (str);
System.out.println("String 100 is long " + l );
String sbs = Integer.toBinaryString(num);
System.out.println("int 2008 in binaryStr " + sbs);
String sos = Integer.toOctalString(num);
System.out.println("int 2008 in octalStr " + sos);
String shs = Integer.toHexString(num);
System.out.println("int 2008 in hexaStr " + shs);
}
}
Character - Example
class IsCharDemo
{
public static void main(String args[])
{
char a[] = {'a', '5', 'B', ' '};
for(int i=0; i < a.length ; i++)
{
if( Character.isDigit (a[i]))
System.out.println(a[i] + " is a digit.");
if( Character.isLetter (a[i]))
System.out.println(a[i] + " is a letter.");
Character - Example
if( Character.isWhitespace (a[i]))
System.out.println(a[i] + " is whitespace.");
if( Character.isUpperCase (a[i]))
System.out.println(a[i] + " is uppercase.");
if( Character.isLowerCase (a[i]))
System.out.println(a[i] + " is lowercase.");
}
}
}
Collections Overview
A collection is a group of objects.
The Java collections framework standardizes the
way in which groups of objects are handled by your
programs.
java.util is one of Java's most widely used packages.
Their applications include generating pseudorandom
numbers, manipulating date and time, and tokenizing
strings.
The collection framework defines several classes and
interfaces.
Collections Overview
The
collection
interfaces
determine
the
characteristics of a collection.
At the top of the interface hierarchy is Collection,
which defines the features common to all collections.
Subinterfaces add the attributes related to specific
types of collections.
Several classes, such as ArrayList, LinkedList
provide concrete implementations of the collection
interfaces.
Algorithms are static methods defined within the
Collections class that operate on collections.
ArrayList - Example
import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
ArrayList alOb = new ArrayList();
System.out.println("Initial size of al: " + alOb.size());
alOb.add("C");
alOb.add("A");
alOb.add("E");
alOb.add("B");
ArrayList - Example
alOb.add("D");
alOb.add("F");
alOb.add(1, "A2");
System.out.println("Size of al after additions: " + alOb.size());
System.out.println("Contents of al: " + alOb);
alOb.remove("F");
alOb.remove(2);
System.out.println("Size of al after deletions: " + alOb.size());
System.out.println("Contents of al: " + alOb);
}
}
LinkedList - Example
import java.util.*;
class LinkedListDemo
{
public static void main(String args[])
{
LinkedList llOb = new LinkedList();
llOb.add("F");
llOb.add("B");
llOb.add("D");
llOb.add("E");
llOb.add("C");
llOb.addLast("Z");
llOb.addFirst("A");
LinkedList - Example
llOb.add(1, "A2");
System.out.println("Original contents of llOb: " + llOb);
llOb.remove("F");
llOb.remove(2);
System.out.println("Contents of llOb after deletion: " + llOb);
llOb.removeFirst();
llOb.removeLast();
System.out.println("llOb after deleting first and last: "+ llOb);
Object val = llOb.get(2);
llOb.set(2, (String) val + " Changed");
System.out.println("llOb after change: " + llOb);
}
}
HashSet - Example
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
HashSet hs = new HashSet();
hs.add("B");
hs.add("A");
hs.add(new Integer(1000));
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
TreeSet - Example
import java.util.*;
class TreeSetDemo {
public static void main(String args[]) {
TreeSet ts = new TreeSet();
ts.add("C");
ts.add("A");
ts.add("E");
ts.add("Demo");
ts.add("Xplore");
ts.add("String");
System.out.println(ts);
}
}