Data Type in Java
Data Type in Java
Topperworld.in
Data types in Java specify how memory stores the values of the variable. Each
variable has a data type that decides the value the variable will hold.
Moreover, Primitive Data Types are also used with functions to define their
return type.
©Topperworld
Java Programming
©Topperworld
A boolean data type can store either True or False. They can be used to check
whether two values are equal or not (basically in conditional statements to
return True or False). Typically, programmers use it as a flag variable to track
true or false conditions.
The default boolean value is False. Moreover, the boolean type’s size depends
on the Java Virtual Machine. Therefore, it fluctuates on different platforms.
Example-
©Topperworld
Java Programming
©Topperworld
The char data type stores a single character. It stores lowercase and
uppercase characters, which must be enclosed in single quotes. The char data
type in Java supports Unicode characters and provides provision to multiple
languages like English, French, German, etc. It takes memory space of 16 bits
or 2 bytes. The values stored range between 0 to 65536.
Example:-
Output: Ad
Integer type:
©Topperworld
Java Programming
• Byte
The byte is the smallest data type among all the integer data types. It is an 8-
bit signed two’s complement integer. It stores whole numbers ranging from -
128 to 127.
• Short
• Int
Int is a 32-bit signed two’s complement integer that stores integral values
ranging from 2147483648 (-2^31) to 2147483647 (2^31 -1). Its default value
is 0.
• Long
long is a 64-bit signed two’s complement integer that stores values ranging
from -9223372036854775808(-2^63) to 9223372036854775807(2^63 -1). It
is used when we need a range of values more than those provided by int. Its
default value is 0L. This data type ends with ‘L’ or ‘l’.
©Topperworld
Java Programming
©Topperworld
Example:
Float type:
©Topperworld
Java Programming
1. Float
It is a floating-point data type that stores the values, including their decimal
precision. It is not used for precise data such as currency or research data.
A Float value:
2. Double
The double data type is similar to float. The difference between the two is
that is double twice the float in the case of decimal precision. It is used for
decimal values just like float and should not be used for precise values.
A double value:
©Topperworld
Java Programming
©Topperworld
Example:
Output:
©Topperworld
Java Programming
©Topperworld
Array
An array holds elements of the same type. It is an object in Java, and
the array name (used for declaration) is a reference value that carries
the base address of the continuous location of elements of an array.
Example:
int Array_Name = new int[7];
©Topperworld
Java Programming
String
The String data type stores a sequence or array of characters. A string
is a non-primitive data type, but it is predefined in Java. String literals
are enclosed in double quotes.
Output:
©Topperworld
Java Programming
©Topperworld
Class
A class is a user-defined data type from which objects are created. It describes
the set of properties or methods common to all objects of the same type. It
contains fields and methods that represent the behaviour of an object. A class
gets invoked by the creation of the respective object.
There are two types of classes: a blueprint and a template. For instance, the
architectural diagram of a building is a class, and the building itself is an object
created using the architectural diagram.
Example:
©Topperworld
Java Programming
©Topperworld
Interface
An interface is declared like a class. The key difference is that the interface
contains abstract methods by default; they have nobody.
Example:
Enum
An enum, similar to a class, has attributes and methods. However, unlike
classes, enum constants are public, static, and final (unchangeable – cannot
be overridden). Developers cannot use an enum to create objects, and it
cannot extend other classes. But, the enum can implement interfaces.
©Topperworld
Java Programming
Declaration of an Enum:
enum Level {
LOW,
MEDIUM,
HIGH
}
©Topperworld