Data types in Java. Primitive data types. Wrapper types
Contents
- 1. What are the primitive data types exist in the Java programming language? Wrapper types
- 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?
- 13. Watch the video “Java. Data types“
- Related topics
Search other websites:
1. What are the primitive data types exist in the Java programming language? Wrapper types
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
In Java programs, it is advisable to use primitive types in cases where you need to ensure maximum performance (speed).
For every simple type, Java implements a corresponding wrapper type. This type is a class that contains facilities for performing a variety of common operations on this type. Unlike primitive types, wrapper types are part of the Java object hierarchy.
For primitive types (byte, short, int …) the following wrapper classes are implemented (Byte, Short, Integer, …)
byte => Byte short => Short int => Integer long => Long double => Double float => Float char => Character boolean => Boolean
If you need to process data of a primitive type or use standard methods for processing this data, then the corresponding wrapper type is used for this.
For example, below is a code snippet that defines the basic characteristics of a primitive type int using its wrapper type Integer
// Define characteristics of type int via wrapper type Integer // 1. Size of int type in bits int sizeBits = Integer.SIZE; // 32 // 2. Size of int type in bytes int sizeBytes = Integer.BYTES; // 4 // 3. The maximum allowable value of type int int maxValue = Integer.MAX_VALUE; // 2147483647 // 4. The minimum allowable value of type int int minValue = Integer.MIN_VALUE; // -2147483648
⇑
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.
⇑
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:
float double
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 a primitive type is not an object as in the case of a reference variable;
- 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 reference variable is the address of the memory area (reference) by which the internal data and the set of program code (methods) that process this data are formed;
- primitive data types are not part of the Java data type hierarchy and are not inherited from the Object class. Accordingly, there are no predefined methods for such types that are implemented in the Object class.
⇑
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.
⇑
13. Watch the video “Java. Data types”
⇑
Related topics
- Literals. Identifiers. Keywords. Comments
- Variables. Declaring variables. Initialization of variables
- Converting of types. Automatic type cast in expressions. Automatic advancement in expressions