13:AutoBoxing and AutoUnboxing

AutoBoxing and AutoUnboxing  :
1.AutoBoxing  :
 java5.0 takes care of converting the primitive datatype into its respective wrapper class objects
Automatically is known as “AutoBoxing”.


Output:

Integer value is :5
Float value is : 5.6
Double value is : 8.9
package com.itarget.basics;
public class AutoBoxing {
public static void main(String[] args) {
         
/*int i=5;
Integer i1=new Integer(i);
System.out.println(i1);*/     //java4.0
         
          int i=5;
          Integer i1=i;               //AutoBoxing(java5.0)
          System.out.println("Intetger value is :"+i1);
          float f=5.6f;
          Float f1=f;
          System.out.println("Float value is :"+f1);
          double d=8.9;
          Double d1=d;
          System.out.println("Double value is :"+d1);      }    }
1.AutoUnBoxing  :
 java5.0 takes care of converting the  wrapper class objects into its respective  primitive datatype
Automatically is known as “AutoBoxing”.

package com.itarget.basics;

Output:

int value is :5
float value is :5.6

public class AutoUnBoxing {
          public static void main(String[] args) {
                  
          /*Integer i=new Integer(543);
          int i1=i.intValue();
          System.out.println(i1);*/
                  
          Integer i=new Integer(543);
          int i1=i;
          System.out.println("int value is :"+i1);
          Float f=new Float(58.98f);
          float f1=f;
          System.out.println("float value is :"+f1);
          }    }

                                                Wrapper Classes
Ø There are 8 datatypes are available in java.
Ø There are some situations where we need to represent primitive data types in terms of objects.
Ø To represent the primitive data types in terms of objects some classes are defined in java.lang.packagethose classes are known as a “wrapper classes”.
Ø All the wrapper classes contains two constructors.
Ø 1st is defined to accept the corresponding primitive data type variable(as parameter).
Ø 2nd constructor is defined to accept string value (as parameter) and convert that into object of corresponding wrapper class.
Ø In wrapper classes thre is no default constructor.

Primitive data types
Corresponding wrapper classes
Byte
Java.lang.Byte
Short
Java.lang.Short
int
Java.lang.Integer
Long
Java.lang.Long
Float
Java.lang.Float
Double
Java.lang.Double
Char
Java.lang.Char
Boolean
Java.lang.Boolean




public Java.lang.Byte(byte b){  }
public Java.lang.Byte(String str) { }
public Java.lang.Short (short s) { }
public Java.lang.Short(String str) { }
public Java.lang.Integer(int i) {  }
public Java.lang. Integer(String str) {  }
public Java.lang.Long(long l) {  }
public Java.lang.Long(String str) {  }
public Java.lang.Float(float f)  {  }
public Java.lang.Float(String str) {  }……….so  on

Purpose of  Wrapper classes:-

Wrapper classes are used

1.   To convert Primitive data types into Objects.
2.   To convert String value to Primitve Datatypes (or) Object of Primitive Datatypes.

package com.itarget.basics;
Output:-

143
public class integer {
public static void main(String[] args) {
          String str="143";
          Integer i = new Integer(str);
          System.out.println(i);
          }  }


Non-static Methods
Static Methods
Byte
byteValue(Byte b);
parseByte(String str)
Short
shortValue(Short s);
parseShort(String str)
Long
longValue(Long l);
parseLong(String str)
Integer
integerValue(Integer i);
parseInteger(String str)
Float
floatValue(Float f);
parseFloat(String str)
Double
doubleValue(Double d);
parseDouble(String str)

Ø  Non-static methods are used to “convert wrapper classes to Primitive datatype”.
Ø Static Methods are used to “convert String value to Primitive datatype”.



Example:-     
    Integer   i = new Integer(14);       -------à  primitive                   -------- Object
    int i = i.intvalue();                        -------à WrapperClass             ---------Primitive
                               String s= “143”;
  Integer   i1 = new Integer(s);        --------à  String                       ----------Wrapperclass
 int i1 =i1.intValue();                     ---------à Wrapperclass(String)   ------- Primitive   
                                                     
     Q. How to convert the .XML file into String   ?
     A.  This is a very simple tip on  how to load a XML file into XML Document obj. and then load it into
            a String object.
       This is a kind of a handy utility because when you call toString(); on XML Document object, it does not return you its contents but it returns the fullclassName ”System.Xml.XmlDocument

                                                 Conditional Statement
Ø We have 3 kinds of Conditional Statements and 3 kinds of Conditional Loops.

1.   “If”-conditional statement:-   Here condition contains always Boolean Expression

Ex;-   a>b, a<b, a==b, a!=b.

Ø (Condition = = true) the statements inside if block will be executed. Otherwise Statements will not be Executed.

Ex:-  


public class conditional {
void comparision(int x)
{
if(x>7)
{ System.out.println(x);
  System.out.println("Inside of if");
}
 if (x<=7)
{System.out.println("x is less than 7---->"+x);}
}
Output:-

8

Inside of it
public static void main(String[] args)
{
conditional m = new conditional();
m.comparision(8);
}}



public class conditional {
void comparision(int x)
{
Output:-

       8
Inside of if
Outside of if

if(x>7)
{ System.out.println(x);
  System.out.println("Inside of if");
}
System.out.println(“outside of it”);
}
Public static void main (String[] args)
{
Conditional m = conditional();
m.comparision(8);
}}

Note;-   if there is only one statement that need to be executed after the condition we don’t need to keep block if we want to execute more than one statement based on the condition. We need to use block after if comparision.
2.   “If-else” conditional statement:-
Ex:-
public class conditional {
void function1(int x)
{
Output:-

x>7
if(x<=7)
{   System.out.println("x<=7");  }
else
{ System.out.println("x>7");   }   }
public static void main(String[] args){
conditional m = new conditional();
m.function1(8);
} }







Regular Expression:-
1.   && (AND)   
2.   || (OR)
a
B
a && b
a || b
T
T
T
T
T
F
F
T
F
T
F
T
F
F
F
T

3.   “Else- If” Conditional Statement:-
Ex:-
public class conditional {
Output:-

I am out of the condition
void function1(int x)
{
if(x<=7)
{   System.out.println("x<=7");  }
else if (x == 10)
{  System.out.println("x=10");   }
else
{  System.out.println("I am out of the condition"); } }
public static void main(String[] args)
{ conditional m = new conditional();
m.function1(8); } }

If the condition if comparision is false then only the cursor will go to the Else-if Block.

Loop:-   Loop is  a Block of code that will be repeatedly executed one by one up to condition

Syntax:-
// initialization to  variable
While (condition) {
//statements
Variable increment (or) decrement        
} 

Codings standards:-
                     According to coding standards we should not create objects of classes inside the loop. Even if it is essential to create declare a variable before the loop and assign object to it inside the loop.
public class conditional {
Output:-

while-loop
Even number is8
while-loop
void evenNumbers(int num)
{
 int i=0;
 while(i<=num)
{   if (i%2 == 0)
 System.out.println("Even number is"+i); 
 System.out.println("while-loop");
i++;  }  }
public static void main(String[] args)
{
conditional c = new conditional();
c.evenNumbers(8);
} }

do-while:-(syntax)
do{
statements(s)
} while (expression);

 
Output:-

0,2,4,6,8
public class conditional {
void evenNumbers(int num){ int i=0;
do { if(i%2 == 0)
System.out.println(i); i++;    }
           while(i<=num);  }
public static void main(String[] args){
conditional c = new conditional();
c.evenNumbers(8);} }

for-loop;-
For( initialization; condition; increment/decrement)
{
Statements
}

No comments:

Post a Comment