Skip to main content

Q) What are wrapper classes?




A)
->for each primitive data type a corresponding java class is given in java.long.package.
These 8 library classes corresponding to 8 primitive data types are known as wrapper class.
->An instance of wrapper class wraps (encapsulates) a primitive value and hence the name.

Primitive data type
Wrapper class
int
Integer
byte
Byte
short
Short
long
Long
float
Float
double
Double
char
character
boolean
Boolean

->the java is not purely 100% object oriented, because it contains primitive data types. If any language contains primitive data types then that language is not pure object oriented.
->but the pure object oriented language is smalltalk.
->Wrapper object:
Object oriented manner an object is encapsulate the primitive value. That object is called wrapper object.
Ex:
Int a=10;
Integer i=nre Integer(10);
->to convert a primitive value into wrapper object create the object of wrapper class by supplying primitive alue as argument to the parameterized constructor of the wrapper class.
Ex:
Integer i= new Integer(10);
Boolean b=new Boolean(true);
Float f= new Float(1.1);
Double d=new Double(2.5);

->By calling xxxvalue(); method on the wrapper object we can get the primitive value.
Ex:
int a= i.intValue();
boolean flag= b.booleanValue();
double b=d.doubleValue();

-> Conversion of object type to primitive type.
->integer i=new Integer(10);  then the Integer class of encapsulated in the Integer object.

Ex:
class wrapperclass {
       public static void main(String[] args) {
                     Integer i=new Integer(10);
                     int c=40;
                     //intb=i.intValue(10);
                     int b=i+c; //doesn't work before jdk 1.5
                     System.out.println("b: "+b);
       }
}

Output:
b: 50

->implicit conversion of wrapper object to primitive type is known as auto unboxing. It is introduced in jdk1.5.
int b=i+c;
->in the above statement, Integer object I is implicitly converted into primitive value then arithmetic operation is performed. I.e. auto unboxing is implemented and then addition is performed.

Comments

Popular posts from this blog

Q) Explain repeated deletion of records.

A) //RepeatedDeletion.java import java.util.Scanner; import java.sql.*; class RepeatedDeletion {        public static void main(String[] args) {               Connection con= DriverManager. getConnection ( "jdbc:odbc:prince" , "scott" , "tiger" );               PreparedStatement ps=con.PreparedStatement( "DELETE FROM ACCOUNT WHERE ACCNO=?" );               Scanner s= new Scanner(System. in );               while ( true )               {                      System. out .println( "Enter accno : " );      ...

Introduction

1) What is not JDBC? 2) What is the purpose of JDBC? 3) What is ODBC? 4) What is JDBC? 5) What is JDBC architecture? 6) What is JDBC client? What are its responsibilities? 7) What is JDBC API?  8) What is driver manager? 9) What is JDBC driver? What is its role in java database connection? 10) What is database server?

Q) Explain about nested classes?

A) ->defining a class with in another class is known as nesting of the class. Ex: class A {        class B        {                      } //nested class, and inner class.because it is not static class. } //enclosing class or outer class. ->static class is declared in inside an another class then it does not become an inner class. Note: non-static nested class is known as “inner class”. In the above example class B is nested but not an inner class. Ex: class D {        static c        {                      } //nested class, but not an inner class , because of static class. } //outer class. ->we have three different kinds of inner classes. 1) Member inner class 2) Meth...