×

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

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.