booleanbytecharshortintlongdouble
- Close last project
- Create New from Template of Command line app
- A packages is a way to organize Java projects.
- Consider them folders for now
- Companies use their domain names reversed
Now we have this code
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
}Notice
- Package name at the top
- Main method already written
- Look at the side bar and unfold the src file
package com.company;
public class Main {
public static void main(String[] args) {
int myValue = 10000;
}
}- Lets see it in code!
package com.company;
public class Main {
public static void main(String[] args) {
int myValue = 10000;
int myMinValue = Integer.MIN_VALUE;
int myMaxValue = Integer.MAX_VALUE;
System.out.println("Integer Minimum value = " + myMinValue);
System.out.println("Integer Maximum value = " + myMaxValue);
}
}Output
Integer Minimum value = -2147483648
Integer Maximum value = 2147483647
Process finished with exit code 0
- Java has wrapper classes for all eight primitive types
- This gives us ways to perform ops on an int
- int has Integer
- Min is -2147483648
- Max is 2147483647
- Code
package com.company;
public class Main {
public static void main(String[] args) {
int myValue = 10000;
int myMinValue = Integer.MIN_VALUE;
int myMaxValue = Integer.MAX_VALUE;
System.out.println("Integer Minimum value = " + myMinValue);
System.out.println("Integer Maximum value = " + myMaxValue);
System.out.println("Busted MAX value = " + (myMaxValue + 1));
}
}Ouput
Integer Minimum value = -2147483648
Integer Maximum value = 2147483647
Busted MAX value = -2147483648
Process finished with exit code 0
Notice the max went all the way back around to the min int. This is an integer overflow.
Let's do the reverse
package com.company;
public class Main {
public static void main(String[] args) {
int myValue = 10000;
int myMinValue = Integer.MIN_VALUE;
int myMaxValue = Integer.MAX_VALUE;
System.out.println("Integer Minimum value = " + myMinValue);
System.out.println("Integer Maximum value = " + myMaxValue);
System.out.println("Busted MAX value = " + (myMaxValue + 1));
System.out.println("Busted MIN value = " + (myMinValue - 1));
}
}Output
Integer Minimum value = -2147483648
Integer Maximum value = 2147483647
Busted MAX value = -2147483648
Busted MIN value = 2147483647
- This triggers an underflow
- IJ will flag a literal number over the max int
int myMaxIntTest = 2147483648;will be red in the IDE- Running it will give error
java: integer number too large
- Running it will give error
Note you can use int myMaxIntTest = 2_147_483_647; in java for comma seperation if you want.