Introduction to Java Programming.
Chapter 1: Introduction
1.8 Storing Information: Java Data Types
In
programming, a data type classifies the type of value a
variable can hold. It specifies the size and type of values that can be stored
in a memory location, as well as the operations that can be performed on those
values.
Understanding
Java’s data types is fundamental to writing effective and efficient code. Java
categorizes data types into two main groups:
·
Primitive Data Types
·
Non-Primitive (Reference) Data Types
1.8.1 Primitive Data Types
Primitive data types are the most basic data types in Java.
They are predefined by the language and named by a keyword. They are stored
directly in memory (on the stack for local variables, or within an object’s
memory on the heap for instance/static variables) and contain the actual value.
Numeric Types
These are used to store numerical values. They are further
divided into integer types and floating-point types.
Integer Types
Used for whole numbers (without a fractional component).
·
byte
o Size: 8-bit signed two’s complement integer
o Range: –128 to 127
o Memory Usage: 1 byte
o Example: byte
smallNumber = 50;
·
short
o Size: 16-bit signed two’s complement integer
o Range: –32,768 to 32,767
o Memory Usage: 2 bytes
o Example: short
quantity = 1000;
·
int (default integer type)
o Size: 32-bit signed two’s complement integer
o Range: –2³¹ to 2³¹–1 (approx. ±2 billion)
o Memory Usage: 4 bytes
o Example: int
population = 1500000;
·
long
o Size: 64-bit signed two’s complement integer
o Range: –2⁶³ to
2⁶³–1 (approx. ±9 quintillion)
o Memory Usage: 8 bytes
o Example: long
worldPopulation = 7800000000L;
(Note the L suffix)
Floating-Point Types
Used for numbers with a fractional (decimal) component.
·
float
o Size: 32-bit IEEE 754 single-precision floating-point
o Precision: Up to 7 decimal digits
o Memory Usage: 4 bytes
o Example: float
temperature = 25.5f;
(Note the f suffix)
·
double (default
floating-point type)
o Size: 64-bit IEEE 754 double-precision floating-point
o Precision: Up to 15 decimal digits
o Memory Usage: 8 bytes
o Example: double
pi = 3.1415926535;
Character Type
·
char
o Size: 16-bit Unicode character
o Memory Usage: 2 bytes
o Example: char
grade = 'A';
or char initial = ‘\u0041’;
o Note: char
literals are enclosed in single quotes (‘ ‘
).
Boolean Type
·
boolean
o Represents one bit of information (size not precisely
defined by JVM; typically uses 1 byte in arrays)
o Values: true
or false
o Example: boolean
isJavaFun = true;
Declaration and Initialization
Before using a variable, it must be declared. You may
initialize it at the time of declaration or later.
·
Syntax:
dataType
variableName;
·
Syntax with
Initialization:
dataType
variableName = value;
Examples:
// Declaration only
int count;
count
=
10;
// Declaration + Initialization
double
price
=
99.99;
char
status
=
‘P’;
boolean
isActive
=
true;
1.8.2 Non-Primitive Data Types
Non-primitive data types, also known as reference
types, store references (memory addresses) to objects rather
than the actual data. Examples include:
String
·
A special class (java.lang.String
) used to store sequences of characters (text).
·
Immutable:
Once created, its content cannot be changed.
·
Examples:
·
String
message
=
"Hello, Java!";
·
String
name
=
new
String(
"Parmeshwar");
Arrays
·
Used to store a
collection of fixed-size elements of the same data type.
·
Example:
·
int[] numbers = {
1,
2,
3,
4,
5};
·
String[] names =
new
String[
3];
Classes and Interfaces
·
User-defined classes
and interfaces are also non-primitive data types.
·
Example:
·
Student
s1
=
new
Student();
// s1 holds the reference to a Student object
1.8.3 Default Values and Literals
Literals
Literals are fixed values used in programs. Examples:
·
10
→ Integer literal
·
3.14
→ Floating-point literal (default double)
·
'A'
→ Character literal
·
"Hello"
→ String literal
·
true
, false
→ Boolean literals
·
100L
→ Long literal
·
2.5f
→ Float literal
Default Values for Primitive Types
For instance variables (class members), Java assigns
default values automatically. Local variables must
be explicitly initialized.
Data Type |
Default Value |
byte |
0 |
short |
0 |
int |
0 |
long |
0L |
float |
0.0f |
double |
0.0d |
char |
‘\u0000’
(null character) |
boolean |
false |
Reference
Types (String, Arrays, Classes, etc.) |
null |
Example:
class
DefaultValueExample {
int defaultInt;
// Default = 0
boolean defaultBoolean;
// Default = false
String defaultString;
// Default = null
public
static
void
main(String[] args) {
DefaultValueExample
obj
=
new
DefaultValueExample(
);
System.out.println(
"Default int: " + obj.defaultInt);
System.out.println(
"Default boolean: " + obj.defaultBoolean);
System.out.println(
"Default String: " + obj.defaultString);
}
}