Key Concepts

Review core concepts you need to learn to master this subject

boolean Data Type

boolean result = true; boolean isMarried = false;

In Java, the boolean primitive data type is used to store a value, which can be either true or false.


boolean result = true; boolean isMarried = false;

A String in Java is a Object that holds multiple characters. It is not a primitive datatype.

A String can be created by placing characters between a pair of double quotes (").

To compare Strings, the equals() method must be used instead of the primitive equality comparator ==.

int Data Type

boolean result = true; boolean isMarried = false;

In Java, the int datatype is used to store integer values. This means that it can store all positive and negative whole numbers and zero.

char Data Type

boolean result = true; boolean isMarried = false;

In Java, char is used to store a single character. The character must be enclosed in single quotes.

Primitive Data Types

boolean result = true; boolean isMarried = false;

Java’s most basic data types are known as primitive data types and are in the system by default.

The available types are as follows:

  • int
  • char
  • boolean
  • byte
  • long
  • short
  • double
  • float

null is another, but it can only ever store the value null.

Static Typing

boolean result = true; boolean isMarried = false;

In Java, the type of a variable is checked at compile time. This is known as static typing. It has the advantage of catching the errors at compile time rather than at execution time.

Variables must be declared with the appropriate data type or the program will not compile.

Math Operations

boolean result = true; boolean isMarried = false;

Basic math operations can be applied to int, double and float data types:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo (yields the remainder)

These operations are not supported for other data types.

Comparison Operators

boolean result = true; boolean isMarried = false;

Comparison operators can be used to compare two values:

  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
  • == equal to
  • != not equal to

They are supported for primitive data types and the result of a comparison is a boolean value true or false.

Arrow Chevron Left Icon
Learn Java: Variables
Lesson 1 of 2
Arrow Chevron Right Icon
  1. 1
    Let’s say we need a program that connects a user with new jobs. We need the user’s name, their salary, and their employment status. All of these pieces of information are stored in our program. W…
  2. 2
    The first type of data we will store is the whole number. Whole numbers are very common in programming. You often see them used to store ages, or maximum sizes, or the number of times some code has…
  3. 3
    Whole numbers don’t accomplish what we need for every program. What if we wanted to store the price of something? We need a decimal point. What if we wanted to store the world’s population? That nu…
  4. 4
    Often our programs face questions that can only be answered with yes or no. Is the oven on? Is the light green? Did I eat breakfast? These questions are answered with a boolean, a type that ref…
  5. 5
    How do we answer questions like: What grade did you get on the test? What letter does your name start with? The char data type can hold any character, like a letter, space, or punctuation mark. …
  6. 6
    So far, we have learned primitive data types, which are the simplest types of data with no built-in behavior. Our programs will also use Strings, which are objects, instead of primitives. Objects…
  7. 7
    The Java programming language has static typing. Java programs will not compile if a variable is assigned a value of an incorrect type. This is a bug, specifically a type declaration bug. Bug…
  8. 8
    Let’s imagine we’re storing a user’s name for their profile. Which code example do you think is better? String data = “Delilah”; or String nameOfUser = “Delilah”; While both of these will comp…
  9. 9
    Creating and filling variables is a powerful concept that allows us to keep track of all kinds of data in our program. In this lesson, we learned how to create and print several different data ty…
  1. 1
    Let’s say we are writing a program that represents a user’s bank account. With variables, we know how to store a balance! We’d use a double, the primitive type that can hold big decimal numbers. …
  2. 2
    In our bank account example from the last exercise, we used + to add! double balance = 20000.99; double depositAmount = 1000.0; double updatedBalance = balance + depositAmount; //updatedBalance no…
  3. 3
    Let’s say that our employer is calculating our paycheck and depositing it to our bank account. We worked 40 hours last week, at a rate of 15.50 an hour. Java can calculate this with the multiplicat…
  4. 4
    If we baked 10 cookies and gave them out in batches of 3, how many would we have leftover after giving out all the full batches we could? The modulo operator %, gives us the remainder after tw…
  5. 5
    Now, we’re withdrawing money from our bank account program, and we want to see if we’re withdrawing less money than what we have available. Java has relational operators for numeric datatypes th…
  6. 6
    So how would we validate our paycheck to see if we got paid the right amount? We can use another relational operator to do this. == will tell us if two variables are equal: double paycheckAmount …
  7. 7
    How could we make sure we got paid at least the amount we expected in our paycheck? We could use greater than or equal to, >=, or less than or equal to, = calculatedPaycheck); //this will print tr…
  8. 8
    So far, we’ve only been using operations on primitive types. It doesn’t make much sense to multiply Strings, or see if one String is less than the other. But what if we had two users logging into a…
  9. 9
    We have covered a lot of built-in functionality in Java throughout this lesson. We’ve seen +, -, With the value of the variable username displayed. The + operator, which we used for adding num…
  10. 10
    What’s the use of having variables if you can’t do anything with them? We’ve now seen some ways you can operate on variables and compare them. The possibilities are endless! We covered: - Addition…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo