You are on page 1of 2

1 Variables and Data Types in Java

In Java, variables are named storage locations used to store and manipulate
data during program execution. They have names, data types, and values.

1.1 Primitive Data Types


Java provides several primitive data types to represent different types of data:

• int: Represents integer numbers (e.g., 1, 100, -42).


• double: Represents floating-point numbers with double precision (e.g.,
3.14, -0.005).
• char: Represents a single character (e.g., ’A’, ’7’, ’

• boolean: Represents a true or false value (e.g., true, false).


• byte: Represents 8-bit integer numbers (limited range).
• short: Represents 16-bit integer numbers (limited range).
• long: Represents 64-bit integer numbers (e.g., 1000000L).

• float: Represents floating-point numbers with single precision (e.g., 3.14f).

1.2 Reference Data Types (Objects)


In addition to primitive data types, Java also supports reference data types,
which are objects:

• String: Represents a sequence of characters.


Example:

String message = "Hello, Java!";

• Arrays: Used to store collections of elements of the same data type.


Example:

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

• Custom Objects: You can create your own custom reference data types
by defining classes and objects.
Example:

1
class Person {
String name;
int age;
}
Person person = new Person();
person.name = "John";
person.age = 30;

Understanding variables and data types is essential when working with Java,
as it forms the basis for storing and manipulating information in your programs.

You might also like