data types in java

Java Data Types | Primitive and Non-Primitive Data Types

Data types, as the name implies, define the types of data that can be stored in variables in Java. Java is a language that is statically typed. This means that before any variables can be used, they must first be declared.

int demo;

Here, demo is a variable, and the data type of the variable is int.

The different sizes and values that can be stored in the variable are defined by data types. In Java.

There are two types of data types:

  1. Primitive data types
  2. Non-Primitive data types

Difference between Primitive data types and Non-Primitive data types

Primitive data types Non – Primitive data types
The primitive data types in Java are boolean, char, byte, short, int, long, float, and double. The size of these primitive data types does not change from one operating system to another, so the developers of Java included them to keep the language portable.
1: Whole numbers are stored using the byte, short, int, and long data types.
2: For fractional numbers, float and double are used.
3: Characters are stored in the char variable.
4: Variables that are either true or false are represented by the boolean data type.
Non-primitive data types in Java include classes, objects, arrays, strings, Interface, and etc. In Java, these data types aren’t predefined. Programmers are the ones who make them. A group of values is stored using non-primitive data types.
1: Non-primitive data types are referred to as reference types because they refer to objects.

Primitive Data Types

The size and type of variable values are specified by a primitive data type, which has no additional methods.

In Java, data types are divided into four categories: int, float, character, and boolean. However, there are eight data types in total. The following are the details:

  1. boolean data type
  2. byte data type
  3. char data type
  4. short data type
  5. int data type
  6. long data type
  7. float data type
  8. double data type
Data TypeSizeDescription
byte1 byteWhole numbers from -128 to 127 are stored.
short2 bytesWhole numbers from -32,768 to 32,767 are stored.
int4 bytesWhole numbers from -2,147,483,648 to 2,147,483,647 are stored.
long8 bytesWhole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 are stored.
float4 bytesFractional numbers are stored in this variable. It’s enough to store 6 to 7 decimal digits.
double8 bytesFractional numbers are stored in this variable. It’s enough to store 15 decimal digits.
boolean1 bitTrue or false values are stored in this variable.
char2 bytesA single character/letter or ASCII values are stored.

You can understand the different data types in terms of the memory allotted to them by looking at the diagram below.

primitive data types

byte data Type:

This can hold whole numbers ranging from -128 to 127. Mostly used to save memory and when you are certain that the numbers will fall within the byte data type’s limit.

In large arrays, where memory savings are most needed, the byte data type is used to save memory. Because a byte is four times smaller than an integer, it saves space. 

  • This data type’s default size is 1 byte.
  • 0 is the default value.

Example:

class Main {
  public static void main(String[] args) {

    byte number;
    number = 99;
    System.out.println(number);    
  }
}

Output:

99

If you ran the same program and assigned the value more than 127  to a variable number, you would get a type mismatch error because the value is outside the range of byte data type. As previously stated, the byte range is -128 to 127.

short data Type:

This is larger than a byte but smaller than an integer. Its value ranges from -32,768 to 32767.

  • This data type’s default size is 2 bytes.
  • The short data type, like the byte data type, can be used to save memory.

Example:

class Main {
  public static void main(String[] args) {
    	
    short value;
    value = 500;
    System.out.println(value);  
  }
}

output:

500

Int data Type:

The int data type is a signed two’s complement 32-bit integer. Its value range is – 2,147,483,648 to 2,147,483,647. It has a minimum of – 2,147,483,648 and a maximum of 2,147,483,647. 

  • It has a default value of 0.
  • Unless there is no memory restriction, the int data type is usually used as the default data type for integral values.

Example:

class Main {
  public static void main(String[] args) {
    	
    int value = 7254790;
    System.out.println(value);  
  }
}

output:

7254790

Long data type:

When int is insufficient to hold the value, it is used. It has a wider range than int, ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

  • 8 bytes in size
  • 0 is the default value.
  • The long data type is a 64-bit two’s complement integer.

Example:

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

output:

-19339737826L

double data type:

The double data type is a 64-bit floating-point type with double precision.
It should never be used to represent precise values like money.

  • Default value: 0.0 (0.0d)
  • Size: 8 bytes.
  • Enough to hold 15 decimal digits

Example:

class Main {
  public static void main(String[] args) {
    	
    double value = -49472737.6d;
    System.out.println(value);
  }
}

output:

-49472737.6d

float data type:

The float data type is a 32-bit IEEE 754 floating-point with single precision. If you need to save memory in large arrays of floating-point numbers, use a float (rather than a double).

  • For precise values, such as currency, the float data type should never be used.
  • 0.0F is the default value.

Example:

class Main {
  public static void main(String[] args) {
    	
    float value = -46.5f;
    System.out.println(value);  
  }
}

output:

-46.5f

Boolean data type:

Only two possible values are stored in the Boolean data type: true and false. Simple flags that track true/false conditions are stored in this data type.

  • The Boolean data type specifies a single bit of data, but its “size” cannot be precisely defined.
  • Holds either true or false.
  • false is the default value.

Example:

class Main {
  public static void main(String[] args) {
    	
    boolean flag = true;
    System.out.println(flag);    
  }
}

output:

true

char data type:

  • It’s a Unicode character with a 16-bit value.
  • The char data type has a minimum value of ‘\u0000’ (0) and a maximum value of ‘\uffff’.
  • ‘u0000’ is the default value.

Example:

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

output:

S

String data type:

Java also supports character strings via java.lang.String is a type of class. Strings are not primitive types in Java, instead, they are objects. 

Example:

java data types, java data types, java data types, java data types, java data types, java data types, java data types, primitive data types in Java, primitive data types in java, non-primitive data types in Java, primitive data types in Java, primitive data types in java, non-primitive data types in java, non-primitive data types in java

String developersdome = "Hello World!!";
System.out.println(Developers Dome);

output:

Developers Dome

Non-Primitive Data Types

Non-Primitive data types refer to objects and are thus referred to as reference types. Strings, Arrays, Classes, Interfaces, and other non-primitive types are examples. The image below depicts several non-primitive data types.

Non-Primitive Data Types
  • Strings: A string is a character sequence. In Java, however, a string is an object that represents a series of characters. The java.lang. String class is used to create a string object.
  • Arrays: In Java, arrays are homogeneous data structures that are implemented as objects. Arrays are used to store one or more values of a specific data type and provide indexed access to them. The index of an array element is used to access it.
  • Classes: In Java, a class is a blueprint that contains all of your data. A class contains fields (variables) and methods that describe an object’s behavior.
  • Interface: An interface, like a class, can have methods and variables, but the methods declared in an interface are always abstract.

You might also like:

Java Variables | Java Literals | Java Tutorial

We hope that this article will assist you in understanding all about the Primitive and Non-Primitive Data Types. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, and we will be glad to assist you.

This Post Has 2 Comments

Leave a Reply