Codeforcoding

Datatype and variables in Java programming language

Datatype and variables in Java programming language

Datatype and variables in Java

In this tutorial, we will discuss Datatype and variables in Java programming language

Data type and variables in Java, is an important concept in Java. Data types represent the different values to be stored in the variable and there are two types of data types In Java.

Datatype and variables in Java

Two type of data type exist in Java

The data type is a keyword or reserved word in Java used to allocate memory to variables. It is pre-defined as part of the programming languages.

Datatype and variable in Java programming language
Datatype and variable in Java

Datatype and variables in Java programming language

There are two types of data  in Java:

  1. Primitive data types
  2. Reference data types

Primitive Data Types:

There are eight primitive data types available in Java.

The primitive data type is already predefined and has been allocated with a keyword(known as the reserved word). Let’s look into the eight primitive data types in detail.

byte

The byte data type is an 8-bit signed two’s complement integer

short

The short data type is a 16 bit signed two’s complement integer.

Int

Int data type is a 32-bit signed two’s complement integer.

long

The long data type is a 64-bit signed two’s complement integer.

Float:

Float data type is a single-precision 32-bit IEEE 754 floating point.

double:

the double data type is a double-precision 64-bit IEEE 754 floating point.

Boolean:

Boolean data type represents one bit of information.

char:

the char data type is a single 16-bit Unicode character.

Reference Data Types:

Reference data are also known as Object data type is created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Students, Sun etc.

Data type and the default value

[wp_table id=13588/]

Variable

Variable is using to allocated memory for assigned value. It provides a name and allocates space in memory

How to declare a variable

Declare variable

 

How to allocate memory using variable

Allocate memory for the variable

Syntax

Data_type variable_name=value;

Example

int marks=67;

Types of variable

Local variable

The local variable is a type of variable which is declared inside the method as the local variable.

Global or instance variable

The instance variable is a type of variable which is declared inside the class but outside the methods or constructor.

It doesn’t declare as static.

static variable

A variable that is declared as static is known as the static variable.

Static variable is not a local variable.

Examples for variables

variable in Java – Example

 

Boolean

Boolean is a data type in Java which represents only one bit of information, it is either true or false.

Example – boolean

Output

true

false

 

Byte

The byte data type is an 8-bit signed two’s complement integer.

Output

a value is 34

b value is 126

short

The short data type is a 16-bit signed two’s complement integer looks like to byte.

Example short data type

Output

a value is 342

b value is 1263

 

int

It is a 32-bit signed two’s complement integer.

Output

a value is 1000

b value is 4567

long

The long data type is a 64-bit two’s complement integer.

Output

a value is 34265

b value is 1263345

 

float

The float data type is a single-precision 32-bit IEEE 754 floating point.

Output

a value is 34.35

b value is 12.5

 

double:

The double data type is a double-precision 64-bit IEEE 754 floating point.

Output

a value is 343.35

b value is 142.556

 

char

The char data type is a single 16-bit Unicode character. A char is a single character.

Output

a  is Z

b  is Y

 

Suggested for you

Keywords in Java language

Data types in Python

Data type in C language

Data type in C++ language

Hollow world program in Java

Data type and variable in Java

History of Java

Download and  install  Java

How to install Java

Feature of Java programming language

Flavour and versions of Java Programming language

 

Single dimensional Array in Java language
Exit mobile version