Data types in Java

Before we discuss Java data types, programming languages categorized into two types there are,

  1. Statically typed programming languages
  2. Dynamically typed programming languages
Data 2

Image referred from: https://nosleepjavascript.com/javascript-is-best/

Statically typed programming languages

Statically typed languages require variables must first be declared before they can be used that are checked at compile-time.

Examples of statically typed languages include Java, C, C++, and Swift. In these languages, you must explicitly declare the type of each variable before using it.

For example, in a statically typed language like Java, you would declare a variable of type int as follows:

int number =15;

Here, the number is explicitly declared as an integer type, and the compiler verifies that you perform operations and assignments appropriate for integers.

Dynamically typed programming languages

Dynamically typed languages, on the other hand, do not require explicit type declarations for variables.

The type of a variable is determined at runtime, and it can change during the execution of the program. Examples of dynamically typed languages include Python, JavaScript, Ruby.

For example, in a dynamically typed language like Python, you can assign values without mentioning data type.

number =15;

Data types in java

In Java, there are several built-in data types that you can use to declare variables and store different types of values. In Java data types are categorized into two types.

Data types categorized into two types there are:

  1. Primitive types
  2. Reference types or non-primitive

Primitive data type

Java provides eight primitive data types which are:

Data1

byte:

It represents a signed 8-bit integer value. Range: -128 to 127.

byte number=10;

short:

It represents a signed 16-bit integer value. Range: -32,768 to 32,767.

short number=15;

int:

It represents a signed 32-bit integer value. Range: -2,147,483,648 to 2,147,483,647.

int number=100;

long:

It represents a signed 64-bit integer value. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. You need to append an ‘l’ or ‘L’ otherwise it’s considered an integer.

long number=868143271875123L;

float:

It represents a single-precision 32-bit floating point number. You need to append ‘f’ or ‘F’ otherwise it’s considered double.

float number=86.50F;

double:

It represents a double-precision 64-bit floating point number.

double number=86.50;

boolean:

It represents Boolean value, which can either true or false.

boolean is_available=true;

char:

It represents single Unicode character.

char letter=’A’;

Reference types or non-primitive

In java has reference data types, which are use to store references to object. Reference data type also known as non-primitive data type. Reference data types does not store the actual data. They store references to the actual objects. They include:

String:

It represents sequence of characters. Strings in java are immutable, that mean they cannot be changed once created.

String letter=”vijay”;

Classes and Interfaces:

In java user-defined and pre-defined classes and interfaces are non-primitive data type.

Leave a Reply

Your email address will not be published. Required fields are marked *