Variables and Data Types

Variables and Data Types

In Java every variable has a type. The type decides what kind of value the variable can store.

Primitive types

Some of the most commonly used primitive types are:

  • int integer numbers like 42
  • long larger integer numbers
  • double decimal numbers like 3.14
  • boolean logical values true or false
  • char a single character like A

Example:

java
int age = 25; double price = 19.99; boolean active = true; char grade = 65; // example character (ASCII code for A)

Reference type String

String is used for text. It is written with double quotes.

java
String name = \"Froquiz\"; System.out.println(name);

You can join strings with the plus operator:

java
String fullName = \"Ada\" + \" \" + \"Lovelace\";

In the next lesson you will see how these values are used with conditionals and loops.