Data types

description of image


Introduction


In the last lesson, we learned to create variables.

For example:

int age;

● In Java, "int" is a primitive data type that is used to represent whole numbers. It is a 32-bit signed two's complement integer, which means it can represent values between -2,147,483,648 and 2,147,483,647.



● Integers are a fundamental data type in Java, and they are used in a wide variety of applications.



Variable Type and Size:


Data types determine the type and the size of variables. The int variables

● can only store integers (type)

● are typically 4 bytes (size)

In Java, we cannot create variables without a data type.



Data Type: int


● The int variables can only store integers.

● The size of an int variable is usually 4 bytes (32 bits).

● This means it can take 2^32 distinct integer values from -2147483648 to 2147473647.

Example:

Int age, userId = 45;

Here, age and userId are int type variables. And, we have assigned 45 to the userId variable.



Data Type: float and double


● Both float and double variables can only store floating-point numbers (decimal numbers).

● The size of a double variable is 8 bytes. And, the size of a float variable is 4 bytes.

● This means double variables can store real numbers with better precision.

Example:

Double firstNumber = 64.33;

Float secondNumber = 64.33f;

Here, both variables are storing 64.33 , However for float types we use f or F at the end.



Data Type: float and double


● Both float and double variables can only store floating-point numbers (decimal numbers).

● The size of a double variable is 8 bytes. And, the size of a float variable is 4 bytes.

● This means double variables can store real numbers with better precision.

Example:

double firstNumber = 64.33;

float secondNumber = 64.33f;

Here, both variables are storing 64.33 , However for float types we use f or F at the end.



Data Type: boolean


The boolean variables can only store two values: true or false.

Example:

Boolean result1 = true;
Boolean result2 = false;

Booleans are used in decision making and loops which we will learn in upcoming lessons.



Quiz


Question Text