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>
Essential Knowledge (2.B)
- The casting operators (int) and (double) can be used to created temporary value converted into a different data type.
- Casting a double value to an int causes the digits to the right of the decimal point to be truncated.
- Some programming code causes int values to be automatically cast(widened) to double values.
- 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>
Questions
1. what will be the output of (int) (2.5 * 3.0)
2. what will be the output of (double) 25 / 4
3. what will be the output of 6 / (double) 5
4. what will be the output of (int) (-8.63 - 0.5)
Essential Knowledge (5.B)
- 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.
- 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
- picture of quiz
- 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