Ép Kiểu Trong Java (Type Casting) - VietMX's Blog

Table of Contents

  • Ép kiểu là gì?
  • Nới rộng (widening)
  • Thu hẹp (narrowwing)
  • Bảng mô tả ép kiểu trong Java

Ép kiểu là gì?

Ép kiểu là việc gán giá trị của một biến có kiểu dữ liệu này sang biến khác có kiểu dữ liệu khác.

Ví dụ:

float soLe = 19.7f; int soNguyen = (int)soLe + 1;

Trong ví dụ trên, giá trị soLe được đổi thành giá trị nguyên 19. Sau đó, nó được cộng với 1 và kết quả là giá trị 20 được lưu vào soNguyen .

Trong Java, có hai loại ép kiểu dữ liệu:

Nới rộng (widening)

Nới rộng (widening): Là quá trình làm tròn số từ kiểu dữ liệu có kích thước nhỏ hơn sang kiểu có kích thước lớn hơn. Kiểu biến đổi này không làm mất thông tin. Ví dụ chuyển từ int sang float. Chuyển kiểu loại này có thế được thực hiện ngầm định bởi trình biên dịch.

byte -> short -> int -> long -> float -> double

public class TestWidening { public static void main(String[] args) { int i = 100; long l = i; // không yêu cầu chỉ định ép kiểu float f = l; // không yêu cầu chỉ định ép kiểu System.out.println("Giá trị Int: " + i); // Giá trị Int: 100 System.out.println("Giá trị Long: " + l); // Giá trị Long: 100 System.out.println("Giá trị Float: " + f); // Giá trị Float: 100.0 } }

Thu hẹp (narrowwing)

Thu hẹp (narrowwing): Là quá trình làm tròn số từ kiểu dữ liệu có kích thước lớn hơn sang kiểu có kích thước nhỏ hơn. Kiểu biến đổi này có thể làm mất thông tin như ví dụ ở trên. Chuyển kiểu loại này không thể thực hiện ngầm định bởi trình biên dịch, người dùng phải thực hiện chuyển kiểu tường minh.

double -> float -> long -> int -> short -> byte

public class TestNarrowwing { public static void main(String[] args) { double d = 100.04; long l = (long) d; // yêu cầu chỉ định kiểu dữ liệu (long) int i = (int) l; // yêu cầu chỉ định kiểu dữ liệu (int) System.out.println("Giá trị Double: " + d); System.out.println("Giá trị Long: " + l); System.out.println("Giá trị Int: " + i); } }

Bảng mô tả ép kiểu trong Java

Ép kiểuChuyển sang kiểu
Từ kiểubooleanbyteshortcharint longfloatdouble
booleanNoNoNoNoNoNoNo
byteNoYesCastYesYesYesYes
shortNoCastCastYesYesYesYes
charNoCastCastYesYesYesYes
int NoCastCastCastYesYesYes
longNoCastCastCastCastYesYes
floatNoCastCastCastCastCastYes
doubleNoCastCastCastCastCastCast

Trong đó

  • -: chính nó
  • No: không ép kiểu được
  • Yes: thực hiện ép kiểu nới rộng (widening)
  • Cast: thực hiện ép kiểu thu hẹp (narrowwing)

Related posts:

So sánh HashSet, LinkedHashSet và TreeSet trong JavaXML-Based Injection in SpringThe XOR Operator in Java@Lookup Annotation in SpringComparing Long Values in JavaHướng dẫn Java Design Pattern – Intercepting FilterSpring MVC Content NegotiationJava Program to Implement Fermat Factorization AlgorithmJava Program to implement Bit MatrixJava Program to Implement Find all Forward Edges in a GraphGuide to the Java Queue InterfaceHow to Delay Code Execution in JavaSimple Single Sign-On with Spring Security OAuth2REST Pagination in SpringConvert char to String in JavaA Guide to Java 9 ModularitySo sánh ArrayList và LinkedList trong JavaLinkedHashSet trong Java hoạt động như thế nào?Introduction to Spliterator in JavaHướng dẫn Java Design Pattern – PrototypeSpring Security Login Page with ReactPhương thức forEach() trong java 8Testing an OAuth Secured API with Spring MVCJava Program to Perform Addition Operation Using Bitwise OperatorsJava Program to Perform Searching in a 2-Dimension K-D TreeJava Program to Implement the linear congruential generator for Pseudo Random Number GenerationJava – File to ReaderA Guide to JUnit 5Java Program to Check if it is a Sparse MatrixJava Program to Check Whether a Directed Graph Contains a Eulerian CycleJava Program to Implement the Hill CypherConnect through a Proxy

Từ khóa » ép Kiểu Java