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;

boolean in Java

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

For example:

boolean flag = true;

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.

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.

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";

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.

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.

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.

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 […] 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 […] s, which are objects, instead of primitives. Object…

  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? […] or […] While both of these will compile, the second example is way more easy t…

  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 […] , the primitive type that can hold big decimal numbers….

  2. 2

    In our bank account example from the last exercise, we used […] to add! […] If we wanted to withdraw from the balance, we would use […] : […] Addition and subtraction work with …

  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 af…

  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: […] Notice t…

  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, […] ! […]

  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 […] , […] , […] , […] , and many other operators. Most of these only work on primitives, but s…

  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