Data types in Java
- 1. What are the primitive data types exist in the Java programming language?
- 2. What are the advantages of primitive types in Java?
- 3. What integer data types exist in Java?
- 4. What is the length (dimension) of integer data types?
- 5. Examples of description, initialization and using the variables of integer types.
- 6. What data types belong to the type of floating-point (real type)?
- 7. Example of the use of variables of floating point type
- 8. What are the features of using the data type ‘char’?
- 9. Examples of description and use variables of type char
- 10. What is the purpose of type boolean in the Java programs?
- 11. What are the differences between primitive types and reference types?
- 12. Are there unsigned primitive types in Java?
- Related topics
1. What are the primitive data types exist in the Java programming language?
Primitive data types can be used in the programs directly or for creating the own types. Primitive types are the basis for all other data types.
There are 8 primitive (simple) data types in Java:
byte short int long char float double boolean
2. What are the advantages of primitive types in Java?
Primitive types represent single values. Primitive types are not the complex objects. Primitive types are the simple types. You can create your own complex types based on primitive types.
Primitive types expedient for using in the programs, as it significantly increases the performance. If you implement the primitive types as objects, it would lead to a significant decrease in performance.
Primitive types have a well-defined range of values.
3. What integer data types exist in Java?
The integer data types are integers, which may be either positive or negative.
Integer data types are following:
byte short int long
In Java there are no integer unsigned data types.
4. What is the length (dimension) of integer data types?
The length of integer data types is:
- byte – 8 bits;
- short – 16 bits;
- int – 32 bits;
- long – 64 bits.
5. Examples of description, initialization and using the variables of integer types.
... byte b; // variable with name 'b' of the 'byte' type short sh; // the variable of type 'short' int i=67; long l = 39009284783278l; // variable 'l' of type 'long' b = -13; sh = 1000; ...
6. What data types belong to the type of floating-point (real type)?
In Java, to types of floating-point (real type) belong the types:
For the “float” type belong the single-precision numeric values. For the “double” type belong the numeric values of double precision.
A variable of the float type occupies 32 bits of memory.
The variable of the double type is 64 bits.
7. Example of the use of variables of floating point type.
float f; // variable named f of float type f = 3.998f; // f variable assign a value 3.998 double d; // variable named d of double type d = -340.349489287; float ff = -3.99f; // initialize a variable ff of type 'float' double dd = 779303028.3398; // initialize a variable dd of type 'double'
8. What are the features of using the data type ‘char’?
In Java to represent the characters is entered the data type ‘char‘.
Variables of type ‘char‘ occupy the 16-bit in the memory of computer. This is due to the fact that for the representation of the characters Java uses Unicode. The range of values of this type is from 0 to 65535.
Using the Unicode encoding due to the fact, that Java programs are used throughout the world, that is in countries where the need for representation of characters the 2 bytes (16 bits).
These countries are, for example, Japan, China.
9. Examples of description and use variables of type char.
char c; // variable named 'c' of type 'char' c = 'Z'; // assignment a literal 'Z' c = 90; // c = 'Z' c--; // c = 'Y' char cc = '2'; // initialization of variable 'cc' to value '2'
10. What is the purpose of type boolean in the Java programs?
The ‘boolean‘ type is assigned for storing boolean values. Variables of type boolean can have only two values: true or false.
Example of using the variables of type ‘boolean‘:
boolean b; b = true; boolean bb=false; // bb = false bb = 5>4; // bb = true bb = 55==6; // bb = false
11. What are the differences between primitive types and reference types?
Between primitive types and reference types, the following differences exist:
- a variable of primitive type is not a reference variable (object);
- a variable of primitive type is “automatic” and is stored on the stack. The object (variable) is stored in the “heap”. Therefore, the performance of working with variables of a primitive type is higher;
- when declaring a variable of a primitive type, you do not need to allocate memory using the ‘new’ operator;
- the variable of primitive type retains its value directly, unlike the reference variable.
12. Are there unsigned primitive types in Java?
No. In the Java language, all primitive types are signed, that is, they can take negative values.
- Literals. Identifiers. Keywords. Comments
- Variables. Declaring variables. Initialization of variables
- Converting of types. Automatic type cast in expressions. Automatic advancement in expressions