Casting and Ranges of Variables
Introduction: 1.6.0
- Type Casting is used to convert values between types
- Int and double casting operators are used next to a value to create a temporary value in a different type
(double) 1/3
(int) 0.573
- Truncates the decimal
- Can be used to round decimals
int roundedPositive = (int)(input + 0.5);
int roundedNegative = (int)(input - 0.5);
Computer numbers are weird
- Java limits the number of digits for a double to ~14-15
- Repeating numbers like
1.0/3.0
will be truncated
- Repeating numbers like
int
values only have ~4 bytes of memory- Max value for an integer is
2147483647
- Min value for an integer is
-2147483648
- Storing values outside of that range will result in integer overflow
- The values will wrap around and be stored incorrectly
0xffffffffffffffff
+0x1
->0x0
- https://en.wikipedia.org/wiki/Integer_overflow
- To store larger values, use
long
(Not on the exam)- Can store values between
-9223372036854775808
and9223372036854775807
- Can store values between
- Max value for an integer is
While this code isn’t in the textbook, here is an example of an integer overflowing and not causing an exception
/*
Overflow.java
@author Nathan Jankowski
*/
public class Overflow {
public static void main(String[] args) {
int plsdontoverflow = 2147483645; // this will overflow
for (int i = 0; i < 5; i++) {
System.out.println(plsdontoverflow++);
}
}
}
/*
output:
2147483645
2147483646
2147483647
-2147483648
-2147483647
*/
Ignore the stuff about for loops; you’ll learn that later :)
As the integer increments over the max (0xffffffffffffffff
), wraps back around to 0x0
, which in this case is a negative number.
- ints in Java are “signed”, which is to say the first half of bytes in a number are used to represent the negative range
- a
0
is halfway between0x0
and0xffffffffffffffff
- a
Programming Challenge - Average 3 Numbers: 1.6.1
No real notes here, just make sure you don’t accidentally do integer division!
Using arbitrary numbers
While I’m not usually a fan of just straight up posting solutions here, I’m putting mine here now because I don’t know where else to put it. I’m relatively confident this won’t be used by someone to cheat, because this course hasn’t covered iteration, arrays, or format strings yet.
/*
AverageGrades.java
@author Nathan Jankowski
Based off of the CS Awesome Challenge1-6 Average Template
https://replit.com/@BerylHoffman/Challenge1-6-Average-Template
*/
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int sum = 0;
double average;
int[] grades;
System.out.print("Enter number of grades to be averaged: ");
grades = new int[scan.nextInt()]; // Create Blank array of arbitrary size
for (int i = 0; i < grades.length; i++) {
System.out.print(String.format("Enter Grade %d: ", i+1));
grades[i] = scan.nextInt(); // Get Grade
}
for (int grade : grades) { // Iterate through array
sum += grade;
}
average = (double) sum / grades.length;
// Print solution, rounded to the second decimal
System.out.println(String.format("Average grade: %.02f", average));
}
}
Casting and Unicode
- Unicode represents all letters and numbers with a number
- This is typically hexidecimal, can be base 10
- Java was one of the first to adopt unicode
- You can cast
int
tochar
!!!!!!!char
is a primitive type to store 1 character(char) 65
casts to'A'
- You can cast
Summary: 1.6.2
- Type casting converts variables between types
- Casting operators convert variables between types
(int)
and(double)
- Casting
double
toint
causes the decimal digits to be truncated
- ints have a minimum and maximum value
- ints must stay within that range
- Using numbers outside of that results in an integer overflow
Closing thoughts
None really again; it was another short lesson. The coding puzzles were fun though :)
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.