Datatypes
Data types
• Data types specify the different sizes and values that can be stored in
the variable. There are two types of data types in Java:
• Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
• Non-primitive data types: The non-primitive data types include
Classes, Interfaces, and Arrays.
Java Primitive Data Types
• In Java language, primitive data types are the building blocks of data
manipulation. These are the most basic data types available in
Java language.
• Java is a statically-typed programming language. It means, all
variables must be declared before its use. That is why we need to
declare variable's type and name.
• Java is a statically-typed programming language. It means, all
variables must be declared before its use. That is why we need to
declare variable's type and name.
• Java Primitive data types:
• boolean data type
• byte data type
• char data type
• short data type
• int data type
• long data type
• float data type
• double data type
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Boolean Data Type
• In Java, the boolean data type represents a single bit of information with two
possible states: true or false. It is used to store the result of logical expressions
or conditions.
Example:
• Boolean a = false;
• Boolean b = true;
• One key feature of the boolean data type is its use in controlling program flow.
• It is commonly employed in conditional statements such as if, while,
and for loops to determine the execution path based on the evaluation of a
boolean expression. For instance, an if statement executes a block of code if the
boolean expression evaluates to true, and skips it if the expression is false.
Byte Data Type
• The byte data type in Java is a primitive data type that represents an 8-bit
signed two's complement integer. It has a range of values from -128 to
127. Its default value is 0. The byte data type is commonly used when
working with raw binary data or when memory conservation is a concern,
as it occupies less memory than larger integer types like int or long.
Example:
• byte a = 10, byte b = -20
• One common use of the byte data type is in reading and writing binary
data, such as files or network streams.
Short Data Type
• The short data type in Java is a primitive data type that represents a
16-bit signed two's complement integer. It has a range of values from
-32,768 to 32,767. Similar to the byte data type, short is used when
memory conservation is a concern, but more precision than byte is
required. Its default value is 0.
Example:
• short s = 10000, short r = -5000
Int Data Type
• The int data type in Java is a primitive data type that represents a 32-bit
signed two's complement integer. It has a range of values from -
2,147,483,648 to 2,147,483,647. The int data type is one of the most
commonly used data types in Java and is typically used to store whole
numbers without decimal points. Its default value is 0.
Example
• int a = 100000, int b = -200000
• In Java, int variables are declared using the int keyword. For example, int
myInt = 100; declares an int variable named myInt and initializes it with the
value 100. int variables can be used in mathematical expressions, assigned
to other int variables, and used in conditional statements.
Long Data Type
• The long data type in Java is a primitive data type that represents a
64-bit signed two's complement integer. It has a wider range of values
than int, ranging from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. Its default value is 0.0F. The long data
type is used when int is not large enough to hold the desired value, or
when a larger range of integer values is needed.
Float Data Type
• The float data type in Java is a primitive data type that represents
single-precision 32-bit IEEE 754 floating-point numbers. It can
represent a wide range of decimal values, but it is not suitable for
precise values such as currency. The float data type is useful for
applications where a higher range of values is needed, and precision is
not critical.
• float f1 = 234.5f
• One of the key characteristics of the float data type is its ability to
represent a wide range of values, both positive and negative,
including very small and very large values.
Char Data Type
• The char data type in Java is a primitive data type that represents a single
16-bit Unicode character. It can store any character from the Unicode
character set, that allows Java to support internationalization and
representation of characters from various languages and writing systems.
The char data type is commonly used to represent characters, such as
letters, digits, and symbols, in Java programs. It can also be used to perform
arithmetic operations, as the Unicode values of characters can be treated as
integers. For example, you can perform addition or subtraction operations
on char variables to manipulate their Unicode [Link] letterA = 'A'
Non-Primitive Data Types in Java:
1. Class
• In Java, non-primitive data types, also known as reference data types,
are used to store complex objects rather than simple values.
• Unlike primitive data types that store the actual values, reference
data types store references or memory addresses that point to the
location of the object in memory.
• This distinction is important because it affects how these data types
are stored, passed, and manipulated in Java programs.
Interface
• Interfaces are another important non-primitive data type in Java. An
interface defines a contract for what a class implementing the interface
must provide, without specifying how it should be implemented. Interfaces
are used to achieve abstraction and multiple inheritance in Java, allowing
classes to be more flexible and reusable.
• Arrays
• Arrays are a fundamental non-primitive data type in Java that allow you to
store multiple values of the same type in a single variable. Arrays have a
fixed size, which is specified when the array is created, and can be accessed
using an index. Arrays are commonly used to store lists of values or to
represent matrices and other multi-dimensional data structures.
Enum
• Java also includes other non-primitive data types, such as enums and
collections. Enums are used to define a set of named constants,
providing a way to represent a fixed set of values. Collections are a
framework of classes and interfaces that provide dynamic data
structures such as lists, sets, and maps, which can grow or shrink in
size as needed.