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) Retrieving data from database?

A)   ->to retrieve the data from database we need to submit SELECT statement from JDBC application. -> executeQuery() method of statement object is used for this purpose. This method has the fallowing ResultSet executeQuery(String sql) throws SQLException Ex: ResultSet rs = st.executeQuery(“SELECT * FROM ACCOUNT”); ->objectoriented representation of tables formate data is called ResultSet object. Ao the driver created one object i.e. ResultSet object. ->object orientation representation of a table of records returned from db is nothing but “ ResultSet ” object.  

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 : " );      ...

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.