Primitives

Vocab:

  • String literals - String created using double quoted String
  • Literals - String created by calling the constructor
  • Exceptions - errors thrown by the program
  • Syntax error - error created by bad code syntax
  • Logic error - error created by impossibilities during runtime (i.e. 0/0)
  • Overflow error - error created by amounts of data too large for certain dataypes to hold (i.e. floats with too many decimals)
  • Operation precedence - some operators are prioritized (use PEMDAS)

Primitives are datatypes used in Java as some of its most basic features.

Compared with non-primitives:

  • Predefined vs. defined by the programmer
  • Lowercase vs. uppercase
  • Can't call methods vs. can call methods
  • Must have a value vs. can be null
  • Different sizes (depending on primitive type) vs. same size

Some of the most basic primitives include:

  • Booleans
  • Ints
  • Doubles
  • Chars
  • Etc.

They have particular naming conventions (lowercase, no special characters/keywords) and can also be type casted (manually or automatically)

They can use numerous operators to manipulate these types in differing ways depending on the primitive:

  • +, -, *, /, %, etc.
  • ++ and -- are special incrementing operators, which is equal to x = x + 1 and x = x - 1
public class Primitives {

    public static void main(String[] args) {

        boolean bool = true;
        int num1 = 42;
        double num2 = 2.718;

        System.out.println(num1);
        num1 = num1 + 36;
        System.out.println(num1);

        System.out.println(num2);
        num2 = num2 % 1.6;
        System.out.println(num2);
    }
}

Primitives.main(null);
42
78
2.718
1.1179999999999999

Homework

import java.util.Scanner;

public class GradeCalculator {

    public static void separate() {
        Scanner input = new Scanner(System.in);
        System.out.println("What is your current grade?");
        double grade = input.nextDouble();
        System.out.println("How much percent of your grade is the final?");
        double percent = 
    }

    public static void test() {
        Scanner input = new Scanner(System.in);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Is the final in the tests category?");
        boolean isTest = input.nextBoolean();
        input.close();
        if (isTest) {
            test();
        } else {
            separate();
        }
    }

}

GradeCalculator.main(null);
Is the final in the tests category?

Extra Notes

Casting

Casting is a method of forcefully changing the data type of data, though it won't work in every instance. For instance, a String (obviously) cannot be converted into an int, but an int can be converted into a String.

There are many uses for casting. When dividing with integers, results are automatically truncuated into whole numbers, which can be annoying for many reasons. However, when the integers are casted into doubles, then the result will be a double with the correct accuracy. On the other hand, sometimes truncuated integer results are required from doubles, and the solution is to cast the result into an int.

// Division
int num1 = 4;
int num2 = 3;

System.out.println((double)(num1) / (double)(num2));

// Truncuation/Rounding
double num3 = 5.0;
double num4 = 2.0;

System.out.println((int)(num3 / num4));
1.3333333333333333
2

Wrapper Classes

When it's more useful to turn a primitive datatype into an object (like for parsing and stuff), wrapper classes are used for each of the primitives:

  • Integer for int
  • Long for long
  • Float for float
  • Double for double
  • Boolean for boolean
  • Character for char

Some instances where wrapper classes are required include the use of ArrayLists, which can only store objects. Therefore, all primitive datatypes stored must be converted into their wrapper class equivalents:

ArrayList<Double> list = new ArrayList<Double>();
ArrayList<Character> list2 = new ArrayList<Character>();