Data Types in Java
Java offers eight primitive data types, sometimes referred to as elemental or simple types. These types are denoted with a reserved term and are established by the language. Primitive values don’t share states with one another. For primarily efficiency reasons, these types are not objects because utilizing objects for basic kinds would result in unacceptably high overhead.
Four categories comprise the primitive types:
- Integer Types:
byte
,short
,int
, andlong
. These are used for whole-valued signed numbers. Java does not support unsigned integers.- byte: An 8-bit signed two’s complement integer, with a minimum value of -128 and a maximum of 127. It can be useful for saving memory in large arrays or when working with raw binary data. The default value for fields of this type is 0.
- short: A 16-bit signed two’s complement integer, ranging from -32,768 to 32,767. It can also be used to save memory in large arrays. The default value is 0.
- int: A 32-bit signed two’s complement integer, with a range from -2,147,483,648 to 2,147,483,647. It is the most commonly used integer type and is frequently employed to control loops and index arrays. When
byte
andshort
values are used in an expression, they are promoted toint
. The default value is 0. - long: A 64-bit signed two’s complement integer, useful when an
int
is not large enough, with a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The symbol ‘l’ or ‘L’ can be appended to long literals. The default value is 0L.
- Floating-Point Types:
float
anddouble
. These represent numbers with fractional precision.- float: A single-precision 32-bit IEEE 754 floating-point value. It’s faster and takes less memory than
double
but can be imprecise for very large or very small values. It should never be used for precise values like currency.f
orF
must be appended to a float literal. The default value for fields is 0.0f. - double: A double-precision 64-bit IEEE 754 floating-point value. It is generally the default choice for decimal values. Like
float
, it should not be used for precise values like currency. The default value is 0.0d.
- float: A single-precision 32-bit IEEE 754 floating-point value. It’s faster and takes less memory than
- Character Type (char) and Unicode System:
- char: A single 16-bit Unicode character. It has a minimum value of
\u0000
(or 0) and a maximum of\uffff
(or 65,535 inclusive). Java uses Unicode to support worldwide use and represent characters from all human languages. - Character variables can be assigned a value by enclosing the character in single quotes (e.g.,
'A'
). char
can also be treated as an integer type, allowing arithmetic operations.- Escape Sequences: For characters that cannot be entered directly, Java provides special escape sequences, like
\n
for newline,\t
for tab,\"
for double quote,\'
for single quote, and\\
for backslash. Unicode escape sequences can also be used, such as\u0108
.
- char: A single 16-bit Unicode character. It has a minimum value of
- Boolean Type (
boolean
):- boolean: There are just two potential values for boolean variables:
true
andfalse
. It is applied to basic flags that monitor true or false conditions. The value of this type, which represents a single bit of information, is adequate to governif
statements.
- boolean: There are just two potential values for boolean variables:
Variables
Programs can control the named storage that a variable offers in memory. The size, memory layout, range of values, and operations that can be performed on any variable in Java are all determined by its type.
- Declaration: Prior to being utilized, all variables must be defined. The type and name of the variable are stated in the most basic form of a variable declaration. A list that is separated by commas might include many variables of the same type.
- Variable names, also known as identifiers, are case-sensitive and can be any legal identifier, which is an infinitely long string of Unicode characters and numbers that starts with the letter
$
or_
. According to convention, you should start with a letter and stay away from$
or_
. - Initialization: In most cases, variables need to be assigned a value prior to use. An assignment statement (for example,
int cadence = 0;
) or a defined beginning value can be used to do this. - Java also permits dynamic initialization, in which the starting value is set at declaration time by any valid expression.
- Scope and Lifetime (main method scope): Variables can be declared inside any code block, which is a collection of zero or more statements enclosed in balanced brackets
{}
. By defining a scope, a block establishes what objects are visible and how long they last. A method’s parameters and other variables specified inside the method are considered local variables. They exist only within the block in which they are defined, are produced upon block entry, and are deleted upon block exit. Because they lack default values, local variables require the programmer to actively initialize them.
Constants
Fixed values that are represented in their human-readable format are referred to as constants in Java. They can be allocated to any variable of primitive type. When a constant is declared with the last keyword, its value cannot be altered once it has been initialized.
final double PI = 3.141592; // PI is a constant
Type Conversion and Casting
A value of one type is frequently assigned to a variable of another type. Java either uses explicit casting or automatically converts types to handle this.
- Automatic (Widening) Conversion: When two types are compatible and the destination type is larger an automatic (widening) conversion takes place. We also refer to this as a widening conversion. For enlarging conversions, the floating-point and integer kinds of numbers are compatible. For instance, it is possible to automatically convert a
byte
to anint
. - Explicit (Narrowing) Casting: When automatic conversion is not possible (e.g., assigning a larger type to a smaller type), an explicit cast is required. This is sometimes called a narrowing conversion. A cast uses the syntax
(target-type) value
.- Casting between incompatible types can lead to loss of information. For instance, casting a
long
to ashort
might remove high-order bits, and casting a floating-point value to an integer type will truncate the fractional component. - The
boolean
type cannot be cast to or from any other primitive type.
- Casting between incompatible types can lead to loss of information. For instance, casting a
- Automatic Type Promotion in Expressions: When different types of data are mixed within an expression, they are all converted to the same type based on Java’s type promotion rules.
- All
char
,byte
, andshort
values are first promoted to int. - Then, if one operand is a
long
, the whole expression is promoted tolong
. - If one operand is a
float
, the entire expression is promoted tofloat
. - If any operand is a
double
, the result isdouble
.
- All
- This guarantees that mathematical operations are carried out with adequate accuracy.