ExactConversionsSupport (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.lang.runtime, class: ExactConversionsSupport
Keywords: java.lang.runtime.ExactConversionsSupport class

# Class ExactConversionsSupport

java.lang.Object
java.lang.runtime.ExactConversionsSupport

public final class ExactConversionsSupport extends Object
A testing conversion of a value is exact if it yields a result without loss of information or throwing an exception. Otherwise, it is inexact. Some conversions are always exact regardless of the value. These conversions are said to be unconditionally exact.

For example, a conversion from `int` to `byte` for the value 10 is exact because the result, 10, is the same as the original value. In contrast, if the `int` variable `i` stores the value 1000 then a narrowing primitive conversion to `byte` will yield the result -24. Loss of information has occurred: both the magnitude and the sign of the result are different than those of the original value. As such, a conversion from `int` to `byte` for the value 1000 is inexact. Finally a widening primitive conversion from `byte` to `int` is unconditionally exact because it will always succeed with no loss of information about the magnitude of the numeric value.

The methods in this class provide the run-time support for the exactness checks of testing conversions from a primitive type to primitive type. These methods may be used, for example, by Java compiler implementations to implement checks for `instanceof` and pattern matching runtime implementations. Unconditionally exact testing conversions do not require a corresponding action at run time and, for this reason, methods corresponding to these exactness checks are omitted here.

The run time conversion checks examine whether loss of information would occur if a testing conversion would be to be applied. In those cases where a floating-point primitive type is involved, and the value of the testing conversion is either signed zero, signed infinity or `NaN`, these methods comply with the following:

- Converting a floating-point negative zero to an integer type is considered inexact.
- Converting a floating-point `NaN` or infinity to an integer type is considered inexact.
- Converting a floating-point `NaN` or infinity or signed zero to another floating-point type is considered exact.

Implementation Note:
  Some exactness checks describe a test which can be redirected safely through one of the existing methods. Those are omitted too (i.e., `byte` to `char` can be redirected to `isIntToCharExact(int)`, `short` to `byte` can be redirected to `isIntToByteExact(int)` and similarly for `short` to `char`, `char` to `byte` and `char` to `short` to the corresponding methods that take an `int`).
Since:
  23
See Also:
  - JLS 5.7.1 Exact Testing Conversions
  - JLS 5.7.2 Unconditionally Exact Testing Conversions
  - JLS 15.20.2 The instanceof Operator

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static boolean`
  `isDoubleToByteExact(double n)`
  Exactness method from double to byte
  `static boolean`
  `isDoubleToCharExact(double n)`
  Exactness method from double to char
  `static boolean`
  `isDoubleToFloatExact(double n)`
  Exactness method from double to float
  `static boolean`
  `isDoubleToIntExact(double n)`
  Exactness method from double to int
  `static boolean`
  `isDoubleToLongExact(double n)`
  Exactness method from double to long
  `static boolean`
  `isDoubleToShortExact(double n)`
  Exactness method from double to short
  `static boolean`
  `isFloatToByteExact(float n)`
  Exactness method from float to byte
  `static boolean`
  `isFloatToCharExact(float n)`
  Exactness method from float to char
  `static boolean`
  `isFloatToIntExact(float n)`
  Exactness method from float to int
  `static boolean`
  `isFloatToLongExact(float n)`
  Exactness method from float to long
  `static boolean`
  `isFloatToShortExact(float n)`
  Exactness method from float to short
  `static boolean`
  `isIntToByteExact(int n)`
  Exactness method from int to byte
  `static boolean`
  `isIntToCharExact(int n)`
  Exactness method from int to char
  `static boolean`
  `isIntToFloatExact(int n)`
  Exactness method from int to float
  `static boolean`
  `isIntToShortExact(int n)`
  Exactness method from int to short
  `static boolean`
  `isLongToByteExact(long n)`
  Exactness method from long to byte
  `static boolean`
  `isLongToCharExact(long n)`
  Exactness method from long to char
  `static boolean`
  `isLongToDoubleExact(long n)`
  Exactness method from long to double
  `static boolean`
  `isLongToFloatExact(long n)`
  Exactness method from long to float
  `static boolean`
  `isLongToIntExact(long n)`
  Exactness method from long to int
  `static boolean`
  `isLongToShortExact(long n)`
  Exactness method from long to short

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Method Details

  - ### isIntToByteExact

    public static boolean isIntToByteExact(int n)
    Exactness method from int to byte
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isIntToShortExact

    public static boolean isIntToShortExact(int n)
    Exactness method from int to short
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isIntToCharExact

    public static boolean isIntToCharExact(int n)
    Exactness method from int to char
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isIntToFloatExact

    public static boolean isIntToFloatExact(int n)
    Exactness method from int to float
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToByteExact

    public static boolean isLongToByteExact(long n)
    Exactness method from long to byte
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToShortExact

    public static boolean isLongToShortExact(long n)
    Exactness method from long to short
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToCharExact

    public static boolean isLongToCharExact(long n)
    Exactness method from long to char
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToIntExact

    public static boolean isLongToIntExact(long n)
    Exactness method from long to int
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToFloatExact

    public static boolean isLongToFloatExact(long n)
    Exactness method from long to float
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isLongToDoubleExact

    public static boolean isLongToDoubleExact(long n)
    Exactness method from long to double
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isFloatToByteExact

    public static boolean isFloatToByteExact(float n)
    Exactness method from float to byte
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isFloatToShortExact

    public static boolean isFloatToShortExact(float n)
    Exactness method from float to short
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isFloatToCharExact

    public static boolean isFloatToCharExact(float n)
    Exactness method from float to char
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isFloatToIntExact

    public static boolean isFloatToIntExact(float n)
    Exactness method from float to int
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isFloatToLongExact

    public static boolean isFloatToLongExact(float n)
    Exactness method from float to long
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToByteExact

    public static boolean isDoubleToByteExact(double n)
    Exactness method from double to byte
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToShortExact

    public static boolean isDoubleToShortExact(double n)
    Exactness method from double to short
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToCharExact

    public static boolean isDoubleToCharExact(double n)
    Exactness method from double to char
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToIntExact

    public static boolean isDoubleToIntExact(double n)
    Exactness method from double to int
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToLongExact

    public static boolean isDoubleToLongExact(double n)
    Exactness method from double to long
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type

  - ### isDoubleToFloatExact

    public static boolean isDoubleToFloatExact(double n)
    Exactness method from double to float
    Implementation Requirements:
      relies on the notion of representation equivalence defined in the specification of the Double class.
    Parameters:
      `n` - value
    Returns:
      true if and only if the passed value can be converted exactly to the target type