Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
String - stores text, such as "Hello". String values are surrounded by
double quotes
int - stores integers (whole numbers), without decimals, such as 123 or -
123
float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
boolean - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
String name = "John";
[Link](name);
Final Variables
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only):
final int myNum = 15;
myNum = 20;
Other Types
A demonstration of how to declare variables of other types:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Display Variables
The println() method is often used to display variables.
To combine both text and a variable, use the + character:
String name = "John";
[Link]("Hello " + name);
You can also use the + character to add a variable to another variable:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
[Link](fullName);
Java Data Types
As explained in the previous chapter, a variable in Java must be a specified data
type:
Example
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String
Data types are divided into two groups:
Primitive data types -
includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes
Primitive Data Types
A primitive data type specifies the type of a variable and the kind of values it
can hold.
There are eight primitive data types in Java:
Data Type Description
byte Stores whole numbers from -128 to 127
short Stores whole numbers from -32,768 to 32,767
int Stores whole numbers from -2,147,483,648 to 2,147,483,647
long Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float Stores fractional numbers. Sufficient for storing 6 to 7 decimal
digits
double Stores fractional numbers. Sufficient for storing 15 to 16
decimal digits
boolean Stores true or false values
char Stores a single character/letter or ASCII values
Numbers
Primitive number types are divided into two groups:
Integer types stores whole numbers, positive or negative (such as 123 or -
456), without decimals. Valid types are byte, short, int and long. Which type you
should use, depends on the numeric value.
Floating point types represents numbers with a fractional part, containing one
or more decimals. There are two types: float and double.
Integer Types
Byte
The byte data type can store whole numbers from -128 to 127. This can be used
instead of int or other integer types to save memory when you are certain that
the value will be within -128 and 127:
byte myNum = 100;
[Link](myNum);
Short
The short data type can store whole numbers from -32768 to 32767:
short myNum = 5000;
[Link](myNum);
Int
The int data type can store whole numbers from -2147483648 to
2147483647. In general, and in our tutorial, the int data type is the preferred
data type when we create variables with a numeric value.
int myNum = 100000;
[Link](myNum);
Long
The long data type can store whole numbers from -9223372036854775808 to
9223372036854775807. This is used when int is not large enough to store the
value. Note that you should end the value with an "L":
long myNum = 15000000000L;
[Link](myNum);
Floating Point Types
You should use a floating point type whenever you need a number with a
decimal, such as 9.99 or 3.14515.
The float and double data types can store fractional numbers. Note that you
should end the value with an "f" for floats and "d" for doubles:
Float Example
float myNum = 5.75f;
[Link](myNum);
Double Example
double myNum = 19.99d;
[Link](myNum);
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate
the power of 10:
Example
float f1 = 35e3f;
double d1 = 12E4d;
[Link](f1);
[Link](d1);
Boolean Types
Very often in programming, you will need a data type that can only have one of
two values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, Java has a boolean data type, which can only take the
values true or false:
boolean isJavaFun = true;
boolean isFishTasty = false;
[Link](isJavaFun); // Outputs true
[Link](isFishTasty); // Outputs false
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
char myGrade = 'B';
[Link](myGrade);
Strings
The String data type is used to store a sequence of characters (text). String
values must be surrounded by double quotes:
String greeting = "Hello World";
[Link](greeting);
Non-Primitive Data Types
Non-primitive data types are called reference types because they refer to
objects.
The main differences between primitive and non-primitive data types are:
Primitive types in Java are predefined and built into the language, while
non-primitive types are created by the programmer (except for String).
Non-primitive types can be used to call methods to perform certain
operations, whereas primitive types cannot.
Primitive types start with a lowercase letter (like int), while non-primitive
types typically starts with an uppercase letter (like String).
Primitive types always hold a value, whereas non-primitive types can
be null.
Examples of non-primitive types are Strings, Arrays, Classes etc.