/ Java

Data Types in Java

The data types defines the type of values that a variable can take, for example, if a variable has a float data type, it can only take float values. In Java we have two categories of data type:

1) Primitive data types: The primitive data types include Integer, Character, Boolean, and Floating Point.

2) Non-primitive data types: Classes, Interfaces, Arrays belongs to non-primitive data types.

Java is also one of the statically typed languages. It means the data type of a variable is known at compile time. So, it means you must declare the variable before using it.

int goal;

So in order to use the variable goal in our program, we must declare it first as shown above. It is a good programming practice to declare all the variables (that you are going to use) at the beginning of the program.

Okay, now let us know the datatype of Java.

1) Primitive Data types

There are 8 basic data types in Java that are certainly used to build the program. They are also helpful in data manipulation. They are:

– boolean data type

– char data type

– byte data type

– short data type

– int data type

– long data type

– float data type

– double data type

Here is the table that helps you to know the better about the data types and its default value and default size in the Java.

Data Types

Default Value

Default size

boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

 

  1. Boolean Data Type

This datatype occupies 1 bit of memory. This data type is used to store only two possible values viz. true or false. In Java, ‘true’ and ‘false’ are reserved words to represent logical (boolean) values. They cannot be assigned to another data types nor other data types can be assigned to boolean values. It specifies one bit of information.

Example: boolean result = true;

boolean a=true; is valid

boolean b=’true’; is not valid ✕

boolean c=”false”; is not valid ✕

boolean d=false; is valid

boolean e=1; is not valid ✕

boolean f=34; is not valid ✕

boolean g=10<20; is valid

class JavaBooleanExample {

public static void main(String[] args) {

boolean b = false;

System.out.println(b);

}

}

Output:

false

  1. Char Data Type

Java has Char Data type that takes 2 bytes of memory to support Unicode Characters which differs from C, as it takes only 1 bytes of memory to support ASCII characters. In Java, char data type is single 16-bit Unicode character whose value range lies from

 ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive.There is no negative value is used in the char data type.

The first 256 (numbered from 0 to 255) characters of Unicode are the ASCII set of characters only. So Unicode is compatible with ASCII. So both in ASCII and Unicode ‘A’ is 65, ‘B’ is 66, ‘C’ is 67, ‘ a’ is 97, ‘b’ is 98, ‘0’ is 48, ‘1’ is 49, ‘\n’ is 10, ‘\t’ is 9.

Eg: char a=55; is valid

char b=’A’; is valid

char c=-35; is not valid ✕

Example: char letter D = ‘D’

class JavaCharExample {

public static void main(String[] args) {

char ch = 'D';

System.out.println(ch);

}

}

Output:

D

  1. Byte Data Type

Byte data type used in Java occupies 1 byte of memory and is 8-bit signed two’s complement integer. The value of the byte data type lies between128  (-2^7)  to 127  (2^7-1). Its minimum value is -128 and the maximum value is 127. Its default value is 0. The memory in large arrays can be saved using this data type as it is 4 times smaller than an integer value.

The byte data type is used to save memory in large arrays where the memory savings is most required. The memory space can be saved 4 times than the use of integer data type if you are certain about the range.

We can also use it to use with single characters (ASCII up to 127). It is mostly used when you are sure that numbers would be in the limit specified by byte data type. (for example numbers of goals in a football match.)

Example:

byte a = 9, byte b = -30, byte b=25; is valid, Byte c=225; is not valid ✕

class JavaByteExample {

public static void main(String[] args) {

byte num;

num = 119;

System.out.println(num);

}

}

Output:

119

  1. Short Data Type

In Java, short is data type but in C, short is data modifier.  Its default size is 2 byte. The short is a 16-bit signed two’s complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). The minimum value is -32,768 and a maximum value is 32,767. The default value is 0.

It is used to save memory as it is 2 times smaller than an integer. This is Example:

short num = 1000;

short runs =5000;

class JavaShortExample {

public static void main(String[] args) {

short num;

num = 150;

System.out.println(num);

}

}

Output:

150

  1. Int Data Type

This is the default integer type and most programmers default use this data type even though they can be managed with byte or short. The programmers can use 10 digit number with int type. It occupies 4 bytes of memory to represent value and is a 32-bit signed two’s complement integer. The value range of integer lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1). Its default value is 0.

Example:

int a = 100000;

int b = -200000;

class JavaIntExample{

public static void main(String args[])

{

int rank=56878;

System.out.println("rank);

}

}

Output:

56878

  1. Long Data Type

An integer can store 10 digit values but if we want a value bigger than this range, we can use the long data type which can store up to 19 digit number. It is also a data modifier in C, but a data type in Java. The long value needs to include the letter ‘l” or ‘L’ in its suffix when using a constant bigger than int range.

The long data type takes 8 bytes of memory which value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). It’s default value is 0.

Example:

long a=131009; is fine

long b=123456789012345; is not fine ✕

long c=123456789012345L; is fine

long a = 100000L,

long b = -200000L

class JavaLongExample {

public static void main(String[] args) {

long num = -12332252626L;

System.out.println(num);

}

}

Output:

-12332252626

  1. Float Data Type

The float data type is used to work with the fractional part. It occupies 4 bytes of memory and generally preferable when you want to save memory for a large number of floating arrays.  Its default value is 0.0d. Float data type cannot be used when you need precession like in currency.

The float cannot take decimal values directly but it should be converted to float before assigning. So in regular programming itis not used.

Example:

float a=3.6; is not valid ✕

float b=3.6f; is valid

float c=(float)3.6; is valid

float f1 = 234.5f

class JavaFloatExample {

public static void main(String[] args) {

float num = 19.98f;

System.out.println(num);

}

}

Output:

19.98

  1. Double Data Type

Double is preferred over a float in regular programming because of greater accuracy with a double data type.It is sufficient for holding 15 decimal digits size and occupies 8 bytes of memory. It’s default data type for storing decimal values.

A bigger type (double) value can’t be given to a smaller type (float, int, byte, etc) variable while a smaller type value can be given to bigger type variable.

Example:

float a=5.8; [double to float] compilation error.

int b=5.8; [double to int] compilation error.

short c=5.8; [double to short] compilation error.

double a=3.9; [float to double] valid

double b=834; [ int to double ] valid

double c=’A’; [char to double] valid

Example:

class JavaDoubleExample {

public static void main(String[] args) {

double num = -42937737.9d;

System.out.println(num);

}

}

Output:

-4.29377379E7

Literals in Java

A fixed value that we assign to variables in a program is referred to as Literals.

int goal =19;

Here, value 19 is an integer literal.

char ch = ‘D’

Therefore, the value D is char literal.

Integer Literal

The value assigned to the variables to the data type byte, short, int and long are known as integer literals.

byte b = 100;

short t = 200;

int num = 13313131;

long l = 928389283L;

Float Literals

The value assigned to the variables of the data type float and double are known as Float Literals.

double num1 = 22.4;

float num2 = 22.4f;

Note: To make compiler recognize the float value, always use suffix “f” with float value else compiler will certainly consider it as double.

Char and String Literal

These literals area associated with the char and String type.

char ch = ‘G’;

String str = “Saral Notes”;

Why Most Of The Programmers Prefer “Double” and “Int”?

Generally, In programming when the operation is performed on bytes or shorts, they are upcasted to int data type internally before the actual operations take place. Similarly, ASCII value (integers) are added when we try to add two characters.

Certainly, Java does not allow bigger type data to be assigned to the smaller type. So it is advised to use integer type or even to store small values.

Likewise, when the operation is performed on float type values, they are internally upcasted to double at first, and the actual operation is performed then.

So, the system considers fractional value as double by default. If we try to assign the fraction value to a float, we are likely to get a compilation error. That’s why double is preferable rather than float.

 

For More Details Notes

Download this article

Variables in Java

12 Buzzwords in Java (Features of Java)

Dipendra Laxmi Bahadur Chand Thakuri
CEO @ Vine Software Innovation Company (Pvt.) Ltd. (VSIC)
View More

Comments:

1 comment(s)!
Types of variables in java with example - Saral Notes
[…] int is a datatype that denotes that variable can hold the integer values. There are two types of Datatypes viz. primitive and […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

back to top button
error: Please be respectful of copyright. Unauthorized use is prohibited.