JAVA variables types
Each variable has a data type which indicates the size of that variable.
Here is a table which describes the size of each data type.
The declaration like the other JAVA instructions finishes by a
semi-colon “;”.
After declaring a variable, we should initialize it by giving it a
value. In fact, the value given to a variable must match the data type of that
variable.
When we give a value to a variable, we say that we assigned a value to that variable.
For example, if the type of the variable is Integer, we should initialize it with an integer.
In JAVA, the variables belong to two types:
·
The
JAVA variables of simple type (primitive type):
integers, floats, characters, Booleans
·
The
JAVA variables of complex type (non primitive: objects):
we deal with this type of JAVA variables when we
study the object oriented programming in JAVA.
So, you should put the provided code inside the main method that we have
created in the JAVA development tool tutorial and display the content of the variable using the
instruction System.out.print(variableName) like this example:
public static void main(String[] args){
int i;
i = 5;
System.out.print(i);
}
JAVA variables of numerical types
The type byte (8 bits: a bit can have a value which can be either 0 or 1)
can contain integers between -128 (- 27) and +127 (+27 -
1)
byte temperature;
temperature = 64;
The type short (2 bytes) can contain integers between -32768 (- 215)
and +32767 (215 - 1)
short maxSpeed;
maxSpeed = 32000;
The type int (4 bytes) can contain integers between – 2147483648 (- 231) and + 2147483647 (+231
-1)
int temperature;
temperature = 15600000;
The type long (8 bytes) can contain integers between - 263
and +263 -1
long companyIncome;
companyIncome = 9460700000000000L;
Remark: you should add the L at the end of the number
to inform the JVM that the number is a long.
Otherwise, the JVM will consider it as a variable of
type int.
The type float (4 bytes) is used to store the numbers with a floating
point:
float pi;
pi = 3.141592653f;
or
float nombre;
nombre = 2.0f;
Remark: you notice the f appended to the number in the
first example and you know why we added it.
You may also notice that the comma is replaced by a
dot in JAVA.
In the second example, we added a “.0” to the number
even if the number is a round number.
The type double (8 bytes) is a number with a floating point like the
float but it can hold a bigger number.
double division;
division = 0.333333333333333333333333333333333333333333334d;
Remark: we should use the suffix “d” to inform the JVM that the number is a double.
JAVA variables holding a character
The type char contains only one character surrounded by apostrophes “’”
char character;
character = 'A';
JAVA variables holding a boolean
The variable of type boolean can be either true or false (written
without quotation marks)
boolean question;
question = true;
JAVA variables holding a text
The String data type manages text data (strings). It is a complex data
type (the name of a class which belongs to the core of JAVA).
Remark: The name of the data type begins by a capital
letter and in the initialization we use the double quotation marks.
Also, bare in mind that the primitive data types begin
by a lower case letter: int, char, float, double,…
//First declaration and initialization method
String text;
text = "My first text";
// Second declaration and initialization method
String str = new String();
str = " My second text";
// Third declaration and initialization method
String txt = " My third text ";
// Fourth declaration and initialization method
String t = new String("My fourth text");
The String class‘s name begins by a capital letter because we respect the naming rules.
You should try to respect the naming rules which are the following:
1)
The
class names should begin with a capital letter like our String class
2)
The
variable names should begin by a lower case letter
3)
If
the variable name is composed by many words, we should begin the intermediary
words by a capital letter
4)
We
should avoid accentuation
Here are some examples of class and variable names:
//class naming rules
public class Kid{}
public class Number{}
public class HealthAndFitness{}
//variables naming rules
String name;
String godOfWar;
int number;
int moreImportantNumber;
We can make the declaration and the initialization at the same time like
this:
int number = 32;float pi = 3.1416f;
char charac = 'z';
String text = new String("hello");
And if you have many JAVA variables of
the same type, you can declare and initialize them in the same line.
int number1 = 2, number 2 = 3, number 3 = 0;
Here we declared and initialized three JAVA variables (number1, number2 and number3) of the same type (int) in the same line.
Post a Comment