Datatypes, Literals, Variables, Type Conversion, Casting and Promotion

Java 12 min min read Updated: Mar 30, 2026 Beginner
Datatypes, Literals, Variables, Type Conversion, Casting and Promotion
Beginner Topic 7 of 25

Datatypes, Literals, Variables, Type Conversion, Casting and Promotion

In Java, every value stored in memory must have a proper type. The compiler needs to know whether a value is an integer, decimal number, character, boolean result, or object reference. This is why understanding data types is one of the most fundamental parts of Java programming.

Along with data types, a Java programmer must also understand literals, variables, type conversion, type casting, and type promotion. These concepts decide how values are stored, how they move from one type to another, and how Java handles expressions during operations.

Key Concept: Data types define the type of value a variable can store, literals represent fixed values, variables hold data in memory, and type conversion/casting/promotion control how Java changes one type into another.

What is a Data Type in Java?

A data type tells the compiler what kind of value a variable can store and how much memory is required for it. It also defines what operations can be performed on that value.

Example:

java int age = 25; double salary = 45000.75; char grade = 'A'; boolean isActive = true;

In the above code:

  • int stores integer values
  • double stores decimal values
  • char stores a single character
  • boolean stores true or false

Types of Data Types in Java

Java data types are mainly divided into two categories:

  • Primitive data types
  • Non-primitive data types

1. Primitive Data Types

Primitive data types are the basic built-in data types provided by Java. They store simple values directly.

Java has eight primitive data types:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

Primitive Data Type Table

Data Type Size Default Value Example
byte 1 byte 0 byte b = 100;
short 2 bytes 0 short s = 2000;
int 4 bytes 0 int x = 50000;
long 8 bytes 0L long l = 900000L;
float 4 bytes 0.0f float f = 10.5f;
double 8 bytes 0.0d double d = 25.75;
char 2 bytes '\u0000' char c = 'A';
boolean JVM dependent false boolean flag = true;

Understanding Each Primitive Data Type

byte

The byte data type is the smallest integer type in Java. It is useful when memory saving matters and the value range is small.

java byte age = 25; System.out.println(age);

short

short is larger than byte but smaller than int. It is less commonly used in daily programming.

java short year = 2025; System.out.println(year);

int

int is the most commonly used integer type in Java for normal whole numbers.

java int salary = 50000; System.out.println(salary);

long

long is used when integer values exceed the range of int. A long literal usually ends with L.

java long population = 1400000000L; System.out.println(population);

float

float stores decimal numbers, but with less precision than double. A float literal must end with f or F.

java float price = 99.99f; System.out.println(price);

double

double is the default type for decimal numbers in Java. It has higher precision than float.

java double pi = 3.14159; System.out.println(pi);

char

char stores a single Unicode character enclosed in single quotes.

java char grade = 'A'; System.out.println(grade);

boolean

boolean stores only two values: true or false.

java boolean isJavaFun = true; System.out.println(isJavaFun);

2. Non-Primitive Data Types

Non-primitive data types are used to store complex values such as objects and collections of characters or multiple values.

Examples:

  • String
  • Arrays
  • Classes
  • Interfaces
  • Enums

Example:

java String name = "Amit"; int[] numbers = {10, 20, 30};

What are Literals in Java?

A literal is a fixed value written directly in the program. In simple words, literals are the actual constant values assigned in code.

Example:

java int age = 25; char grade = 'A'; String city = "Delhi"; boolean active = true;

Here:

  • 25 is an integer literal
  • 'A' is a character literal
  • "Delhi" is a string literal
  • true is a boolean literal

Types of Literals in Java

  • Integer literals
  • Floating-point literals
  • Character literals
  • String literals
  • Boolean literals
  • Null literal

1. Integer Literals

Integer literals are whole numbers without decimal points.

java int a = 100; int b = 25;

Java supports integer literals in multiple forms:

  • decimal → 100
  • binary → 0b1010
  • octal → 012
  • hexadecimal → 0x1A
java int decimal = 100; int binary = 0b1010; int octal = 012; int hex = 0x1A; System.out.println(decimal); System.out.println(binary); System.out.println(octal); System.out.println(hex);

2. Floating-Point Literals

Floating-point literals are numbers with decimal points.

java double d = 10.25; float f = 20.5f;

Without f, a decimal literal is treated as double by default.

3. Character Literals

Character literals are enclosed in single quotes and represent one character.

java char c1 = 'A'; char c2 = '9'; char c3 = '#';

Java also supports escape sequences:

java char newline = '\n'; char tab = '\t'; char quote = '\'';

4. String Literals

String literals are enclosed in double quotes.

java String name = "Java"; String city = "Mumbai";

5. Boolean Literals

Boolean literals can only be:

  • true
  • false
java boolean passed = true; boolean loggedIn = false;

6. Null Literal

The null literal represents the absence of an object reference.

java String data = null;

It can be assigned only to non-primitive types.

What is a Variable in Java?

A variable is a named memory location used to store data. Its value can change during program execution unless declared as final.

Syntax:

java dataType variableName = value;

Example:

java int marks = 90; String studentName = "Amit";

Variable Declaration, Initialization, and Assignment

Declaration

Creating a variable with a type and name.

java int age;

Initialization

Assigning value at the time of declaration.

java int age = 25;

Assignment

Assigning a value later.

java int age; age = 25;

What is Type Conversion?

Type conversion means converting a value from one data type to another. When conversion happens automatically by Java, it is called implicit type conversion or widening.

Implicit Type Conversion (Widening)

When a smaller data type is assigned to a larger data type, Java performs the conversion automatically.

Example flow:

text byte → short → int → long → float → double char → int → long → float → double

Example

java int x = 100; double y = x; System.out.println(x); System.out.println(y);

Output

text 100 100.0

Here, int is automatically converted to double.

More Examples of Widening

java byte b = 10; int i = b; long l = i; float f = l; double d = f; System.out.println(d);

No explicit casting is required because conversion is from smaller type to larger type.

What is Type Casting?

Type casting means converting one data type into another manually. It is usually needed when a larger data type is converted into a smaller data type.

This is called explicit type conversion or narrowing.

Explicit Type Casting (Narrowing)

When a larger type is assigned to a smaller type, Java does not do it automatically because data may be lost.

Example

java double d = 99.99; int x = (int) d; System.out.println(d); System.out.println(x);

Output

text 99.99 99

The decimal part is lost because the value is converted from double to int.

More Examples of Type Casting

Long to Int

java long l = 5000L; int i = (int) l; System.out.println(i);

Int to Byte

java int x = 130; byte b = (byte) x; System.out.println(b);

Output here may surprise beginners because byte range is from -128 to 127. So overflow happens and the stored value changes unexpectedly.

Data Loss During Narrowing

One major reason Java requires explicit casting for narrowing is data loss.

Example:

java int num = 257; byte b = (byte) num; System.out.println(b);

Since byte cannot hold 257 directly, the value wraps around and changes.

Difference Between Type Conversion and Type Casting

Point Type Conversion Type Casting
Nature Automatic Manual
Direction Smaller to larger Larger to smaller
Risk of data loss No or minimal Possible
Syntax No special syntax Uses cast operator
Example int → double double → int

What is Type Promotion in Java?

Type promotion means Java automatically promotes smaller data types into a larger common type during arithmetic expressions and operations.

This is done so calculations can be performed safely.

Rules of Type Promotion

  • byte, short, and char are promoted to int during arithmetic operations
  • If one operand is larger, the result is promoted to the larger type
  • Expression result type depends on the highest promoted operand

Example 1: byte Promoted to int

java byte a = 10; byte b = 20; // byte c = a + b; // Error int c = a + b; System.out.println(c);

Even though both operands are byte, Java promotes them to int before addition. So the result is int, not byte.

Example 2: char Promotion

java char c1 = 'A'; char c2 = 'B'; int result = c1 + c2; System.out.println(result);

Here, both characters are converted to their integer Unicode values, then added.

Example 3: Mixed Type Promotion

java int x = 10; double y = 20.5; double result = x + y; System.out.println(result);

Since double is larger than int, the final result becomes double.

Character and Integer Relationship

In Java, characters are internally stored as Unicode integer values.

java char ch = 'A'; int value = ch; System.out.println(value);

Output

text 65

Since Unicode value of 'A' is 65, Java converts it into that integer value.

Example of Converting int to char

java int value = 66; char ch = (char) value; System.out.println(ch);

Output

text B

Practical Example with Different Types

java public class DataTypeDemo { public static void main(String[] args) { int marks = 90; double percentage = 89.75; char grade = 'A'; boolean passed = true; System.out.println("Marks: " + marks); System.out.println("Percentage: " + percentage); System.out.println("Grade: " + grade); System.out.println("Passed: " + passed); } }

Common Errors and Edge Cases

1. Assigning Decimal to float Without f

java float price = 10.5; // Error

Decimal literals are double by default, so use:

java float price = 10.5f;

2. Assigning Large int to byte

java byte b = 130; // Error

Value 130 is outside the valid range of byte.

3. Expecting byte Result from byte Arithmetic

java byte a = 10; byte b = 20; byte c = a + b; // Error

Because Java promotes the result to int.

4. Loss of Decimal During Casting

java double d = 12.99; int x = (int) d; System.out.println(x);

Output becomes 12, not 13. Java truncates the decimal part.

5. Overflow During Narrowing

java int x = 300; byte b = (byte) x; System.out.println(b);

Result may not be what beginners expect because the value wraps according to byte range.

Important: Widening is generally safe, but narrowing may cause data loss, truncation, or overflow.

Best Practices

  • Use int for normal integer operations unless there is a special need
  • Use double for decimal values in most cases
  • Use float only when memory optimization matters
  • Use meaningful variable names instead of single-letter names in real projects
  • Be careful while narrowing larger types into smaller ones
  • Understand promotion rules before writing arithmetic with byte, short, or char

Interview-Oriented Points

  • Java has eight primitive data types
  • double is the default type for decimal literals
  • byte, short, and char are promoted to int in arithmetic expressions
  • Type conversion is automatic widening
  • Type casting is manual narrowing
  • Narrowing may cause data loss
  • char stores Unicode values internally

Conclusion

Data types, literals, and variables form the foundation of Java programming. Without understanding them clearly, it becomes difficult to write correct programs or understand how Java stores and processes data.

Type conversion, casting, and promotion are equally important because they control how values move between different types and how expressions are evaluated during operations. These concepts appear everywhere in Java, from simple arithmetic to advanced method calls and object interactions.

Quick Summary: Data types define what kind of data can be stored, literals represent fixed values, variables hold data in memory, widening happens automatically, narrowing requires casting, and promotion decides how Java handles mixed-type expressions.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators