Java Data Types and Variables Explained

☕ Java Data Types and Variables Explained

In Java, variables are containers for storing data values, and each variable has a data type that tells Java what kind of data it holds.


๐Ÿงฉ Java Data Types

Java has two main categories of data types:


1. Primitive Data Types (built-in)

These are the most basic types in Java. There are 8 primitive data types:


Data Type Size Description Example

byte 1 byte Small integer (-128 to 127) byte a = 100;

short 2 bytes Larger than byte (-32K to 32K) short b = 20000;

int 4 bytes Default for integers int c = 100000;

long 8 bytes Very large integers long d = 123456789L;

float 4 bytes Decimal numbers (less precision) float e = 5.75f;

double 8 bytes Decimal numbers (more precision) double f = 19.99;

char 2 bytes Single character char g = 'A';

boolean 1 bit True or false boolean h = true;


2. Non-Primitive (Reference) Data Types

These refer to objects and include:


String


Arrays (int[], String[], etc.)


Classes


Interfaces


java

Copy

Edit

String name = "Alice";

int[] numbers = {1, 2, 3};

๐Ÿ“ฆ Declaring Variables in Java

Syntax:


java

Copy

Edit

dataType variableName = value;

Examples:


java

Copy

Edit

int age = 25;

double price = 19.99;

char grade = 'A';

boolean isJavaFun = true;

You can also declare multiple variables of the same type:


java

Copy

Edit

int x = 1, y = 2, z = 3;

๐ŸŽฏ Variable Types in Java

Type Scope/Usage Example

Local Inside a method void method() { int a = 10; }

Instance Inside a class, but outside methods int age; (used by objects)

Static Shared across all instances (use static) static int count;


✅ Best Practices

Use meaningful variable names: userAge, not x


Choose the smallest data type that fits your needs


Use final if the value shouldn’t change:


java

Copy

Edit

final double PI = 3.14159;

๐Ÿ”š Summary

Java has 8 primitive types and many non-primitive types


Every variable must be declared with a data type


Choose the right type for efficiency and clarity

Learn Full Stack JAVA Course in Hyderabad

Read More

Object-Oriented Programming in Java

Core Java Concepts

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial