Data Types in Java with example
Data Types in Java:
Every variable in Java has a data type. Data Types specify the size and type of values that can be stored. There are mainly two types of data types in Java:
- Primitive Data Types or Built-in data types.
- Non-Primitive Data Types or Reference data types
Primitive Data Types in Java:
Primitive Data Types are classified into two categories :
- Numeric
- Non-Numeric
Numeric Data Types in Java:
Numeric Data Types are two types –
Integer Data Type: Integer data types can hold whole numbers such as 25, -40, and 2456. The size of the values that can be stored depends on your choice’s integer data types. Java supports four types of integer data types and they are byte, short, int and long. It does not support unsigned integer data types. So, all Java values are signed which means those values can be positive or negative.
Floating-Point Data Type Floating-Point Data Type can also hold whole numbers containing fractional parts such as 25.6675. There are two types of Floating-Point Data Types are given as follows:
Float: The float type values are single precision numbers such as 3.142857. A single precision number using 6 decimal digits.
Double: Double types values are represent double precision number such as 3.142857142857. A double-precision number using 12 decimal digits.
Non-Numeric Data Types in Java:
Non-Numeric Data Types are also two types:
Character Data Type: Java provides a character data type that says char. The char type assumes a size of 2 bytes, but it can hold only a single character.
Boolean Data Type: Boolean Data Type is used when we want to test a particular condition during the execution of the program. There are only two values in Boolean Data Types such as true or false. A boolean data type is denoted by the keyword boolean and it uses only one bit of storage.
Non Primitive Data Types in Java:
There are mainly five types of non-primitive data types:
Class: A class is a user-defined data type with a template that serves to define its properties. A class is essentially a description of how to make an object that contains fields and methods.
Object: In Java, objects are essentially a block of memory that contains space to store all the instance variables.
Array: An array is a group of contiguous or related data items that share a common name.
String: It represents a sequence of characters in Java by using a character array.
Interface: The interface is a kind of class, it contains methods and variables. It defines only abstract methods and final fields.