In this Java 6 tutorial we will see which are the primitive data types, how to define variables, which are variables default values and what names we can give to these variables.
In Java there are two types of variables:
-
primitive
-
objects or references
In this tutorial we focus on primitive data types variables and objects (reference-type variables) will be described in the tutorial about classes. Other topics shown in this tutorial can be found in the
Primitive types are basic data types that can not be broken up into other subtypes. In Java 6 there are 8 primitive data types:
Data type |
Size |
Range for signed values |
Category |
byte | 1 byte | -128 –> 127 | integer |
short | 2 bytes | -32768 –> 32767 | integer |
int | 4 bytes | -2147483648 –> 2147483647 | integer |
long | 8 bytes | -9,223,372,036,854,775,808 –> 9,223,372,036,854,775,807 | integer |
float | 4 bytes | 7 significant digits | real simple precision |
double | 8 bytes | 15 significant digits | real double precision |
char | 2 bytes | ’\u0000’ –> ’\uffff’ 0 –> 65535 |
16 bits Unicode char |
boolean | 1 bit | true or false | logic value |
In Java a variable is defined using the syntax:
variable_type variable_name;
where:
variable_type – one of the eight primitive types or classes defined type of programs;
variable_name – variable names defined by the programmer;
How to define variables in Java:
- variable name must begin with a letter, underscore symbol (_) or dollar sign ($);
- variable names can not begin with a digit;
- after first character, you can use digits in the variable name;
- variable name can not be a word reserved for Java language, a keyword ( Java keywords );
- several variables can be defined simultaneously;
public class Variables {
public static void main()
{
//variables defined correctly
int vb1,vb2;
float fvb2;
double _temp;
boolean $flag;
//wrong defined variables - compiling error
byte 3vb; // starts with a digit
long for; // uses a Java keyword
}
}
-
variable names are chosen by the programmer, but for efficiency, there are some naming conventions about variable names: Hungarian notation , CamelCase; although it is not mandatory, in Java there is a recommendation for variables names; this convention is derived from CamelCase and implies that variable names to be more suggestive, and if they are composed of several words, the first word is spelled with lower case; from the second one, each word has its first letter capitalized; also, as a recommendation, boolean variables shoud start with is (e.g. boolean isNumber; )
int iBooksNumber; //Hungarian Notation
int BooksNumber; //CamelCase
int booksNumber; //Java mixed case
How to initialize variables in Java
At initialization of variables should be taken into account their type, because in Java it is NOT possible to assign values that have a different type than the variable. From this viewpoint, Java is strong type language (in opposition VisualBasic is not). For example, the following instructions generate compilation errors Possible loss of precision:
float vb2 = 23.5; //compilation error - possible loss of precision
int vb3 = 45.6; //compilation error - possible loss of precision
boolean test = 23; //compilation error - incompatible types
In the case of float type variable, vb2, the compiler error is generated because the real constant values are considered by default as double . The correct way is to put an f at the end of the value, ie 23.5f.
- the type of value must be the same as the variable type;
- several variables can be initialized at the same time;
- in Java, the only possible values for Boolean variables are true or false (in C or C + +, any numeric value other than 0 is considered true);
- float constant values are defined with the symbol f in the end, because, implicitly, the real constants are considered to be double;
- character symbols are defined between ‘ ‘ (apostrophe) and not between " " (quotation marks);
- real values can be defined in scientific format, for example, 1.234e2 is equivalent to 123.4 ;
- integer values in base 8 are prefixed with 0, eg 021 is 17 in base 10;
- integer values in base 16 (hexadecimal representation) are prefixed with 0x, for example 0×11 is 17 in base 10;
- char variables can have as values a series of special characters, escape sequences:
Escape sequences |
Value |
\b | backspace |
\t | tab |
\n | line feed |
\f | form feed |
\r | carriage return |
\” | double quotes |
\’ | apostrophe |
\\ | backslash |
Examples of variables that are correctly initialized with primitive data types::
int value1;
int value2;
value1 = value2 = 17;
int valueB8 = 021;
int valueB16 = 0x11;
float value3 = 123.4f;
double value4 = 123.4;
char c = 'a';
char enter = '\r';
boolean isNumber = true;
long value5 = 17L;
What are the default values for variables in Java
If variables are not initialized then the compiler may initialize them with default values (but, not in all situations!)
Primitive |
Default value |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | ‘\u0000’ |
boolean | false |
Important! Local variables (defined inside a method) are NOT initialized with default values by the compiler. The default values in the above table are used for attributes, fields, of objects (variables defined in the class). Using an uninitialized local variables will generate a compiler error:
public static void main(){
int sum; //local variable declared in main method
sum = sum + 10; //compiler error
//variable sum might not have been initialized
}
For other Java 6 topics described in this tutorial you can check Tutorial Java 6 – Contents.