Variables and Data Types in Java
In Java, variables and data types are the fundamental concepts used to store and manage data in a program. Every application needs to store different kinds of information, such as names, ages, prices, marks, and other values. Java uses variables to store this information and data types to define what kind of values can be stored.
1. Variables in Java
A variable is a named memory location used to store data in a program. Every variable has a name, a data type, and a value.
Example:
String name = "Abimanyu";
int age = 21;
double salary = 25000.50;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
Here, name, age, and salary are variables. Java allocates memory to store their values based on their data types.
Types of Variables
Java has three main types of variables:
- Local Variable: A variable declared inside a method, constructor, or block. It can only be accessed within its scope and must be initialized before use.
- Instance Variable: A variable declared inside a class but outside methods. Each object gets its own copy of these variables.
-
Static Variable: A variable declared using the
statickeyword. It belongs to the class and is shared among its objects.
2. Data Types in Java
A data type defines what kind of value a variable can store. For example, an integer variable stores whole numbers, whereas a String variable stores text.
Java is a statically typed language, which means we must declare the data type when creating a variable.
Java data types are divided into two main categories:
Primitive Data Types
Primitive data types are the basic built-in data types provided by Java. Java has eight primitive data types.
| Data Type | Size | Example |
|---|---|---|
| byte | 8 bits | byte a = 100; |
| short | 16 bits | short a = 2000; |
| int | 32 bits | int a = 100; |
| long | 64 bits | long a = 10000L; |
| float | 32 bits | float a = 10.5f; |
| double | 64 bits | double a = 20.55; |
| char | 16 bits | char a = 'A'; |
| boolean | JVM-dependent | boolean a = true; |
Some important points:
-
intis commonly used to store integer values. -
longis used for larger integer values. -
floatanddoubleare used for decimal values. -
charstores a single UTF-16 code unit. -
booleanstores eithertrueorfalse.
Non-Primitive Data Types
Non-primitive data types are reference types used to work with objects and more complex data structures.
Some common examples are:
- String: Used to store a sequence of characters.
- Array: Used to store multiple values of the same type.
- Class: A blueprint used to create objects.
Example:
String name = "Abimanyu";
int[] marks = {85, 90, 95};
System.out.println(name);
System.out.println(marks[0]);
Unlike primitive types, reference variables can hold null, which means they do not currently refer to an object.
3. Type Casting in Java
Type casting is the process of converting one data type into another. Java supports two main types of casting.
Widening Casting (Automatic):
Converting a smaller compatible numeric type into a larger type.
int number = 100;
double result = number;
System.out.println(result);
Output: 100.0
Narrowing Casting (Manual):
Converting a larger numeric type into a smaller type. It requires explicit casting and may result in data loss.
double price = 99.99;
int result = (int) price;
System.out.println(result);
Output: 99
Top comments (0)