×

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that they can store. Java defines the following eight primitive data types:

char, byte, short, int, long, float, double, boolean

boolean:- A boolean variable can store one of two values: true or false. It’s used in scenarios where only two states can exist.

Ex:- boolean married = true;

double:- We can use double primitive data types to store decimal numbers. double requires more space than float.

double total = 120.1762;

float:- The float primitive data types are used to store decimal numbers. The float requires less space than double, but it can store a smaller range of values than double. float is less precise than double.

Ex:- float average = 10.124F;

long:-  The long primitive data types use to store large integer numbers, the range of long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Ex:- long e = 1515l;

int:- int data type is a 32-bit signed two's complement integer. An integer is generally used as the default data type for integral values unless there is a concern about memory.

Ex:- int d = 123;

short:- Short data type is a 16-bit signed two's complement integer. The short data type can also be used to save memory as a byte data type. A short is 2 times smaller than an integer.

Ex:- short C = 11111;

byte:- Byte data type is an 8-bit signed two's complement integer. The byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.

Ex :- byte b = 127;

char:- char data type is a single 16-bit Unicode character. The char data type is used to store any character

Ex :- char a = 's';

It’s helpful to categorize the primitive data types like boolean, numeric, and character data types.

The boolean data type is used to store data with only two possible values. The value may be thought of as true/false, or yes/no or any other combination. The actual values that a  boolean can store are true and false.

Numeric values are stored as integers or decimal numbers. byte, short, int, and long can be used to store integers. The byte, short, int, and long data types use 8, 16, 32, and 64 bits, respectively.  Float and Double are used to store decimal numbers. The float uses 32 bit and double use 64 bits respectively, to store their values.


Related Topics

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.