Categories: Data types

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

  • primitive datatype
  • non-primitive datatype

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

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

  • The minimum value is -128
  • The maximum value is 127
  • The default value is 0
  • The byte data type is used to save space in large arrays. Mainly in placed of integer since a byte is four-times smaller than an int.
  • Example: byte a=150,byte b=-60

short

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

  • The minimum value is an -32,768
  • The maximum value is 32767
  • The short data type can also be used to save memory as the byte data type.
  • The default value is 0
  • Example short s=20000. Short r=-25000

Int

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

  • Minimum value is – 2,147,483,648.
  • Maximum value is 2,147,483,647
  • Int is generally used as the default data type for integral values unless there is a concern
    about memory
  • The default value is 0
  • Example: int a = 100000, int b = -200000

long

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

  • Minimum value is – 9,223,372,036,854,775,808
  • Maximum value is – 9,223,372,036,854,775,807
  • So range between (-2^63) to (-2^63-1)
  • This data type is used, When want to wider range of value(like int)
  • The default value is 0L
  • Example: int a = 1000000L, int b = -200000L

Float:

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

  • The float is mainly used to save memory in large arrays of floating point numbers.
  • The default value is 0.0f
  • The float data type is never used for precise values such as currency.
  • Example: float f1 = 234.5f

double:

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

  • This data type is generally used as the default data type for decimal values, generally the
    default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Example: double d1 = 12345.4

Boolean:

Boolean data type represents one bit of information.

  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • The default value is false.
  • Example: Boolean one = true

char:

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

  • Minimum value is ‘\u0000’ or0.
  • Maximum value is ‘\uffff’ or65, 535inclusive.
  • Char data type is used to store any character.
  • Example: char letter=’B’

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.

  • Class objects and various type of array variables come under reference data type.
  • The default value of any reference data type is null.
  • A reference data type can be used to refer to any object of the declared type or any compatible type.
  • Example: Student Sankar = new student(“Vasanth”);

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
  • Global or instance variable
  • Static 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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago