Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java
  • Primitive Data Types

Primitive Data Types

What are data types in java?

In Java, data types specify the type of data that a variable can hold. Java is a strongly-typed language, meaning that each variable must be declared with a specific data type. This helps in ensuring that operations on the data are performed correctly. Java has two main categories of data types:

  1. Primitive Data Types
  2. Reference/Object Data Type

1. Primitive Data Types #

Primitive data types are the most basic data types built into the language. There are eight primitive data types in Java:

1. byte

  • Size: 1 byte (8 bits)
  • Range: -128 to 127
  • Usage: Useful for saving memory in large arrays where the memory savings matter.

Example:

byte b = 100;

System.out.println("Byte value: " + b);  // Output: Byte value: 100

2. short

  • Size: 2 bytes (16 bits)
  • Range: -32,768 to 32,767
  • Usage: Can be used to save memory in large arrays when the range of values is known to be limited.

Example:

short s = 1000;

System.out.println("Short value: " + s);  // Output: Short value: 1000

3. int

  • Size: 4 bytes (32 bits)
  • Range: -2^31 to 2^31-1
  • Usage: Default data type for integral values unless there is a concern about memory.

Example:

int i = 100000;

System.out.println("Int value: " + i);  // Output: Int value: 100000

4. long

  • Size: 8 bytes (64 bits)
  • Range: -2^63 to 2^63-1
  • Usage: Used when a wider range than int is needed.

Example:

long l = 100000L;

System.out.println("Long value: " + l);  // Output: Long value: 100000


5. float

  • Size: 4 bytes (32 bits)
  • Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits)
  • Usage: Used when fractional precision is needed. Suffix f or F is mandatory.

Example:

float f = 10.5f;

System.out.println("Float value: " + f);  // Output: Float value: 10.5

6. double

  • Size: 8 bytes (64 bits)
  • Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits)
  • Usage: Default data type for decimal values. Use double for precise calculations.

Example:

double d = 10.5;

System.out.println("Double value: " + d);  // Output: Double value: 10.5

7. boolean

  • Size: 1 bit (Java uses 1 byte internally)
  • Values: true or false
  • Usage: Used for simple flags that track true/false conditions.

Example:

boolean flag = true;

System.out.println("Boolean value: " + flag);  // Output: Boolean value: true

8. char

  • Size: 2 bytes (16 bits)
  • Range: 0 to 65,535 (unsigned)
  • Usage: Used to store any character.

Example:

char c = 'A';

System.out.println("Char value: " + c);  // Output: Char value: A

Summary #

Here is a consolidated example that uses all the primitive data types:

public class Main {

    public static void main(String[] args) {

        // byte example

        byte b = 100;

        System.out.println("Byte value: " + b);  

        // short example

        short s = 1000;

        System.out.println("Short value: " + s); 

        // int example

        int i = 100000;

        System.out.println("Int value: " + i); 

        // long example

        long l = 100000L;

        System.out.println("Long value: " + l);  

        // float example

        float f = 10.5f;

        System.out.println("Float value: " + f); 

        // double example

        double d = 10.5;

        System.out.println("Double value: " + d);  

        // boolean example

        boolean flag = true;

        System.out.println("Boolean value: " + flag);  

        // char example

        char c = 'A';

        System.out.println("Char value: " + c); 

    }

}

Output:

Byte value: 100
Short value: 1000
Int value: 100000
Long value: 100000
Float value: 10.5
Double value: 10.5
Boolean value: true
Char value: A

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Variable Naming conventionNon Primitive Data Types
Table of Contents
  • 1. Primitive Data Types
  • Summary
© Copyright [2018-2025]. All Rights Reserved.