This lesson we will explain you what are variables, which of them are there and how to use them. As you know big part of programming comes from Math although this gets a little more and more hidden by software you use to write code so it gets simple and fast for a good proficient programmer to embark on a project or for a guy that is starting with baby steps which are mainly the target of this initial lessons.
Let’s start!
The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable’s type and name, as you’ve already seen.
– Oracle Java Tutorials About Primitive
int number = 5; double db = 45.02; boolean ah = true;
With the metaphor given above we can say that we have 4 specific pots: a pot for holding int plants, another for holding double plants and so on. Now forgetting about the plants so everything gets more clear.
What do variables actually hold?
We do have tons of variables but let’s just start by saying what are the primitive variables according to Oracle with the full explanation:
- byte: This data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). It can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of
intwhere their limits help to clarify your code; - short: This data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with
byte, the same guidelines apply: useful for memory savings; - int: By default, the
intdata type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. - long: This data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1;
- float: This data type is a single-precision 32-bit IEEE 754 floating point. Useful if you need to save memory in large arrays of floating point numbers. It is useful for representing very large numbers but not with great precision;
- double: This data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used to represent values with great precision;
- boolean: This data type has only two possible values:
trueandfalse. It’s a logical variable. You can use it for simple flags that track true/false conditions. This data type represents one bit of information, pex.: a program about turning lights on where you use the True or False to check if the lights are on or off; -
char: This data type is a single 16-bit Unicode character. It has a minimum value of
'\u0000'(or 0) and a maximum value of'\uffff'(or 65,535 inclusive).
You can see that Strings are not in there and that’s because Strings are not primitive variables. You can be persuaded to think the opposite due to the fact that you start using these early on your programming experience but they are considered Simple Data Objects which will be talked about later on.
Every primitive variable, if not initiated, starts out with the following values:
| Data Type | Default Value (for fields) |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | ‘\u0000’ |
| String (or any object) | null |
| boolean | false |
Still accessing an uninitialized local variable will result in a compile-time error so be sure to declare them if you want to use them.
We can change this variables representation as literals. You may have noticed that the new keyword isn’t used when initializing a variable of a primitive type. Primitive types are built into the language and aren’t objects created from a class. A literal is the source code representation of a fixed value and so literals are represented directly in your code without requiring computation.
Integer Literals
Values of the integral types byte, short, int, and long can be created from int literals. Values of type long that exceed the range of int can be created from long literals. So integers can be represented as decimal, binary or hexadecimal like below:
int dec_rep = 45; int bin_rep = 0b101101; int hex_rep = 0x2d;
In our website, in a different category, you will soon see how to represent this numbers and how to calculate the binary and hexadecimal representation for a given decimal and vice-versa.
Floating-Point Literals
A floating-point literal is of type float if it ends with the letter F or f or type double can be represented with he letter D or d at the end.
This data types can also be use E or e (for scientific notation).
double d = 5624.2d; double d1 = 5.6242e3; float f = 5624.2f;
Character and String Literals
Literals of types char and String may contain any Unicode (UTF-16) characters. If your editor and file system allow it. Be sure to always use ‘single quotes’ for char literals and “double quotes” for String literals.
The Java also supports a the following sequence of characters:
\b(backspace)\t(tab)\n(line feed)\f(form feed)\r(carriage return)\"(double quote)\'(single quote)\\(backslash)