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
  • Primitive Types
  • Numeric Type:
  • Char Type:
  • Boolean Type:
  • String Type:
  • Naming Variables

Was this helpful?

  1. Basic Java

Variables and Types

PreviousPrinting in JavaNextUser Input

Last updated 5 years ago

Was this helpful?

Variables allow us to store information such as numbers, words, or true/false expressions. A variable can be thought of as a box that stores information inside. In Java, variables are composed of three things: a name, type, and value.

Primitive Types

In Java we must specify what type of information we want our variables to hold. You must always give your variable a type before naming it, and specifying the value it holds. (Ex. int myVariable = 10;)

Here are some of the primitive types found in Java:

Numeric Type:

As seen above, primitive numeric types in Java include both integers and doubles.

Integers are whole numbers, or counting numbers. (Ex. -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)

In Java we declare an integer using int before the variable name. Here are a couple of examples:

int itemsInStore = 10; 

int numberOfMessages = 8;

doubles are like integers, but can have decimals. (Ex. -54.34, 90.21, 0.1223)

In Java we declare a double using double before the variable name. Here are a couple of examples:

double costOfApple = 1.24;

double milesToRun = 5.64;

Char Type:

Characters represent a single character.

In Java we declare a character using char before the variable name. We also use single quotes to identify a character (Ex. 'A'). Here are a couple of examples:

char currentGrade = 'A';

char favoriteLetter = 'W';

Boolean Type:

Booleans are variables that hold a true or a false value.

In Java we declare a boolean using boolean before the variable name. Here are a couple of examples:

boolean passedCalculus = true;

boolean hasDog = false;

String Type:

Strings are variables that hold text. Strings are not a primitive type, so you must declare them using String with a capital S. Unlike characters, we need to use double quotes when assigning strings (Ex. "This is my string."). Here are a couple of examples:

String fishName = "Dog";

String myUniversity = "Arizona State University";

Naming Variables

Giving your variables meaningful names throughout your code is very important. Proper variable names allow others to easily read and understand your code. A good way to name your variables is to give them as descriptive of a name as possible, without making it too long. For example, int numberOfApplesOnTheTree = 10; is a very long name, and can easily be replaced with a name like int numApples = 10;.

Variable Naming Conventions:

  • Variable names must start with a letter, $ symbol, or _ symbol.

  • Variable names are case sensitive so myVariable is different than MyVariable

  • Variable names, after the first character, can contain letters, numbers, or other characters.

Here are some examples of different variable names:

Primitive Types Table
Variable Names Table