AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • What is a Primitive?
  • Primitives and Objects
  • Strings are not Primitives
  • Making Comparisons

Was this helpful?

  1. Classes and Object-Oriented Programming

Objects vs Primitives

Not surprisingly, objects play a key role in object-oriented programming. But objects are not the only way that you will interact with data in the Java programming languages. Up to this point, you have built programs that include both objects and primitive data types. Understanding the differences between objects and primitives will help you plan and write programs.

What is a Primitive?

Primitive data types are the basic building blocks of the Java programming language. These basic types cannot be broken down into other parts. Java includes several primitive types that you have seen before:

Type

Example

int

int x = 42;

char

char oneChar = 'a';

double

double myNumber = 13.37;

boolean

boolean likesCoding = true;

These primitive types can be combined and used to build more complex objects. Primitive types are named with all lowercase letters.

Primitives and Objects

Primitive data types are used to make objects. For example, a Movie object may have a title, director, whether it is a sequel, number of tickets sold, and runtime of the movie in minutes:

public class Movie
{
    private String title;
    private String director;
    private boolean isSequel;
    private int numTicketsSold;
    private double runtime;
}

Strings are not Primitives

Note that Strings are not a primitive data type. Strings are sequences of characters. As such, when creating a new string of text, be sure to capitalize the S in String: private String nyName

Making Comparisons

Comparing primitive data types like integers is straightforward using ==. For example, to check if a user-entered number is 0:

int userNumber = readInt("Enter a number: ");
System.out.println(userNumber == 0);

The values of objects, however, cannot be compared with the ==. Instead, objects are compared with a .equals method. For example:

String textOne = new String("Hello world!");
String textTwo = new String("Hello world!");
System.out.println(textOne.equals(textTwo));
PreviousKey Terms for ClassesNextInheritance

Last updated 5 years ago

Was this helpful?