Notes on Primitive Data Types

There are two main types of data structures, primitive and non-primitive. Strings, arrays, etc. are examples of non-primitive data types.

Within primitive data types, we have boolean, characters (chars), bytes, shorts, ints, longs, floats, and doubles. For non-primitive data types, it is anything the is, well, non-primitive. For example, we have arrays and strings.

// java style to import library
import java.util.Scanner;

public class TestPrimitives {
    public static void main(String[] args) {    
        Scanner input;

        // Integers
        input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        try {
            int sampleInputInt = input.nextInt();
            System.out.println(sampleInputInt);
        } catch (Exception e) {  // if not an integer
            System.out.println("Not an integer, " + e);
        }
        input.close();

        // Doubles
        input = new Scanner(System.in);
        System.out.print("Enter a double: ");
        try {
            double sampleInputDouble = input.nextDouble();
            System.out.println(sampleInputDouble);
        } catch (Exception e) {  // if not a number
            System.out.println("Not an double, " + e);
        }
        input.close();

        // Booleans
        input =  new Scanner(System.in);
        System.out.print("Enter a boolean: ");
        try {
            boolean sampleInputBoolean = input.nextBoolean();
            System.out.println(sampleInputBoolean);
        } catch (Exception e) {  // if not true or false
            System.out.println("Not an boolean, " + e);
        }
        input.close();

        // Wrapper Class Strings
        input =  new Scanner(System.in);
        System.out.print("Enter a String: ");
        try {
            String sampleInputString = input.nextLine();
            System.out.println(sampleInputString);
        } catch (Exception e) { // this may never happen
            System.out.println("Not an String, " + e);
        }
        input.close();
    }
}
TestPrimitives.main(null);
Enter an integer: 3
Enter a double: 3.3
Enter a boolean: true
Enter a String: AP CSA is awesome