• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Primitive Data Types

Contents

  • 1 Integer type primitives:
  • 2 Floating type primitives
  • 3 Character type primitives
  • 4 Boolean type primitive

Java is a strongly typed language, that is, when you declare a variable, you need to specify what type of variable it is. Primitive data types are the fundamental data types provided by Java.

Primarily there are 4 categories of primitive types provided by Java. Those are integers, floating point, character and boolean.

Integer type primitives:

TypeSizeMin ValueMax Value
byte8 bit-128127
short16 bit-3276832767
int32 bit-21474836482147483647
long64 bit-92233720368547758089223372036854775807
Integer type primitives

An integer literal value by default is of type int. A long type literal needs to end with ‘L’.

byte v1 = 127;
short v2 = 32767;
int v3 = 2147483647;
long v4 = 9223372036854775807L;

Floating type primitives

TypeSize
float32 bit
double64 bit
Float type primitives

Floating point type variables can store values that contain fractional portion. By default Java consider a floating point value as double. If you want the value to be stored in float type variable, that value needs to end with ‘f’.

float var1 = 25.5f;
double var2 = 25.5;

Character type primitives

TypeSizeMin ValueMax Value
char16 bit065536
Char type primitives

To store a single character, we can use char type. Java supports Unicode to represent a character, that means, it can represent any character found worldwide.

Character type literals are placed within single quotes.

char c1 = 'A';

You can also store a Unicode character as follow:

char c2 = '\u20B9'; // Indian rupee Unicode representation

You can also store a integer to represent a character as follows:

char c3 = 8377; // Indian rupee numerical representation

Boolean type primitive

TypeSizeSupported Values
boolean1 bittrue / false
Boolean type primitive

When you want to store the result of a condition, we use boolean type. Its values are true and false.

boolean b1 = true;
boolean b2 = false;
boolean b3 = 5 > 2; // since, 5 is greater than 2, b3 will be true
   Variables    

    Operators   

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • Java Tutorial
    • Introduction
    • Install Java
    • First Java Program
    • Statements and Comments
    • Packaging the Classes
    • Variables
    • Primitive Data Types
    • Operators
    • if-else statement
    • Loops in Java
    • Arrays
    • break and continue
    • Switch Statement
    • Classes and Objects
    • Methods
    • Encapsulation
    • Constructor
    • Inheritance
    • Access Modifiers
    • Static Variables and Methods
    • Final keyword in Java
    • Abstract Class
    • What is an Interface in Java?
    • Method Overloading
    • Java – Method Overriding
    • Polymorphism
    • Java – super Keyword
    • Nested Classes
    • Exception Handling
    • String Class

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in