Skip to content

Instantly share code, notes, and snippets.

// integers default to integers
System.out.println(42 + 2);
System.out.println(42 - 2);
System.out.println(42 * 2);
System.out.println(42 / 4);
System.out.println(42 % 4);
// doubles default to doubles
System.out.println(42.0 / 4.0);
// Numerous math functions (check the doc for complete list)
// With numeric types, you can automatically cast from smaller types
int tenInt = 10;
long tenLong = tenInt;
// Otherwise use (TYPE) newValue
double someDouble = 1.234;
int someInt = (int) someDouble;
System.out.println(someInt); // 1
// Most numeric types have methods to convert to string
// boolean data type represents one bit of information: true or false
boolean javaIsFun = false;
// min / max value
System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
// float data type is a single-precision 32-bit IEEE 754 floating point
float piFloat = 3.14f;
// min / max value
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
// int is a 32-bit signed two's complement integer from -2,147,483,648 (-2^31) to 2,147,483,647(inclusive) (2^31 -1)
int oneMillion = 1000000;
// min / max value
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
// Long is a 64-bit signed two's complement integer from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
// mainly used when the range of int is not enough
long largeNo = 9223372036854775807L;
long withSeparator = 9_223_372_036_854_775_807L;
// min / max value
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
// double is a double-precision 64-bit IEEE 754 floating point
double pi = 3.14;
// min / max value
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
// short is a 16-bit signed integer from -32,768 (-2^15) to 32,767 (inclusive) (2^15 -1)
// 2 times smaller than int
short tenThousand = 10000;
// min / max value
System.out.println(Character.MIN_VALUE + 0);
System.out.println(Character.MAX_VALUE + 0);
// char stores a single 16-bit Unicode character from '\u0000' (or 0) to '\uffff' (or 65,535 inclusive)
char A = 'A'; // char's are always wrapped in single quotes
char special = '~';
// min / max value
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
// byte can be used to save space when working with the integers and the range of byte is enough.
// It is 4 times smaller than int
byte fifty = 50;
byte min = -128;
byte max = 127;