Types

Overview

Data Types

public class ApplicationRunner {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Object Type

public class ApplicationRunner {
    public static void main(String[] args) {
        Object age = 27;
        Object ageGreeting = "My age is ";        
        Object ageOutput = ageGreeting + age;

        System.out.println(nameOutput);
    }
}
My age is 27

Variable Initialization

Variable Declaration

Variable Instantiation

Why is Type relevant?

Notable Object Behaviors

.toString()

public class ApplicationRunner {
    public static void main(String[] args) {
        Object ageAsInteger = 27;
        Object ageAsString = age.toString();
    }
}

.equals()

public class ApplicationRunner {
    public static void main(String[] args) {
        Object ageAsInteger = 27;
        Object ageAsString = age.toString();
        Object areObjectsEqual = ageAsInteger.equals(ageAsString);
    }
}

What is Casting?

How to Cast

VariableType variableName = new VariableType();
DesiredType newVariableName = (DesiredType)variableName;
public class ApplicationRunner {
    public static void main(String[] args) {
        Object ageAsObject = 27;
        Integer ageAsInteger = (Integer)ageAsObject;
    }
}
public class ApplicationRunner {
    public static void main(String[] args) {
        Object isMaleObject = true;
        Boolean isMaleBoolean = (Boolean)ageAsObject;
    }
}
public class ApplicationRunner {
    public static void main(String[] args) {
        Object nameAsObject = "Leon Hunter";
        String nameAsString = (String)nameAsObject;
    }
}

When to Cast

public class ApplicationRunner {
    public static void main(String[] args) {
        Integer ageAsInteger = 27;
    }
}
public class ApplicationRunner {
    public static void main(String[] args) {
        Boolean isMaleBoolean = true;
    }
}
public class ApplicationRunner {
    public static void main(String[] args) {
        String nameAsString = "Leon Hunter";
    }
}