Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Khác biệt giữa Widening Casting (Implicit) và Narrowing Casting (Explicit) trong Java?


Type casting trong Java được dùng để chuyển đổi đối tượng hoặc biến từ kiểu này sang kiểu khác. Khi chuyển đổi hoặc gán kiểu dữ liệu này sang kiểu khác, chúng có thể không tương thích. Nếu phù hợp thì sẽ diễn ra suôn sẻ, ngược lại có khả năng mất dữ liệu.

Các loại Type Casting trong Java

Java Type Casting được phân loại thành hai loại.

  • Widening Casting (Implicit) – Automatic Type Conversion
  • Narrowing Casting (Explicit) – Cần Explicit Conversion

Widening Casting (kiểu nhỏ sang kiểu lớn)

Widening Type Conversion có thể xảy ra nếu cả hai kiểu tương thích và kiểu đích lớn hơn kiểu nguồn. Widening Casting diễn ra khi hai kiểu tương thích và kiểu đích lớn hơn kiểu nguồn.

Ví dụ 1

public class ImplicitCastingExample { 
    public static void main(String args[]) { 
        byte i = 40; 
        // No casting needed for below conversion 
        short j = i; 
        int k = j; 
        long l = k; 
        float m = l; 
        double n = m; 
        System.out.println("byte value : "+i); 
        System.out.println("short value : "+j); 
        System.out.println("int value : "+k); 
        System.out.println("long value : "+l); 
        System.out.println("float value : "+m); 
        System.out.println("double value : "+n); 
    } 
}

Kết quả

byte value : 40 short value : 40 int value : 40 long value : 40 float value : 40.0 double value : 40.0

Widening Casting của Class Type

Trong ví dụ dưới, class Child là kiểu nhỏ hơn, chúng ta gán nó cho kiểu Parent lớn hơn nên không cần casting.

Ví dụ 2

class Parent { 
    public void display() { 
        System.out.println("Parent class display() called"); 
    } 
} 
public class Child extends Parent { 
    public static void main(String args[]) { 
        Parent p = new Child(); 
        p.display(); 
    } 
}

Kết quả

Parent class display() method called

Narrowing Casting (kiểu lớn sang kiểu nhỏ)

Khi gán kiểu lớn cho kiểu nhỏ, Explicit Casting là bắt buộc.

Ví dụ 1

public class ExplicitCastingExample { 
    public static void main(String args[]) { 
        double d = 30.0; 
        // Explicit casting is needed for below conversion 
        float f = (float) d; 
        long l = (long) f; 
        int i = (int) l; 
        short s = (short) i; 
        byte b = (byte) s; 
        System.out.println("double value : "+d); 
        System.out.println("float value : "+f); 
        System.out.println("long value : "+l); 
        System.out.println("int value : "+i); 
        System.out.println("short value : "+s); 
        System.out.println("byte value : "+b); 
    } 
}

Kết quả

double value : 30.0 float value : 30.0 long value : 30 int value : 30 short value : 30 byte value : 30


Narrowing Class Type

Khi gán kiểu lớn cho kiểu nhỏ, thì cần explicitly typecast.

Ví dụ 2

class Parent { 
    public void display() { 
        System.out.println("Parent class display() method called"); 
    } 
} 
public class Child extends Parent { 
    public void display() { 
        System.out.println("Child class display() method called"); 
    } 
    public static void main(String args[]) { 
        Parent p = new Child(); 
        Child c = (Child) p; 
        c.display(); 
    } 
}

Kết quả

Child class display() method called