Java

What Data Types Does Java Have?

A variable’s data type determines the possible values for it; for instance, if a variable has the data type int, it can only accept integer values. There are two types of data in Java: 1) Simple data structures 2) Non-primitive data types – Arrays and Strings are examples of non-primitive data types; we’ll talk about them in more detail in the next courses. Here, we’ll talk about Java’s primitive data types and literals.

Java is a language with static typing. If the data type of a variable is known at compile time, a language is said to be statically typed. This implies that before using a variable, you must declare its type (declare the variable).
We learnt how to declare a variable in the last tutorial on Java variables. Let’s review:

int num;

Therefore, we must first define the variable num as indicated above in order to utilize it in our application. Declaring all of the variables you plan to use at the start of the program is a smart programming practice.

1) Primitive data types

Eight primitive data types are available in Java: boolean, char, byte, short, int, long, float, and double. Since the size of these primitive data types is constant across operating systems, Java developers added them to preserve Java’s portability.

Whole numbers are stored using the data types byte, short, int, and long.

Fractional numbers are expressed using float and double.

Characters are stored in char (letters).

For variables that can only carry true or false values, boolean data types are employed.

byte

This can store entire numbers in the range of -127 to 127. When you are assured that the values will be inside the range allowed by the byte data type and want to save memory, you should utilize this technique.
This data type’s default size is 1 byte.
Default setting: 0
Example:

class JavaExample {
    public static void main(String[] args) {
    	
    	byte num;
    	
    	num = 113;
    	System.out.println(num);
    }
}

Output:

113

short

This is bigger than a byte but smaller than an integer. It falls between -32,768 and 32767.
This data type’s default size is 2 bytes.

short num = 45678;

int

Its range is broader and is used when short cannot contain the number: -2,147,483,648 to 2,147,483,647
Standard size: 4 bytes
Default setting: 0
Example:

class JavaExample {
    public static void main(String[] args) {
    	
    	short num;
    	
    	num = 150;
    	System.out.println(num);
    }
}

Output:

150

A short data type, which has a greater range, can contain the number 150 but the byte data type cannot.

long

It has a greater range than int data type, spanning from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is used when int is not large enough to contain the value.
length: 8 bytes
default value: 0

Example:

class JavaExample {
    public static void main(String[] args) {
    	
    	long num = -12332252626L;
    	System.out.println(num);
    }
}

Output:

-12332252626

double

Enough to carry 15 decimal digits.

Size: 8 bytes,
Example:

class JavaExample {
    public static void main(String[] args) {
    	
    	double num = -42937737.9d;
    	System.out.println(num);
    }
}

Output:

-4.29377379E7

float

Size: 4 bytes, enough to carry 6 to 7 decimal digits.

class JavaExample {
    public static void main(String[] args) {
    	
    	float num = 19.98f;
    	System.out.println(num);
    }
}

Output:

19.98

boolean

Either true or false is true.

class JavaExample {
    public static void main(String[] args) {
    	
    	boolean b = false;
    	System.out.println(b);
    }
}

Output:

false

char

It keeps character data.
length: 2 bytes

class JavaExample {
    public static void main(String[] args) {
    	
    	char ch = 'Z';
    	System.out.println(ch);
    }
}

Output:

Z

Literals in Java

In a program, a literal is a fixed value that is assigned to a variable.

int num=10;

Here, the number 10 is a literal Integer.

char ch = 'A';

Here, A is a character literal.

Integer Literal

The variables of data types byte, short, int, and long are given integer literals.

byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;

Float Literals

used with float and double data types.

double num1 = 22.4;
float num2 = 22.4f;

Note: Always append float values with a “f”; otherwise, the compiler will treat them as doubles.

Char and String Literal

For use with the char and String types.

char ch = 'Z';
String str = "BeginnersBook";

Related Articles

Leave a Reply

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

Back to top button