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)
public class FruitWeightCalculator {
public static void main(String[] args) {
// Weights of fruits in grams (integers)
int weightOfAppleInGrams = 150; // 0.15 kg
int weightOfBananaInGrams = 120; // 0.12 kg
int weightOfCherryInGrams = 5; // 0.005 kg
// Calculating average weight in grams
int totalWeight = weightOfAppleInGrams + weightOfBananaInGrams + weightOfCherryInGrams;
int averageWeightInGrams = totalWeight / 3;
System.out.println("Average weight in grams: " + averageWeightInGrams);
// Type casting to float for precision in kilograms
float averageWeightInKilograms = (float) averageWeightInGrams / 1000;
System.out.println("Average weight in kilograms: " + averageWeightInKilograms);
}
}
FruitWeightCalculator.main(null);
Average weight in grams: 91
Average weight in kilograms: 0.091