Entri Blog
No Result
View All Result
Sunday, February 5, 2023
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
FREE GK TEST: SIGNUP NOW
Entri Blog
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
No Result
View All Result
Entri Blog
Free GK Test
banner top article banner top article
Home Articles

Java Data Types: Understanding the 8 Primitive Data Types In Java

by Feeba Mahin
April 15, 2022
in Articles
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • Data Types in Java
  • Primitive Data Types

Classifying data into different data types (or more often, simply ‘types’) is a fundamental preoccupation of programming. Most programming languages categorize data into strict categories. This is particularly true for strongly typed languages (i.e. languages which do not permit data from one type to be used in another type) like Java.

Python and Machine Learning Square

You can learn more about data types in Java in this article.

In this blog , we will learn about the 8 data types in Java and their characteristics.

Data Types in Java

Java is a strongly typed language. Although there are no universally agreed definitions of what ‘strongly typed’ actually means, general meaning holds that a ‘strongly typed’ language does not permit data from one declared variable type to be used in another. The implication, is that you have to explicitly declare what type of data a variable belongs to before declaring it.

You undertake such “strongly typed” declarations in real life all the time. When you say, “John is a man”, you essentially declare that: a) John is male, and b) John is old enough to not be a boy or child. Henceforth, for all practical purposes, John will remain classified as a ‘man’ and cannot be used in other similar categories (like ‘woman’, ‘boy’, etc.).

Grab the opportunity to learn Java with Entri! Click Here

It’s the same case with data types in languages like Java. Once you say that a variable is an integer, it cannot be used to store decimal values or strings.

Data types are divided into two groups:

  • Primitive data types – includes byte, short, int, long, float, double, boolean and char
  • Non-primitive data types – such as Strings, Arrays and Classes

Primitive Data Types

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java:

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

Grab the opportunity to learn Java with Entri! Click Here

Java Primitive Data Types

Any data type built-into a programming language is called ‘primitive’ data type (the word itself betrays its meaning). Built-in data types are the basic building blocks of a programming language. It is often possible to combine them to create composite data types. The basic behavior of a primitive data type itself, however, cannot be modified (it is, after all, something primitive to the language – like the human Id).

Ace your Java skills with Entri! Hurry up !

There are eight primitive data types in Java. These are as follows:

1. Byte: A byte, for those of you who skipped CS 101, is one of the most basic units of memory made up of 8 individual bits. Byte data types in Java have the following characteristics:

  • Minimum Value: -128 (2^7)

  • Maximum Value: 127 (2^7-1)

  • Default Value: 0

Examples:

byte x = 56

byte y = 68

Thus, you can save numbers between -128 and 127 (inclusive) in a byte. Bytes, because of their size, are useful for storing small data in large arrays. Another programmer looking through your code will also instantly recognize that a byte type will hold only a small value, thus improving your code’s readability (a major issue for large applications).

Grab the opportunity to learn Java with Entri! Click Here

2. Short: A short is twice the size of a byte, i.e. it is made up of 16-bits. Its chief characteristics are:

  • Minimum Value: -32,768 (2^15)

  • Maximum Value: 32,767 (2^15-1)

  • Default Value: 0

Examples:

short x = 9870

short y = -635

Like bytes, short types are useful alternatives to int (see below) data types, particularly if your data falls within the specified range. As with byte, using short also improves code readability, besides saving memory.

Grab the opportunity to learn Java with Entri! Click Here

3. Int: An integer is four times the size of a byte (i.e. it is made up of 32 bits). It is one of the most commonly used data types in Java.

  • Minimum Value: -2,147,483,648 (2^31)

  • Maximum Value: 2,147,483,647 (2^31 – 1)

  • Default Value: 0

Examples:

int x = 150000

int y = -2004320

As the most easily understood data type, you will use int a lot in your code.

Ace your Java skills with Entri! Hurry up !

4. Long: A long data type is twice the size of an integer, i.e. it is made up of 64-bits. It’s chief characteristics are:

  • Minimum Value: -9,223,372,036,854,775,808 (2^63)

  • Maximum Value: 9,223,372,036,854,775,807 (2^63 – 1)

  • Default Value: 0

Examples:

long x = 6778005876543

long y = -554233254242

You’ll use long only if you encounter data that doesn’t fit within the int range (which will be rare).

Grab the opportunity to learn Java with Entri! Click Here

5. Float: In programming, any decimal or fractional value is called a ‘float’. If there is a decimal after the number, it will be classified as a float. In Java, a float is made up of 32-bits IEEE floating points*.

The minimum/maximum value of float is not the same as that of the int data type (despite both being made of 32-bits). The full range of float values is beyond the scope of this tutorial. For now, the only thing you need to know is that you’ll use float (and double – see below) for saving decimal values.

Examples:

float x = 2.321

float y =  1.234

*The float value range depends on the IEEE standard classification for floating point numbers.

Ace your Java skills with Entri! Hurry up !

6. Double: Double is a data type that is twice the size of a float. I.e. it is made up of 64-bit IEEE floating points.

As with float, discussing the minimum/maximum value of double data type is beyond the scope of this article. What you should know is that double is a much more precise type than float. For all practical purposes, it is recommended that you use double instead of float for storing decimal values.

Ace your Java skills with Entri! Hurry up !

Examples:

double a = 1.245240

double y = 12.2232

7. Char: Char data type refers to a single 16-bit Unicode character. Unicode is a computer industry standard for representing text related data. This includes alphabets, symbols ($, &, *, #, @, !, etc.), and special figures such as ¢, £, ¥, etc. The Unicode character set includes over 110,000 characters covering more than 100 language scripts.

In other words, any data besides numbers goes into the char data type.

Examples:

char name = ‘John’

char country = ‘USA’

Grab the opportunity to learn Java with Entri! Click Here

8. Boolean: Boolean is the smallest data type in Java, i.e. it is made up of only one bit. Thus, a Boolean data type can have only two values – 0 (or False) and 1 (or True).

Example:

boolean x = true

boolean y = false

(Tip: ‘True’ and ‘False’ written above are not strings. Do not enclose them within quotes as we did with the char example above)

Python and Machine Learning Square

If you found these data types a little confusing, taking a comprehensive Java foundation course would be of great help.

Grab the opportunity to learn Java with Entri! Click Here

Share62SendShare
Feeba Mahin

Feeba Mahin

Related Posts

MPPEB ITI TO Result 2022 Out: Answer Key, Cut Off Marks
Articles

MPPEB ITI TO Result 2022 Out: Answer Key, Cut Off Marks

February 2, 2023
OPSC OMAS Syllabus 2023 Out: Exam Pattern, PDF
Articles

OPSC OMAS Syllabus 2023 Out: Exam Pattern, PDF

February 2, 2023
BMC Gujarat Bharti 2023: Apply Online for 149 Vacancies, Notification PDF
Articles

BMC Gujarat Bharti 2023: Apply Online for 149 Vacancies, Notification PDF

February 2, 2023
Next Post
weekly vocabulary based on the hindu oct 22 2021

Weekly English Vocabulary Based on The Hindu Editorial 2022 April 15

Discussion about this post

Latest Posts

  • Learn Continuous Tense Examples
  • Examples of Adverb in English Sentences
  • Slogans on World Environment Day
  • Top CSS Tools for Web Developer in 2023
  • TNPSC Inspector of Fisheries Admit Card 2023 Out: Download Here

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    149828 shares
    Share 59928 Tweet 37455
  • List of Government Banks in India 2023: All you need to know

    61097 shares
    Share 24439 Tweet 15274
  • TNPSC Group 2 Posts and Salary Details 2022

    39459 shares
    Share 15784 Tweet 9865
  • New Map of India with States and Capitals 2023

    28565 shares
    Share 11426 Tweet 7141
  • Odisha Police Recruitment 2023 PDF Download for 4790 Posts – Eligibility, Selection Process

    863 shares
    Share 345 Tweet 216

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET

© 2021 Entri.app - Privacy Policy | Terms of Service