Casting Variable

CASTING

"Change the data type of variable from one type to another."

Example

  • Int variable —-» double
  • Double variable —-» Int
  • 6.0 —> 6
  • 80 —> 80.0
/*
* Purpose: Demonstrate casting using division.
*/
public class CastingNumbers {
    public static void main(String[] args) {
        System.out.println(6 / 4); // if I just do divide 6 by 4, these are both int value, so I'm going to wing up with an int result.
        System.out.println (6.0 / 4); // Since one of those value is a double value, output will be double 
        System.out.println(6 / 4.0); // (Same Reason) I wind up with the result of 1.5
    }
}

<!DOCTYPE html>

Java Code Runner

Essential Knowledge (2.B)

  1. The casting operators (int) and (double) can be used to created temporary value converted into a different data type.
  2. Casting a double value to an int causes the digits to the right of the decimal point to be truncated.
  3. Some programming code causes int values to be automatically cast(widened) to double values.
  4. Value of type double can be rounded to the nearest integer (int) (x + 0.5) + (int) (x - 0.5) for negative numbers.

Ranges of variable

int: Stored by using a finite amount (4 bytes) of memory.

Integer

Max: 2,147,483,647
Min: -2,147,483,648

Double

up to 14 - 15 digits for storage

Boolean

only 2 (true and false)

/*
* Purpose: Demonstrate Integer Range
*/
public class TooBigNumber {
    public static void main(String[] args) {
        int posInt = 2147483647; 
        int negInt = -2147483648;
        System.out.println(posInt + " "); 
        System.out.println(negInt);
    }
}
TooBigNumber.main(null);
2147483647 
-2147483648
/*
* Purpose: Demonstrate Integer.MAX_VALUE and Integer.MIN_VALUE.
*/
public class IntMinMax {
    public static void main(String[] args) {
        int posInt = Integer.MAX_VALUE; 
        int negInt = Integer.MIN_VALUE;
        System.out.println(posInt + " "); 
        System.out.println(negInt);
    }
}
IntMinMax.main(null);
2147483647 
-2147483648
/*
* Purpose: Demonstrate Integer Overflow
*/
public class TooBigNumber {
    public static void main(String[] args) {
        int posInt = Integer.MAX_VALUE; 
        int negInt = Integer.MIN_VALUE;
        System.out.println(posInt + 1); 
        System.out.println(negInt - 1);
    }
}
TooBigNumber.main(null);
-2147483648
2147483647

<!DOCTYPE html>

Java Code Runner

Questions

1. what will be the output of (int) (2.5 * 3.0)

7.0
7
7.5
7.50

2. what will be the output of (double) 25 / 4

6.50
6.0
6
6.25

3. what will be the output of 6 / (double) 5

1.0
1
1.2
1.125

4. what will be the output of (int) (-8.63 - 0.5)

-9
-13.63
-9.13
-8

Essential Knowledge (5.B)

  1. Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
  2. If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. This could result in an incorrect value within the allowed range.

Hack

  1. picture of quiz
  2. be creative (make a code that represent the lesson)

import java.util.Scanner;

public class DataTypeDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Demonstrate casting from double to int
        System.out.print("Enter a double value: ");
        double doubleValue = scanner.nextDouble();
        int intValue = (int) doubleValue; // Casting from double to int
        System.out.println("Casting " + doubleValue + " to int results in " + intValue);

        // Demonstrate automatic widening from int to double
        int intValue2 = 5;
        double doubleValue2 = intValue2; // Automatic casting from int to double
        System.out.println("Automatic casting from int to double: " + doubleValue2);

        // Demonstrate rounding to the nearest integer
        System.out.print("Enter a double value to round: ");
        double valueToRound = scanner.nextDouble();
        int roundedValue = (int) (valueToRound + 0.5); // Rounding formula
        System.out.println("Rounded value: " + roundedValue);

        scanner.close();
    }
}

DataTypeDemo.main(null);

Enter a double value: Casting 2.9 to int results in 2
Automatic casting from int to double: 5.0
Enter a double value to round: Rounded value: 2