Table of Contents
A data type is an attribute of a variable that tells the compiler or interpreter how the programmer intends to use the variable. It defines the operations that can be done on the data and what type of values can be storedAccording to the properties they possess, data types are divided into two groups:
Learn Coding in your Language! Enroll Here!
- Primitive Data Types
- Non-Primitive Data Types
Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods.
Non-Primitive Data Types: These data types are not actually defined by the programming language but are created by the programmer. They are also called “reference variables” or “object references” since they reference a memory location that stores the data.
Primitive Data Types
Data types in Java are classified into 4 aspects as int, float, character, and boolean. But, in general, there are 8 data types. They are as follows:
- Booleandata type
- bytedata type
- char data type
- shortdata type
- intdata type
- longdata type
- floatdata type
- doubledata type
Boolean data type
A boolean data type comprises of a bit of information and can store only true or false values. This data type is used to track true/false conditions. Now let’s write a small program and understand how it works.
class booleanDataType{
public static void main(String args[]){
// Setting the values for boolean data type
boolean Java = true;
boolean Python = false;
System.out.println(Java); // Output will be true
System.out.println(Python); // Output will be false
}
}
byte data type
This is an example of a primitive data type. It is an 8-bit signed two’s complement integer. It stores whole numbers that lie between -128 to 127. A byte data type is helpful for saving memory in large amounts. Now let’s write a small program and understand how it works.
class ByteExample {
public static void main(String[] args) {
byte n, a;
n = 127;
a=177;
System.out.println(n); // prints 127
System.out.println(a); // throws an error because it cannot store more than 127 bits
}
}
char data type
This data type is used to store a single character. The character must be enclosed within single quotes, like ‘E’ or ‘e’. Alternatively, you can also use ASCII values to display certain characters. Let’s take a small example and see how it works.
char alpha = ‘J’; char a = 65, b = 66, c = 67; System.out.println(alpha); // prints J System.out.println(a); // Displays 65 System.out.println(b); // Displays 66 System.out.println(c); // Displays 67
short data type
A short data type is greater than byte in terms of size and less than a integer. It stores the value that ranges from -32,768 to 32767. The default size of this data type: 2 bytes. Let’s take an example and understand the short data type.
class ShortExample {
public static void main(String[] args) {
short n= 3435,
System.out.println(n); // prints the value present in n i.e. 3435
}
}
Learn Coding in your Language! Enroll Here!
int data type
This data type can store whole numbers from -2147483648 to 2147483647. Generally, int is the preferred data type when you create variables with a numeric value.
For example:
int num = 5464564;
System.out.println(num); // prints 5464564
long data type
This data type is a 64-bit two’s complement integer. By default, the size of a long data type is 64 bit and its value ranges from -2 63to 2 63–1.
For example:
long num = 15000000000L;
System.out.println(num); // prints 15000000000
Floating Datatypes
You should use a floating point type whenever you need a number with a decimal, such as 8.88 or 3.14515.
float data type
This data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an “f”. Let’s take a small example and understand this data type in a detailed manner.
float num =67;
System.out.println(num); // prints the floating number value
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a “d”:
double num = 79.678d;
System.out.println(num); // prints double value
Learn to code from industry experts! Enroll here
Non-Primitive Datatypes
1: What is the default value of a boolean in Java?
Non-Primitive data types refer to objects and hence they are called reference types. Examples of non-primitive types include Strings, Arrays, Classes, Interface, etc.
Strings
It is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The Strings: String java.lang.String class is used to create a string object.
Arrays
Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index.
Classes
class in Java is a blueprint which includes all your data. A class contains fields(variables) and methods to describe the behavior of an object.
Interface
Like a class, an interface can have methods and variables, but the methods declared are by default abstract (only method signature, no body).
Difference between primitive and non-primitive data types
The difference between primitive and non-primitive data types are as follows:
- Primitive types are predefined in Java. Non-primitive types are created by the programmer and is not defined by Java.
- Non Primitive types can be used to call methods to perform certain operations, while primitive types cannot.
- A primitive type always has a value, whereas non-primitive types can be null.
- A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
- The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Learn Coding in your Language! Enroll Here!
Why is it important to choose Entri?
- Excellent online platform for all the Competitive Exams.
- Provides updated materials created by the Entri Experts.
- Entri provides a best platform with full- length mock tests including previous year question papers.
- You can download the app for free and join the required classes.
- Entri wishes you all the best for your examinations and future endeavours.
“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”