Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

In Java, data types define the type of data that a variable can hold.

Java has two categories of data


types: primitive data types and reference data types.

Primitive Data Types:

1. Integral Types:

 byte: 8-bit signed integer.

 short: 16-bit signed integer.

 int: 32-bit signed integer.

 long: 64-bit signed integer.

Example:

javaCopy code

int myInt = 42;

2. Floating-Point Types:

 float: 32-bit floating-point number.

 double: 64-bit floating-point number.

Example:

javaCopy code

double myDouble = 3.14;

3. Character Type:

 char: 16-bit Unicode character.

Example:

javaCopy code

char myChar = 'A';

4. Boolean Type:

 boolean: Represents true or false.

Example:

javaCopy code

boolean myBool = true;

Reference Data Types:


1. Class Types:

 Instances of classes created by the user.

Example:

javaCopy code

String myString = "Hello, Java!";

2. Array Types:

 An ordered collection of elements of the same type.

Example:

javaCopy code

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

3. Interface Types:

 Similar to class types but used to define interfaces.

Example:

javaCopy code

interface MyInterface { // interface definition }

4. Enumeration Types:

 A special data type that consists of a set of named constants.

Example:

javaCopy code

enum Days {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};

5. Other Reference Types:

 Other types include Object (the root class for all Java classes) and various special types
for handling specific scenarios.

These data types are used to declare variables, and their usage depends on the requirements of the
program. Java is a statically-typed language, meaning that the data type of a variable must be explicitly
declared before it is used. Additionally, Java provides automatic type checking, helping to catch
potential errors during compilation.

You might also like