Datatype and variables in Java programming language
- Home
- Data types
- Datatype and variables in Java programming language
- On
- By
- 0 Comment
- 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 variables in Java programming language
There are two types of data in Java:
- Primitive data types
- 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
Variable
Variable is using to allocated memory for assigned value. It provides a name and allocates space in memory
How to declare a variable
How to allocate memory using 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
Boolean
Boolean is a data type in Java which represents only one bit of information, it is either true or false.
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.
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
Data type and variable in Java
Feature of Java programming language
Flavour and versions of Java Programming language