Computer Science

Java Type Casting

Java Type Casting is the process of converting one data type into another data type. It is used when a value is assigned to a variable of a different data type or when an operation is performed between two variables of different data types. Type casting can be either implicit or explicit.

Written by Perlego with AI-assistance

3 Key excerpts on "Java Type Casting"

  • 100+ Solutions in Java
    eBook - ePub

    100+ Solutions in Java

    A Hands-On Introduction to Programming in Java: A Hands-On Introduction to Programming in Java (English Edition)

    Here, different types of variables are declared and initialized with appropriate values. Note that the meaningful names have been used for variables based on the type of value they store. The following screenshot shows the output of the code:
    Figure 2.2: Variables of different data types

    2.4 Type casting

    Java supports the conversion of one type into another by using the type casting feature. Type casting can be of two types, which are as follows:
    • Implicit type casting : When data of a type is assigned to a variable of the similar or larger type, it leads to implicit or automatic conversion of the smaller type to the larger type. This is also called type promotion in which even if one operand of an expression is larger, the result of the whole expression is promoted to the larger type. For example, int num = 20; \double numD = num; // assigning integer value to double type
    • Explicit type casting: Java supports conversion of a higher type to a lower type, however, at the cost of loss of precision. This is called explicit casting. For example, float numF = 45.65f; int num = (int) numF; // converting a float value to integer type
    Here, the fractional part of the number numF will be lost and the result will be saved in the integer type variable num .

    2.5 Literals and escape sequences

    The values assigned to variables are called literals. Here are the different types of literals assigned to variables of the corresponding type:
    • Integer literals : Integer literals can be any whole numbers. Decimal values with base 10 from 0 to 9, such as 40, binary values with base 2, that is 0 and 1, such as 0c01001, hexadecimal values with base 16, that is, numbers 0 to 9 and letters A to F, such as 0x1d.
    • Floating-point literals : Floating-point literals are numbers with fractional components such as 2.9, 2.11, etc. The use of E or e at the end of the number indicates exponentiation. For example, 8.37E6, e+394, etc.
      With Java SE 7, the support for the underscore (_) character in numeric literals was added to improve the readability of large data. However, a number cannot begin or end with an underscore. An underscore cannot be placed beside the decimal point in a floating-point number, before a suffix, L or F, before or after binary/hexadecimal symbols like b or x. Some examples of valid use of underscore are 9449_3848_3948_2944L, 2.43_37F, 0b11010111_11100100_00010111. Examples of invalid use of underscores are _3837, 2_.43_37F, 0x_83.
  • 100+ Solutions in Java - 2nd Edition
    eBook - ePub

    100+ Solutions in Java - 2nd Edition

    Everything you need to know to develop Java applications (English Edition)

    Here, different types of variables are declared and initialized with appropriate values. Note that the meaningful names have been used for variables based on the type of value they store. The following screenshot shows the output of the code:
    Figure 2.2: Variables of different data types
    2.4 Type casting Java supports the conversion of one type into another by using the type casting feature. Type casting can be of two types, which are:
    • Implicit type casting : When data of a type is assigned to a variable of the similar or larger type, it leads to implicit or automatic conversion of the smaller type to larger. This is also called type promotion, in which even if one operand of an expression is larger, the result of the whole expression is promoted to the larger type. For example, int num = 20; double numD = num; // assigning integer value to double type
    • Explicit type casting : Java supports conversion of a higher type to a lower type, however, at the cost of loss of precision. This is called explicit casting. For example, float numF = 45.65f; int num = (int) numF; // converting a float value to integer type
      Here, the fractional part of the number numF will be lost and the result will be saved in integer type variable num .
    2.5 Literals and escape sequences The values assigned to variables are called literals. Listed below are the different types of literals assigned to variables of the corresponding type:
    • Integer literals : Integer literals can be any whole number. Decimal values with base 10 from 0 to 9, such as 40, binary values with base 2, that is 0 and 1, such as 0c01001, hexadecimal values with base 16, that is numbers 0 to 9 and letters A to F, such as 0x1d.
    • Floating-point literals : Floating-point literals are numbers with fractional components such as 2.9, 2.11, and so on. The use of E or e at the end of the number indicates exponentiation. For example, 8.37E6, e+394, and so on.
    • With Java SE 7, the support for underscore (_ ) character in numeric literals was added to improve the readability of large data. However, a number cannot begin or end with an underscore. Underscore cannot be placed beside the decimal point in a floating-point number, before a suffix, L or F , before or after binary/hexadecimal symbols like b or x
  • Android Development with Kotlin
    • Marcin Moskala, Igor Wojda(Authors)
    • 2017(Publication Date)
    • Packt Publishing
      (Publisher)
    smart casts .
    In Kotlin, we can perform a few types of casts:
    • Cast objects to different types explicitly (safe cast operator)
    • Cast objects to different types or nullable types to non-nullable types implicitly (smart cast mechanism)
    Passage contains an image

    Safe/unsafe cast operator

    In strongly typed languages, such as Java or Kotlin, we need to convert values from one type to another explicitly using the cast operator. A typical casting operation is taking an object of one particular type and turning it into another object type that is its supertype (upcasting), subtype (downcasting), or interface. Let's start with a small remainder of casting that could be performed in Java:
    Fragment fragment = new ProductFragment(); ProductFragment productFragment = (ProductFragment) fragment;
    In the preceding example, there is an instance of ProductFragment that is assigned to a variable storing Fragment data type. To be able to store this data into the productFragment variable that can store only the ProductFragment data type, so we need to perform an explicit cast. Unlike Java, Kotlin has a special as keyword representing the unsafe cast operator to handle casting:
    val fragment: Fragment = ProductFragment() val productFragment: ProductFragment = fragment as ProductFragment
    The ProductFragment variable is a subtype of Fragment, so the preceding example will work fine. The problem is that casting to an incompatible type will throw the exception ClassCastException. That's why the as operator is called an unsafe cast operator:
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.