Wrapper Class and Vector in Java

1.Wrapper Classes

Primitive Types – Wrapper Class
byte – Byte
short – Short
char – Character
boolean – Boolean
int – Integer
long – Long
float – Float
double – Double

package org.vit.java.collections;
public class WrapperTest {
 
public static void main(String[] args) {
double d1 = 1.1234;
Double d = new Double(d1);
System.out.println(d.intValue());
System.out.println(d.doubleValue());
System.out.println(d.floatValue());
System.out.println(d.isNaN());
System.out.println(d.byteValue());
System.out.println(d.compareTo(0.0));
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
 
}
 
}

Output :
1
1.1234
1.1234
false
1
1
4.9E-324
1.7976931348623157E308

2.Type Casting

package org.vit.java.collections;
 
import javax.swing.JOptionPane;
public class TypeCasting
{
 
public static void main(String[] args)
{
int x =10;
String str = String.valueOf(x);
float f = Float.parseFloat(str);
JOptionPane.showMessageDialog(null,x);
}
 
}

Output:
The result is 10.0

3. Vector Example

package org.vit.java.collections;
 
import java.util.Vector;
 
public class VectorTest {
 
public static void main(String[] args) {
Vector v = new Vector();
System.out.println("The Default Capacity is "+v.capacity());
v.add("antony");
v.add("raj");
v.add("10");
v.add("23.4");
v.add("kamal");
v.add("234");
v.add("214.40");
v.add("java");
v.add("2.0");
v.add("2.4");
v.add("11");
v.add("end");
System.out.println("The Vector is growing its size doubled s"+v.capacity());
System.out.println("The first element of vector is "+v.firstElement());
System.out.println("The last element of vector is "+v.lastElement());
System.out.println("The 10th element of vector is "+v.get(9));
System.out.println("The size of the vector is "+v.size());
v.set(0, "start");
System.out.println("The first element of vector is "+v.firstElement());
v.remove(9);
System.out.println("The Vector Values are");
for(int i=0;i<v.size();i++)>
{
System.out.print(v.get(i)+" ");
}
v.clear();
}
}

Output :
The Default Capacity is 10
The Vector is growing its size doubled s20
The first element of vector is antony
The last element of vector is end
The 10th element of vector is 2.4
The size of the vector is 12
The first element of vector is start
The Vector Values are
start raj 10 23.4 kamal 234 214.40 java 2.0 11 end

Leave a Response