public class DatatypesDemo { public static void main(String[] args) { // ********************** declaration statements for local variables in the main method int num1 = 9; int num2 = 2; double num3 = 0; // int num4; // error, can't declare a local primitive variable without // initializing it to a value such as zero int num5 = 0, num6 = 0; // it is legal to declare & initialize two variables in the same statement // but I consider it bad style // ********************** assignment statement examples num1 = 1000000; // num1 = 4.5; // error, can't directly assign a decimal number to an int variable // num1 = num2 + num3; // error, can't store a decimal value into an int variable even though num3 is zero and num2 + num3 // would work out to a whole number // num1 = 3000000000; // error, can't assign a value greater than Integer.MAX_VALUE to an int // num1 = 1e6; // error, using e for exponential notation causes 1e6 to be treated as a double rather than an int num3 = 1e6; // legal to use exponential notation with num3 since it is a double // int num1 = 10; // error, since num1 was already declared as an int in this method & you can't redeclare a variable twice // *********************** integer division examples ********************** num1 = 9; num2 = 2; num3 = num1 / num2; // example of an assignment statement System.out.println(num3); // 4.0 (not 4.5) displays due to integer division // even though num3 is a double. The value is immediately truncated as soon as values are divided and before the result // is assigned to the variable num3 num1 = 9; num2 = 2; num3 = (double) num1 / num2; // example of casting the int variable num1 to a double System.out.println(num3); // 4.5 displays since num1 is casted to a double to avoid integer division num1 = 9 / 2; System.out.println(num1); // 4 displays due to integer division which occurs first before the value is assigned to num1 num3 = 9. / 2; System.out.println(num3); // 4.5 displays since the decimal point after the 9 causes the computer to treat 9. as // a double rather than an int value // num1 = 8. / 2; // error, can't directly assign a decimal value into an int variable. Note that 8. / 2 is the double 4. and not the integer 4 num2 = 1; num3 = 2.5; num1 = num2 + (int) num3; // example of casting a double to an int to avoid the error that would occur if not casted System.out.println(num1); // 3 since the casting caused the 2.5 to be truncated to 2 num1 = 0; num1 += 2; // using the += compound operator, equivalent to num1 = num1 + 2; System.out.println(num1); // 2 num1++; // using the ++ incrementing operator, equivalent to num1 = num1 + 1; System.out.println(num1); // 3 --num1; // using the -- decrementing operator to subtract one System.out.println(num1); // 2 num1 = Integer.MAX_VALUE; System.out.println(num1); // 2147483647 //num1 = 2147483648; // error, since directly assigning a value that overflows int num1 = Integer.MAX_VALUE + 1; // legal since not directly assigning a value to num1 but ..... System.out.println(num1); // -2147483648 displays since you wrapped around the number line after integer overflow num3 = 1e6; // exponential notation System.out.println(num3); // 1000000.0 since 1 x 10^6 = 1 million num3 = 1e-2; System.out.println(num3); // 0.01 since 1 x 10^-2 = 1/100 = one one-hundredth num3 = 1.0000000000000000002; System.out.println(num3); // 1.0 due to loss of precision w/ double data type, only about 15 overall digits are accurately stored num1 = 123; System.out.println(num1 % 10); // 3 one's digit System.out.println(num1 % 100 / 10); // 2 ten's digit System.out.println(num1 % 1000 / 100); // 1 hundred's digit num3 = 19.99; System.out.println(Math.floor(num3)); // 19.0 displays since the floor method truncates the decimal number // num1 = Math.sqrt(9); // error since num1 is int & can't store sqrt of 9 which is 3.0 not 3 System.out.println(Math.pow(2.5, 2)); // 6.25 which is 2.5 squared System.out.println(Math.abs(-3)); // 3 since 3 is the absolute value of -3 num1 = 2; num2 = 5; num3 = 1; // it is legal to combine 3 assignment statements on 1 line of code, though I consider it bad style System.out.println(Math.max(num1, num2)); // 5 displays since it is the maximum value between num1 (2) and num2 (5) int v = 1, w = 4, x = 5, y = 2, z = 3; System.out.println(Math.max(Math.max(Math.max(Math.max(v, w), x), y), z)); // 5 displays. This awkward nesting is necessary since Math.max only accepts 2 parameters System.out.println(Math.min(Math.min(num1, num2), num3)); // 1.0 System.out.println(Math.ceil(1.1)); // 2.0 displays since ceil moves you to the next integer to the right on the number line System.out.println(Math.ceil(-1.9)); // -1.0 since ceil moves you to the next integer to the right on the number line System.out.println(Math.floor(9.99)); // 9.0 since floor moves you to the next integer to the left on the the number line System.out.println(Math.floor(-1.1)); // -2.0 since floor moves you to the next integer to the left on the the number line System.out.println(Math.random()); // a random value between [0, 1) such as 0.342 String stringNum = "123"; System.out.println(stringNum + 1); // "1231" since Java concatenates when an int and String are "added" System.out.println(Integer.parseInt(stringNum) + 1); // "124" since string value "123" is first converted to numeric value 123. // The Integer.parseInt method is similar to the Val and CInt methods in Visual Basic stringNum = "123.4"; System.out.println(Double.parseDouble(stringNum) + 0.1); // "123.5" } }