Key Concepts

Review core concepts you need to learn to master this subject

int in Java

In Java, the int primitive is used to hold whole positive and negative numbers, and zero. int stands for integer. For example:

int i=10;

Java int datatype

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

boolean in Java

In Java, the boolean primitive is used to hold true and false values.

For example:

boolean flag = true;

Java boolean data type

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

char in Java

In Java, the char primitive is used to hold a single character value. For example:

char ch = 'a';

The value must be between single quotes.

Java char primitive data type

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

Primitive Data Types in Java

Primitive datatypes in Java are in built types which are used to declare a variable. They are int, char, boolean, byte, long, short, double and float. null is also a primitive type but its value can not be null.

Java primitive data types

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

The available types are as follows: int, char, boolean, byte, long, short, double and float. null is another, but it can only ever store the value null.

String data type in Java

In Java, the String datatype is not a primitive. It is an Object that holds multiple characters. For example:

String name = "Robin";

Java Strings

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 ==.

Static Typing in Java

In Java, the types of all variables are known at compile time, rather than at runtime. This is called static typing, and allows for static checking, where errors are caught when the user compiles the program.

Static typing allows the user to set the data type of a variable during programming rather than execution.

Java static typing

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 in Java

In Java, the math operations of addition, subtraction, division, multiplication, modulo, and exponentiation are supported for int, double, and float primitive data types.

These operations can not be applied to objects directly.

Java math operations

In Java, basic math operations can be applied to int, double and float data types.

These operations are not supported for other data types.

Comparison Operators in Java

In Java, the comparison operations of equality, less than, greater than, and inequality are supported for primitive data types. The result of a comparison is a boolean value - true or false.

Java comparison operators

In Java, you can use comparison operators to compare two values. These are the available operators:

  • == “equal to”
  • > “greater than”
  • < “less than”
  • >= “greater than or equal to”
  • <= “less than or 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.

Chevron Left Icon
Learn Java: Variables
Lesson 1 of 2
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 allow us to keep track of all kinds of data in our program. In this lesson, we learned how to create and print several different datatyp…
  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