TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Java Data Types Explained
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java BasicJava Tutorials

Java Data Types Explained

Last updated: Sep 30, 2023 6:37 pm
By Meenakshi Agarwal
Share
4 Min Read
Work with Java Data Types
SHARE

This tutorial provides you with the details of various Java data types. It is an essential topic for you to know and develop real Java programs.

Contents
Data types in JavaPrimitive types:· Integer (int):· byte:· short:· long:· float:· double:· boolean:· char:Non-primitive types:

Learn to Use Data Types in Java

The tutorial has the following sections to help you learn quickly.

Data types in Java

Every programming language provides a set of data types for the programs to pass data and tell its compiler/interpreter to process it accordingly.

Java too defines the following two major categories for data types:

  • Primitive
  • Non-primitive

Primitive types:

Primitive data types consist of the following eight variations:

· Integer (int):

This data type is used to hold integer data values. The size of an int is 4 bytes. And the range of values it can store is – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).

int a = 10;

· byte:

This data type can also hold integer values. The size of a single byte is 1 byte. Therefore, the range of values it can store is -128 to 127 (inclusive).

byte b = 10;

· short:

It holds integer values, and its size is 2 bytes. The range of values is -32,768 to 32,767 (inclusive).

short c = 120;

· long:

The long data type is the biggest of all data types as it consumes memory of 8 bytes and holds integer type values. Range of long is -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).

long d = 10000;

By default, integer literals will have an int data type. Literal is the value stored in the variable.

int a = 20; // 20 is the literal and its type is int
byte b = 20; // here also, 20 is the literal but its type is byte

We rely on the concept of typecasting (both internal and external) to convert the int literal to any other format.

· float:

A float is a data type used to store real numbers in Java. The size of a float is 4 bytes, and the range of values it can allow is approximately ±3.40282347E+38F up to 6-7 significant decimal digits as per the IEEE 754 standard.

float f= 23.4f;

· double:

A double is another data type that allows real numbers with double precision. The size of the double is 8 bytes, and it can store approximately ±1.79769313486231570E+308 up to 15 significant decimal digits.

double d = 1.2;

By default, all decimal numbers end up as double literals. Therefore, we typecast the value for float by adding an addition “f” along with the value.

float f = 1.2f;

· boolean:

This data type stores only boolean values. It means that either it can assume a true or false value.

boolean val = true;

· char:

The char data type facilitates to storage of characters. Its size is 2 bytes, and the values map according to the Unicode characters. The range of values lies between ‘\u0000’ (or 0) to ‘\uffff’.

Learn to Write Your First Java Program.

Non-primitive types:

Non-primitive are user-defined data types. Their purpose is not to store a value. Instead, they refer to a memory location. In programming terms, we call it a Heap, which further allocates space for the primitive data types (involved in the non-primitive operations).

For non-primitive data types, Java keeps the reference, additionally called location, not merely a value.

Alternatively, we refer to them as reference data types. They provide a way to grant space to members of a class or interface. An array is the simplest example of a non-primitive data type.

Data type allocation in Java

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Write Your First Java Program Write Your First Java Program
Next Article Java Variables Explained Variables in Java

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x