Unit 1 - Primitive and Reference Types Examples
Primitive Data Types
In your personal blog you should have a comprehensive example that help you recall Primitive Data Types.
public class PrimitiveTypesDemo {
public static void main(String[] args) {
// Declare and initialize variables of different primitive types
byte myByte = 10; // 8-bit integer
short myShort = 32000; // 16-bit integer
int myInt = 123456; // 32-bit integer
long myLong = 123456789L; // 64-bit integer
float myFloat = 3.14f; // 32-bit floating-point
double myDouble = 3.14159; // 64-bit floating-point
char myChar = 'A'; // 16-bit Unicode character
boolean myBoolean = true; // true or false
// Perform some operations
int sum = myInt + 1000;
float division = myFloat / 2;
boolean isEqual = myByte == 10;
// Print the results
System.out.println("Byte value: " + myByte);
System.out.println("Short value: " + myShort);
System.out.println("Int value: " + myInt);
System.out.println("Long value: " + myLong);
System.out.println("Float value: " + myFloat);
System.out.println("Double value: " + myDouble);
System.out.println("Char value: " + myChar);
System.out.println("Boolean value: " + myBoolean);
System.out.println("Sum of myInt and 1000: " + sum);
System.out.println("Division of myFloat by 2: " + division);
System.out.println("Is myByte equal to 10? " + isEqual);
}
}
PrimitiveTypesDemo.main(null);
Byte value: 10
Short value: 32000
Int value: 123456
Long value: 123456789
Float value: 3.14
Double value: 3.14159
Char value: A
Boolean value: true
Sum of myInt and 1000: 124456
Division of myFloat by 2: 1.57
Is myByte equal to 10? true
Refrence Types Examples
In your Personal Blog you should create a class that captures key reference type in Java. In fact, there is at least one common reference type we will be testing on in AP CSA that is not represented below.
The class Person illustrates the basics of a Java class. When a Person person object is created, it is stored on the heap, and it contains data for the name and age. This object is a reference type, meaning that the variable person
holds a reference (or address) to the memory location where the actual Person
object is stored, rather than the object itself.
Key Points:
- Instance Variables: Attributes of the class that hold data.
- Constructor: Initializes the instance variables.
- Methods: Define behaviors for the class.
- Reference Type: The variable holds a reference to the memory location of the object on the heap.
public class Person {
// Fields (attributes) of the Person class
String name;
int age;
// Constructor to initialize the Person object
Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method to introduce the person
void introduce() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
// Method to simulate having a birthday
void haveBirthday() {
age++;
System.out.println("Happy Birthday! I am now " + age + " years old.");
}
public static void main(String[] args) {
// Create an instance of the Person class (a custom reference type)
Person person = new Person("John", 25);
// Call methods on the Person object
person.introduce();
person.haveBirthday();
}
}
Person.main(null);
The class ArrayReferenceTypes demonstrates the usage of arrays, which are reference types in Java. This class includes examples of a single-dimensional array of integers (int[]
) and a two-dimensional array of strings (String[][]
). Both arrays are initialized with hard-coded literals.
Key Points:
- Single-Dimensional Array: Demonstrates the use of int[] with hard-coded literals.
- Two-Dimensional Array: Demonstrates the use of String[][] with hard-coded literals.
- Enhanced For-Loop: Used for iterating over the single-dimensional array.
- Classic For-Loop: Used for iterating over the two-dimensional array with i and j indices.
- Reference Type: Arrays are reference types, meaning the variable holds a reference to the memory location of the array data.
public class ReferenceTypesDemo {
public static void main(String[] args) {
// Create an instance of the Person class (a custom reference type)
Person person = new Person("John", 25);
// Call methods on the Person object
person.introduce();
person.haveBirthday();
// Create and initialize an array of integers
int[] numbers = {1, 2, 3, 4, 5};
// Print the contents of the integer array
System.out.println("Array contents:");
for (int number : numbers) {
System.out.println(number);
}
// Create and initialize a 2D array of strings
String[][] matrix = {
{"A1", "B1", "C1"},
{"A2", "B2", "C2"},
{"A3", "B3", "C3"}
};
// Print the contents of the 2D string array
System.out.println("2D array contents:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
ReferenceTypesDemo.main(null);
Hello, my name is John and I am 25 years old.
Happy Birthday! I am now 26 years old.
Array contents:
1
2
3
4
5
2D array contents:
A1 B1 C1
A2 B2 C2
A3 B3 C3
Overflow and Underflow
In Java, by nature of using strongly typed definitions, the developer needs to be aware of the limits of numeric data. The class OverFlow demonstrates the constraints of int and double addition, and introduces the terms overflow and underflow.
import java.text.DecimalFormat;
public class OverFlow {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#,###");
DecimalFormat dfDouble = new DecimalFormat("#,###.################");
System.out.println("Max Integer: " + Integer.MAX_VALUE);
System.out.println("Min Integer: " + Integer.MIN_VALUE);
System.out.println("Max Double: " + dfDouble.format(Double.MAX_VALUE));
System.out.println("Min Double: " + Double.MIN_VALUE);
// Integer Show Overflow
int i = Integer.MAX_VALUE;
i++;
System.out.println("Overflow error (flips negative), Integer Max + 1: " + df.format(i));
// Integer Show Underflow
int j = Integer.MIN_VALUE;
j--;
System.out.println("Underflow error (flips positive), Integer Min - 1: " + df.format(j));
// Double Show Double Handling Integer.MAX_Value + 1
double d = Integer.MAX_VALUE + 1.0;
System.out.println("Double Handling Integer Max + 1: " + dfDouble.format(d));
// Double Show Double Handling Integer.MIN_Value - 1
double e = Integer.MIN_VALUE - 1.0;
System.out.println("Double Handling Integer Min - 1: " + dfDouble.format(e));
// Integer Max + Min
int k = Integer.MAX_VALUE + Integer.MIN_VALUE;
System.out.println("Integer Max + Min, shows Min is greater: " + df.format(k));
}
}
OverFlow.main(null);
Max Integer: 2147483647
Min Integer: -2147483648
Max Double: 179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
Min Double: 4.9E-324
Overflow error (flips negative), Integer Max + 1: -2,147,483,648
Underflow error (flips positive), Integer Min - 1: 2,147,483,647
Double Handling Integer Max + 1: 2,147,483,648
Double Handling Integer Min - 1: -2,147,483,649
Integer Max + Min, shows Min is greater: -1
Casting
In some numeric operations, it may be more logical to change the type.
- Widening: This is the process of converting a smaller data type to a larger data type. For example, in division, where two integers (int) produce a real number (double).
- Narrowing: This is the process of converting a larger data type to a smaller data type, such as truncating a double to an int.
public class Casting {
public static void main(String[] args) {
// Implicit casting (widening)
int num1 = 5;
int num2 = 2;
System.out.println("Integer math: " + num1 / num2);
// to widen one or the other must be a double
System.out.println("Cast math to Double: " + (double) num1 / num2);
// Explicit casting (narrowing)
double rounding = 0.1;
double test1 = 90.0;
double test2 = 89.5;
double test3 = 90.4;
double average = (test1 + test2 + test3) / 3;
// to narrow all must be casted to int
double averageTruncated = (int) average;
// to simplify narrow, cast after all of the math operations
double averageRounded = (int) (average + rounding);
System.out.println("Double average: " + average);
System.out.println("Cast average to int, result is truncated: " + averageTruncated);
System.out.println("Cast average to int, result is truncated after rounding adjust: " + averageRounded);
}
}
Casting.main(null);
Integer math: 2
Cast math to Double: 2.5
Double average: 89.96666666666665
Cast average to int, result is truncated: 89.0
Cast average to int, result is truncated after rounding adjust: 90.0
ArrayLists and Wrapper Classes
In the context of College Board, this Teacher believes that ArrayLists and Wrapper classes go together in learning and discussion.
A key use of Wrapper Classes is with ArrayList
, which only accepts reference types. This allows primitives to be used within ArrayList
through their Wrapper Classes, enabling dynamic resizing and access to built-in methods like streams for sum and average calculations.
Additionally, Wrapper Classes in Java allow primitive types to behave like reference types, providing useful methods and constants such as MIN_VALUE
and MAX_VALUE
. They enable transformations between primitives and strings, like converting an int
to a String
and vice versa.
In the following examples, the first example goes through the language elements of Wrapper Classes
culminating with ArrayList
. The second example puts these elements together in a more practical application, demonstrating the value of using Wrapper Classes
and ArrayList
to simplify code and perform calculations.
import java.util.ArrayList;
import java.util.Arrays;
public class StudentGradeCalculator {
int age; // Primitive data type, int
ArrayList<Integer> testScoresList; // ArrayList of Wrapper class for Integer
// Constructor to initialize age and test scores
public StudentGradeCalculator(int age, String testScoresString) {
this.age = age;
this.testScoresList = new ArrayList<>();
// Split input string and convert to integer values
String[] testScoresArray = testScoresString.split(", ");
// Add integer values to ArrayList
for (String score : testScoresArray) {
// Add integer values to ArrayList, if not an integer, set to minimum of 55 percent
this.testScoresList.add(score.matches("\\d+") ? Integer.parseInt(score) : 55);
}
}
// Method to calculate average score using College Board method
public double calculateAverage() {
int total = 0;
for (int score : testScoresList) {
total += score;
}
return (double) total / testScoresList.size();
}
// Method to calculate average score using Java 8 Stream API
public double calculateAverageStream() {
return testScoresList.stream().mapToInt(Integer::intValue).average().orElse(0.0);
}
// Override toString method to display student details
@Override
public String toString() {
return "Age: " + age + ", Test Scores: " + testScoresList + ", Average Score: " + calculateAverage() + ", Average Score (Stream): " + calculateAverageStream();
}
public static void main(String[] args) {
// Initialize students with ages and test scores
StudentGradeCalculator john = new StudentGradeCalculator(17, "85, A, 78, 92");
StudentGradeCalculator anita = new StudentGradeCalculator(16, "88, 86, 90, 85");
StudentGradeCalculator jamal = new StudentGradeCalculator(18, "92, 81, 79, 95");
// Output student details
System.out.println("John's Details: " + john);
System.out.println("Anita's Details: " + anita);
System.out.println("Jamal's Details: " + jamal);
}
}
StudentGradeCalculator.main(null);
John's Details: Age: 17, Test Scores: [85, 55, 78, 92], Average Score: 77.5, Average Score (Stream): 77.5
Anita's Details: Age: 16, Test Scores: [88, 86, 90, 85], Average Score: 87.25, Average Score (Stream): 87.25
Jamal's Details: Age: 18, Test Scores: [92, 81, 79, 95], Average Score: 86.75, Average Score (Stream): 86.75
Enums (Project-based Learning Topic)
Enums in Java, similar to those in the ‘C’ language, are a special reference data type that enables a variable to be a set of predefined constants. They are useful for representing fixed sets of related constants, such as days of the week, months of the year, etc.
Enums can be used in assignments, conditional checks (if, switch), and iterations (for, while), making them versatile for various programming scenarios.
Similarity to Classes:
- Fields and Methods: Enums can have fields and methods just like classes.
- Constructors: Enums can have constructor-like methods to initialize their fields, similar to class constructors.
- Encapsulation: Enums encapsulate data (fields) and behavior (methods) just like classes.
In this example, we are setting constants and descriptions to represent educational units and questions for the APCSA curriculum.
public class APCSACurriculum {
// Enum to represent educational units
enum Units {
// Each enum constant is initialized with a description and is associated with a getter method
UNIT_1("Primitive Types—You’ll learn the fundamentals of Java, a programming language, as well as other foundational concepts for coding."),
UNIT_2("Using Objects—You’ll explore reference data as a way to represent real-world objects in a digital world and discover methods to perform more complex operations."),
UNIT_3("Boolean Expressions and if Statements—You’ll delve into the building blocks of algorithms and focus on using conditional statements to solve problems and control results."),
UNIT_4("Iteration—You’ll learn about iteration, another building block of algorithms that are for repetition."),
UNIT_5("Writing Classes—You’ll explore how real-world interactions can be expressed digitally by organizing behaviors and attributes into classes, and you’ll examine the legal and ethical implications of computer programming."),
UNIT_6("Array—You’ll learn techniques and standard algorithms to work with collections of related data, known as data structures."),
UNIT_7("ArrayList—You’ll delve deeper into data sets, exploring ArrayList objects for larger amounts of data, as well as the privacy concerns related to personal data storage."),
UNIT_8("2D Array—Now that you’ve explored 1D arrays, you’ll branch out into 2D arrays and experiment with data sets represented in a table."),
UNIT_9("Inheritance—You’ll learn how to manipulate programming without altering existing code by using subclasses to create a hierarchy."),
UNIT_10("Recursion—You’ll work on solving larger problems by solving smaller, simpler versions of the same problem using recursive methods.");
// Private field to hold the description of each unit
private final String description;
// Constructor-like method to initialize the description field
Units(String description) {
this.description = description;
}
// Getter method to retrieve the description
public String getDescription() {
return description;
}
}
// Enum to represent Free Response Questions (FRQs)
enum FRQs {
// Each enum constant is initialized with a description and is associated with a getter method
QUESTION_1("Methods and Control Structures—You’ll be asked to write program code to create objects of a class and call methods, and satisfy method specifications using expressions, conditional statements, and iterative statements."),
QUESTION_2("Classes—You’ll be asked to write program code to define a new type by creating a class and satisfy method specifications using expressions, conditional statements, and iterative statements."),
QUESTION_3("Array/ArrayList—You’ll be asked to write program code to satisfy method specifications and create, traverse, and manipulate elements in 1D array or ArrayList objects."),
QUESTION_4("2D Array—You’ll be asked to write program code to satisfy method specifications and create, traverse, and manipulate elements in 2D array objects.");
// Private field to hold the description of each question
private final String description;
// Constructor-like method to initialize the description field
FRQs(String description) {
this.description = description;
}
// Getter method to retrieve the description
public String getDescription() {
return description;
}
}
public static void main(String[] args) {
System.out.println("AP Computer Science A Curriculum and FRQ Topics:");
// Accessing enum values for Units
System.out.println("Units:");
for (Units unit : Units.values()) {
System.out.println("\t" + unit + ": " + unit.getDescription());
}
System.out.println("FRQ types:");
// Accessing enum values for FRQs
for (FRQs frq : FRQs.values()) {
System.out.println("\t" + frq + ": " + frq.getDescription());
}
}
}
APCSACurriculum.main(null);
AP Computer Science A Curriculum and FRQ Topics:
Units:
UNIT_1: Primitive Types—You’ll learn the fundamentals of Java, a programming language, as well as other foundational concepts for coding.
UNIT_2: Using Objects—You’ll explore reference data as a way to represent real-world objects in a digital world and discover methods to perform more complex operations.
UNIT_3: Boolean Expressions and if Statements—You’ll delve into the building blocks of algorithms and focus on using conditional statements to solve problems and control results.
UNIT_4: Iteration—You’ll learn about iteration, another building block of algorithms that are for repetition.
UNIT_5: Writing Classes—You’ll explore how real-world interactions can be expressed digitally by organizing behaviors and attributes into classes, and you’ll examine the legal and ethical implications of computer programming.
UNIT_6: Array—You’ll learn techniques and standard algorithms to work with collections of related data, known as data structures.
UNIT_7: ArrayList—You’ll delve deeper into data sets, exploring ArrayList objects for larger amounts of data, as well as the privacy concerns related to personal data storage.
UNIT_8: 2D Array—Now that you’ve explored 1D arrays, you’ll branch out into 2D arrays and experiment with data sets represented in a table.
UNIT_9: Inheritance—You’ll learn how to manipulate programming without altering existing code by using subclasses to create a hierarchy.
UNIT_10: Recursion—You’ll work on solving larger problems by solving smaller, simpler versions of the same problem using recursive methods.
FRQ types:
QUESTION_1: Methods and Control Structures—You’ll be asked to write program code to create objects of a class and call methods, and satisfy method specifications using expressions, conditional statements, and iterative statements.
QUESTION_2: Classes—You’ll be asked to write program code to define a new type by creating a class and satisfy method specifications using expressions, conditional statements, and iterative statements.
QUESTION_3: Array/ArrayList—You’ll be asked to write program code to satisfy method specifications and create, traverse, and manipulate elements in 1D array or ArrayList objects.
QUESTION_4: 2D Array—You’ll be asked to write program code to satisfy method specifications and create, traverse, and manipulate elements in 2D array objects.