Translate

25 Temmuz 2014 Cuma

Implicit and Explicit



Implicity and Explicity &&   Implicit vs. Explicit Conversions

I'll give two example for implicty

1)
    public class implicit {
  
        public int value1;
        public int value2;
        
        public void total(){
            System.out.println( value1+ value2);
        }    
    }

 2)
    public class Explicit{
        
        public void getEmployee(public int value1,public int value2){
             System.out.println(value1+value2);
        }
     }

 ////(TEST)////

    public class Test {
  
     public static void main(String[] args) {
        implicit imp= new implicit ();
        imp.value1 = 1;
        imp.value2 = 2;
        imp.total();
        //here there isn't any parameter passing to class
        //we just call the method
        Explicit  explicit = new Explicit();
        explicit.total(1,2);
        //here we passed some values as paramater
        }
    }
  


Implicit vs. Explicit Conversions

 widening conversions, which are always safe from data loss, generally occur implicitly, while narrowing conversions, which run the risk of data loss, are usually performed explicitly.

            
public class Conversions
{
    public static void main(String[] args)
    {
        int i = 200;
        byte b = 10;
        
        int j = b;               
        byte c = (byte)i;        
        
        System.out.println(j);
        System.out.println(c);
    }
}

// Output

10
-56

   

Hiç yorum yok:

Yorum Gönder

Bu Blogda Ara