Topic 1.5: Casting and Ranges of Variables

Understanding Casting and Ranges of Variables

When working with variables in Java, it's essential to understand how different data types interact, especially when converting one type to another. This is called casting. Let's dive into the key concepts of casting and the ranges of variables.

1. Casting Operators: (int) and (double)

Casting is the process of converting a variable from one type to another. In Java, you can use casting operators to create a temporary value of a different data type.

Example:

double pi = 3.14159;

int intPi = (int) pi;  // Casts the double value of pi to an integer

System.out.println(intPi);  // Output: 3

In this example, the value 3.14159 is cast to an integer, resulting in 3.

 

2. Truncation When Casting Double to Int

When you cast a double to an int, the digits to the right of the decimal point are truncated (cut off), not rounded.

Example:

double number = 9.99;

int truncatedNumber = (int) number;  // Truncates the decimal part

System.out.println(truncatedNumber);  // Output: 9

 

3. Automatic Casting (Widening)

Java automatically casts (widens) an int to a double when necessary. This happens because a double can hold larger and more precise values than an int.

Example:

int count = 10;

double newCount = count;  // Automatically casts int to double

System.out.println(newCount);  // Output: 10.0

 

4. Rounding a Double to the Nearest Integer

To round a double to the nearest integer, you can use:

Example:

double number = 4.7;

int roundedNumber = (int)(number + 0.5);  // Rounds to the nearest integer

System.out.println(roundedNumber);  // Output: 5

 

5. Ranges of Integer Values

In Java, int values are represented using 4 bytes of memory, which means they must fall within the range:

 

6. Integer Overflow

If an expression evaluates to a value outside the int range, an integer overflow occurs, resulting in an incorrect value within the allowed range.

Example:

int largeNumber = Integer.MAX_VALUE;

int overflow = largeNumber + 1;  // Causes overflow

System.out.println(overflow);  // Output: -2147483648

Real-World Example: Converting Temperature

Let's say you want to convert a temperature from Fahrenheit to Celsius. You might get a decimal result that you need to cast to an integer.

Example:

double fahrenheit = 98.6;

double celsius = (fahrenheit - 32) * 5 / 9;

int roundedCelsius = (int)(celsius + 0.5);  // Rounds to nearest integer

System.out.println("Temperature in Celsius: " + roundedCelsius);  // Output: 37

 

Full Program Example

Here's a full Java program demonstrating the concepts of casting and ranges of variables:

java

Copy code

public class CastingAndRanges {

    public static void main(String[] args) {

        // Example of casting double to int

        double originalDouble = 5.99;

        int castedInt = (int) originalDouble;  // Truncates to 5

        System.out.println("Casted int: " + castedInt);

 

        // Automatic casting (widening) from int to double

        int originalInt = 10;

        double widenedDouble = originalInt;  // Automatically casts to 10.0

        System.out.println("Widened double: " + widenedDouble);

 

        // Rounding a double to the nearest integer

        double numberToRound = 7.6;

        int roundedInt = (int)(numberToRound + 0.5);  // Rounds to 8

        System.out.println("Rounded int: " + roundedInt);

 

        // Demonstrating integer overflow

        int maxInt = Integer.MAX_VALUE;

        int overflowInt = maxInt + 1;  // Causes overflow

        System.out.println("Overflowed int: " + overflowInt);

    }

}

 

Programming Problem

Write a program that:

  1. Prompts the user to enter a temperature in Fahrenheit.
  2. Converts the temperature to Celsius.
  3. Rounds the Celsius temperature to the nearest integer.
  4. Prints the rounded Celsius temperature.

Hints:

Sample Solution:

import java.util.Scanner;

 

public class TemperatureConverter {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

       

        System.out.print("Enter temperature in Fahrenheit: ");

        double fahrenheit = scanner.nextDouble();

       

        double celsius = (fahrenheit - 32) * 5 / 9;

        int roundedCelsius = (int)(celsius + 0.5);

       

        System.out.println("Temperature in Celsius: " + roundedCelsius);

    }

}

Now you have a solid understanding of casting and ranges of variables in Java! Practice these concepts by writing your own programs and experimenting with different values. Happy coding!