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) What is JDBC driver? What is its role in java database connection?

A) ->A translation software written in java according o JDBC specification is nothing but “JDBC driver”. -> JDBC driver implements JDBC API. Use single API. JDBC driver role: They are 4 roles   1)       Establishing connection between JDBC client and database server. 2)       Receiving JDBC method calls made by JDBC client while performing database (CRUD) operations. 3)       Translating java method calls into DBMS understandable calls and forwarding them to database server. 4)       Receiving the results from database server. Translating them into java format and handling over the same to the JDBC client.

Q) What is ODBC?

A) ->open database connectivity is a technology from MS that enables any language program (other than java) to communication with database Management System. ->in java program ODBC is directly not allowed for the fallowing reasons 1) OS dependency 2) Procedural oriented approach 3) Security thread

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...