Java Data Types – POFTUT

Java Data Types

Java is a completely object-oriented programming language which provides a lot of different type of features. Primitive data types are some of them. Java provides basic or primitive data types like integer, string, logical true and false, etc. In this tutorial, we will learn the Java Primitive Datatypes.

Primitive Data Types

Most of the programming languages provide the primitive data type. Java also provides a primitive data type as a complete and popular programming language. Primitive means basic or non-complex data type simply storing a single type of data. Integers, floating-point numbers, strings, characters are named as primitive because they hold a single type of data. Class, Structs, Objects are referred to as nonprimitive data types.

Primitive Data Types
Primitive Data Types

Composite or Non-Primitive Types

Composite or Non-primitive types are used to store complex data which is generally multiple types in a single type like Class, Struct, Objects, etc. For example, a class which has members like name, age, etc is a non-primitive type. But in some cases, non-primitive types can contain other non-primitive types.

Number Types

Java provides 6 different number types to represent a different type of numbers. As general there is two main number type which is integer and floating-point but there are also derivatives.

Integer Type

An integer is used to store integers and requires 4 bytes in memory even the number do not require it. An integer can be between  -2147483648 to 2147483647 which is generally enough for most of the scenarios and cases. int keyword is used to define the integer type in Java. Here are some examples of Java integer data type.

int myNum = 100000;


Byte Type

If we want to store very little numbers like 50, 7, we can use the byte data type. The byte data type can store numbers between 128 and -127 . Byte data type uses 1 byte in memory. byte keyword is used to define the byte data type.

int myByte= 126; 


Short Type

As integer can store a range of number but in some cases, we may need to store some little numbers like 598, 15290, etc. Short type can store numbers between -32768 to 32767. In order to define the short type short keyword is used. The short data type can be converted into other number types like integer, long, floating-point, double without a problem.

short myNum = 5000;


Long Type

Well, integer type provides a long-range to define numbers. But in some cases, this may not enough for our usage and calculations. Long type can store numbers between -9223372036854775808 to 9223372036854775807. This may be very useful scientific or economy use cases. Long type can be defined with the long keyword. Long type can be converted to integer and short if it is in the range of these types if not we will see some error

long myNum = 15000000000L;


Floating Number Types

Integers are useful but some calculations require some floating-point numbers. There are two types of floating-point numbers in the Java programming language. The only difference is the precision of the types.

LEARN MORE  C String Variable Type

Floating Point Type

Floating point type is the first floating-point type. We can use float in order to define the floating-point type. Floating type store floating numbers between 3.4e−038 to 3.4e+038. We can also use f postfix at the end of the float value in order to specify it is a floating-point number.

float myNum = 5.75f;

float percentage = 10.8;


Double Type

In some cases, the floating-point type may not be enough to store given floating-point number. Double can store numbers between 1.7e−308 to 1.7e+308 . We will use double keyword to define a double data type. A double type variable requires 8-byte memory which is expensive and should be used carefully. We can postfix or end d to the end of the floating data to explicitly specify it is double.

double myNum = 19.99d;


Boolean Type

Java can store logical values false and true. Boolean type is used to store logical values. We will use boolean keyword to define a logical value and true and false keyword to set value.

boolean isPoftutFun = true;

boolean isSaladTasty = false;

System.out.println(isPoftutFun );     // Outputs true

System.out.println(isSaladTasty );   // Outputs false

Character Type

The character type is used to store a single character. char keyword is used to define the character type. Character values generally surrounded by the single quotes but double quotes can be also used. Char variable can be created by providing the character directly like 'A' or with its ASCII value like 65.

char grade = 'A';


char d = 68, e = 69, f = 70;




Non-Primitive Type String

The string is not a primitive type and one of the examples to non-primitive data types. The string data type is used to store multiple characters or a text in a single variable. We can also define a single array of characters. The string variable data can be defined inside double quotes or single quotes.

String greeting = "Hello Poftut";


Leave a Comment