freeCodeCamp/guide/english/java/variables/index.md

3.3 KiB

title
Variables

Variables

Variables store values. They are the most basic entity used to store data such as text, numbers, etc. in a program.

In Java, variables are strongly typed, which means you have to define the type for each variable whenever you declare it. Otherwise, the compiler will throw an error at compile time. Therefore, each variable has an associated 'data-type' of one of the following:

  • Primitive Type: int, short, char, long, boolean, byte, float, double
  • Wrapper Type: Integer, Short, Char, Long, Boolean, Byte, Float, Double
  • Reference Type: String, StringBuilder, Calendar, ArrayList, etc.

You may have noticed that the Wrapper Type consists of types spelled exactly like the Primitive Type, except for the capitalised alphabet in the begining (like the Reference Type). This is because the Wrapper Types are actually a part of the more general Reference Types, but closely linked with their primitive counterparts via autoboxing and unboxing. For now, you just need to know that such a 'Wrapper Type' exists.

Typically, you can declare (i.e., create) variables as per the following syntax: <data-type> <variableName>;

// Primitive Data Type
int i;

// Reference Data Type
Float myFloat;

You can assign a value to the variable either simultaneously when you are declaring it (which is called initialisation), or anywhere in the code after you have declared it. The symbol = is used for the same.

// Initialise the variable of Primitive Data Type 'int' to store the value 10
int i = 10;
double amount = 10.0;
boolean isOpen = false;
char c = 'a'; // Note the single quotes

//Variables can also be declared in one statement, and assigned values later.
int j;
j = 10;

// initiates an Float object with value 1.0
// variable myFloat now points to the object
Float myFloat = new Float(1.0);

//Bytes are one of types in Java and can be
//represented with this code
int byteValue = 0B101;
byte anotherByte = (byte)0b00100001;

As evident from the above example, variables of Primitive type behave slightly differently from variables of Reference (& Wrapper) type - while Primitive variables store the actual value, Reference variables refer to an 'object' containing the actual value. Java Programming language defines mainly three kind of variables.

  1. Instance variables
  2. Static Variables
  3. Local Variables

You can find out more in the sections linked below.

Other Resources