Types
Overview
- Data Types
- Object-type
- Variable Initialization / Assignment Statements
- Variable Declaration
- Variable Instantation
- Casting
Data Types
- In Object Oriented Programming (OOP) languages, “Everything is an Object”.
- The simplest program one could write is named the “Hello, World!” program.
- This program states three essential things:
- There will be a classification of an Object named
ApplicationRunner
. - The
ApplicationRunner
has one static behavior namedmain
. - The behavior of the
main
method is to print, “Hello, World!” to the console.
- There will be a classification of an Object named
public class ApplicationRunner {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Object Type
- In Object Oriented Programming, an object is any value in memory referenced by an identifier.
- An object can be a
- variable
- data structure
- function
- method
- Because “everything is an object”, all variables can be assigned to the
Object
type. - A type is the name of the class of an object.
- A class is a template, or blueprint from which objects are created
- it is the cookie-cutter to a cookie
- it is the classification of an object.
- An object is an instance of a class.
- All objects have the following three traits:
- objects have a state
- objects have behaviors
- objects have an identity
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);
}
}
- Output
My age is 27
Variable Initialization
- Any expression which states a variable type, a variable name, an equal operator (
=
), followed by a variable value, is considered an assignment statement. - All assignment statements result in variable initialization or reinitialization.
- General Syntax:
VariableType variableName = ${some-value-here};
- Example 1:
Object age = 27;
- Example 2:
Object name = "Leon Hunter";
- Example 3:
Object accountBalance = 999999.99;
- General Syntax:
Variable Declaration
- Any expression which states a variable type and a variable name, but omits the equal operator (
=
) followed by a variable value, is considered a variable declaration.- General Syntax:
VariableType variableName;
- Example 1:
Object age;
- Example 2:
Object name;
- Example 3:
Object accountBalance;
- General Syntax:
Variable Instantiation
- Any expression which states a variable type, a variable name, an equal operator (
=
), followed by the keywordnew
, is considered a variable instantiation.- General Syntax:
VariableType variableName = new VariableType();
- Example 1:
Object age = new Integer(27);
- Example 2:
Object name = new String("Leon Hunter");
- Example 3:
Object accountBalance = new Double(99999.9);
- General Syntax:
Why is Type relevant?
- The declared type of a variable determines which behaviors it will be able to perform.
- In OOP, methods denote behavior.
- Methods are functions defined within the scope of a class.
- In pure OOP languages, all functionality is achieved through methods, not functions.
Notable Object Behaviors
- The two most notable object behaviors are
.toString()
and.equals()
.toString()
- An
Object
can convert to string
public class ApplicationRunner {
public static void main(String[] args) {
Object ageAsInteger = 27;
Object ageAsString = age.toString();
}
}
.equals()
- An Object can compare its equivalence
public class ApplicationRunner {
public static void main(String[] args) {
Object ageAsInteger = 27;
Object ageAsString = age.toString();
Object areObjectsEqual = ageAsInteger.equals(ageAsString);
}
}
What is Casting?
- Because the declared type of a variable determines which behaviors it will be able to perform, assigning variables to
Object
gives access to very few behaviors. - An
Object
can be treated as a more-specific type through a mechanism called down-casting.- Downcasting gives access to more behaviors.
- An
Object
can be treated as a less-specific type through a mechanism called up-casting.- Upcasting restricts access to more-specific behaviors.
How to Cast
-
To cast a Type to another Type, wrap the desired type in parenthesis, and associate it with the value.
-
General Syntax:
VariableType variableName = new VariableType();
DesiredType newVariableName = (DesiredType)variableName;
- Example 1:
public class ApplicationRunner {
public static void main(String[] args) {
Object ageAsObject = 27;
Integer ageAsInteger = (Integer)ageAsObject;
}
}
- Example 2:
public class ApplicationRunner {
public static void main(String[] args) {
Object isMaleObject = true;
Boolean isMaleBoolean = (Boolean)ageAsObject;
}
}
- Example 2:
public class ApplicationRunner {
public static void main(String[] args) {
Object nameAsObject = "Leon Hunter";
String nameAsString = (String)nameAsObject;
}
}
When to Cast
- Avoid casting at all times.
-
Generally, variables should be declared as their desired type upon initialization
- Example 1:
public class ApplicationRunner {
public static void main(String[] args) {
Integer ageAsInteger = 27;
}
}
- Example 2:
public class ApplicationRunner {
public static void main(String[] args) {
Boolean isMaleBoolean = true;
}
}
- Example 2:
public class ApplicationRunner {
public static void main(String[] args) {
String nameAsString = "Leon Hunter";
}
}