Java Data Types

The various values and sizes that are stored in a variable are determined by its data type. Data is categorized into different types by a data type, which informs the interpreter or compiler of the programmer's intended usage of the data. Numerous forms of data, including which can be an integer, real, char or a string, and boolean, are supported by the majority of programming languages. In this article, we will talk about the types of data types, different types of primitive and nonprimitive data types, and the difference between them.

What are Java Data Types

Different values and sizes of data types can be saved in variables that are created for convenience and under specific conditions to account for all test cases. Let's also discuss some additional significant issues. There are primarily two categories of languages, which are as follows:

The first is a language with statically typed variables and expressions, where each type is called compile time. A variable cannot include values of a few other data types once that data type has been declared for it. For instance, C, C++, and Java.

The second is the dynamically typed languages. Over time, these languages may encounter various data kinds. For instance, Python and Ruby.


Because each data type (such as char, int, hexadecimal or packed decimal)being pre-defined as part of the programming language and requires that all constants and variables defined for a given program be described with one of the data types, Java is statically typed and also a strongly typed language.

Java Data types are categorized into 2 types which are:

  • Primitive Data Types : These data types include short, byte, long, int, boolean,  float, double, and char.
  • Non Primitive Data Types : These data types include classes, strings, and arrays.

Primitive Data Types

A primitive data type has no extra methods; it only describes the type and size of variable values. The primitive data types serve as the foundation for data manipulation. Primitive data types are of 8 types:

Type 1: Boolean

The boolean data type size depends on the virtual machine, but it only holds one bit of information, true or false, which represents the 2 main truth logical values in Boolean algebra. Boolean values are neither implicitly nor intentionally (using casts) transformed to any type. However, the programmer can create conversion code with ease.

The size of the boolean is dependent on the virtual machine. The values are boolean only such as false or true. The default value of the boolean is always false.

Let us see a Java program below to understand how boolean works as a data type :

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

boolean a = true;
boolean b = false;
if (b == false){

System.out.println("Welcome to this training");
}
if(a == true){

System.out.println("Welcome to HKR");
}
}
}

Output is:

Welcome to this training

Welcome to HKR

Type 2: Byte

An 8-bit signed two's complement integer is the byte data type. In big arrays, the byte is majorly helpful for saving memory. The size of a byte is 1 byte which is equivalent to  8 bits. The values range from -127 to 128. The default value for byte is 0. 

Let us see a Java program below to understand how byte works as a data type :

class ABC {

public static void main(String args[]) {

byte x = 126;

System.out.println(x);

x++;
System.out.println(x);

x++;
System.out.println(x);
x++;
System.out.println(x);
}
}

The Output is :

126

127-128

-127

Type 3: Short

A 16-bit signed two's complement integer is the short data type. The short is used to conserve memory in sizable arrays when the memory savings are crucial, similar to byte. The size of the short is 2 bytes which is equivalent to 16 bits. The values ranges from -32, 768 to 32 and 767. The default value for byte is 0.

Type 4: int

It is a signed, 32-bit integer with two's complements. The size of int is 4 bytes which is equivalent to 32 bits. The values ranges from -2, 147, 483, 648 to 2, 147, 483, 647. The default value for byte is 0. 

A 32-bit unsigned integer with a value between [0, 232-1] can be represented using the int data type in Java SE 8. For using the int data type as an unsigned integer, use the Integer class.

Type 5: Long

A long has a considerable range. When an int type is insufficient to carry the required value, the long data type—a 64-bit two's complement integer—comes in handy. The size of the long is 8 bytes which is equivalent to 64 bits. The values ranges from {-9, 223, 372, 036, 854, 775, 808} to {9, 223, 372, 036, 854, 775, 807. The default value for long is 0. In order to implement arithmetic operations for unsigned long, the Long class also includes methods like compare Unsigned, divide Unsigned, etc.

Type 6: float

A single-precision 32-bit IEEE 754 floating-point data type is the float. If you need to conserve memory in big arrays of floating-point integers, use a float (instead of a double). The size of a float is 4 bytes which are equivalent to 32 bits. The values range up to 7 digits. The default value for float is 0.0.

Let us see a Java program below to understand how float works as a data type :

import java.io.*;


class ABC{

public static void main(String[] args)

{

float a = 5.87f;

System.out.println(a);

}

}

The output of the Java code is :

5.87

Type 7: Double

A double-precision in 64-bit IEEE 754 floating-point is called the double data type. This data type is typically the default option for decimal values. The size of a double is 8 bytes which are equivalent to 64 bits. The values range up to 16 decimal places. The default value for double is 0.0.

Because approximation errors are allowed in scientific computations, the double and float data types were created specifically for these purposes.

Type 8: char

One 16-bit Unicode character is contained in the char data type. The size of char is 2 bytes which are equivalent to 16 bits. The values range from ‘\u0000’ (0) to ‘\uffff’ (65535).

Why is the size of char only 2 bytes?

Only ASCII characters are used in other programming languages like C/C++, and 8 bits is sufficient for representing all ASCII letters. However, Java employs the Unicode system rather than the ASCII coding system, and as 8 bits is insufficient to represent all characters in the Unicode system, Java uses 2 bytes instead. Most of the world's written languages can be represented by the character set that Unicode defines as being truly international. Numerous character sets, including Latin, Greek, Arabic, Cyrillic, Katakana, and many more, have been combined.

Wish to make a career in the world of Java? Start with HKR'S Java Training !

Java Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Non-Primitive Data Types

Because the reference types won't store the value of variables directly in memory, the reference data types will have a memory location for the variable values. You can find them as strings, objects, arrays, etc.

Type 1: String

The definition of a string is a collection of characters. In Java, a character array is different from a string because a character array is a collection of distinct char-type entities as opposed to a string, which is designed to carry a sequence of chars present in a single variable. Java strings do not end with a null character, in contrast to C/C++ strings.

Below is the syntax for declaring a string :

String str = "HKR Trainings";

Type 2: Class 

A user-defined class serves as a prototype or a template from which objects can be built. It stands for the collection of attributes or operations that are shared by all objects of a particular type. These elements can typically be found in class declarations, in the following order :

  • Modifiers: In this, the class can have default access or be made public. Think of Java's access specifiers for interfaces or classes.
  • Class name: The name ought to start with a letter. If there is a superclass: If there's one, the term extends is followed by the name of the superclass which is the class's parent. Only one parent can be extended (subclassed) by a class.
  • Interfaces(if any): A list, separated by commas, of the interfaces that the class, if any, implements. A class may support several interfaces.
  • Body: Braces, or, enclose the class body

Type 3: Object

It serves as a fundamental building block in Object-Oriented Programming representing actual entities. Many objects are created by a typical Java program, and as you are aware, these objects interact via calling methods. An object includes :

  • State: An object's properties serve as a representation of it. Additionally, it reflects an object's characteristics.
  • Behavior: The methods available with objects serve as a representation of behavior. It also shows how one object reacts to other objects.
  • Identity: It gives an object a special name and makes it possible for that thing to communicate with other objects.

Type 4: Interface

An interface, like a class, can have variables and methods, but by default, the methods stated in the interface are abstract.

  • Interfaces define what a class should do, not how it must do it. It serves as the curriculum's guide.
  • A Player interface, for example, maybe about capabilities, and any class that implements it must be able to move (or must implement moving) (). Consequently, it lists a number of methods that a class must implement.
  • A class must be designated abstract if it implements an interface but won't provide method bodies for all of the functions listed in the interface.
  • Comparator Interface is a good example of a Java library. A class can be used for sorting a collection if it implements this interface.

Type 5: Array

A collection of variables of similar types that go by the same name is known as an array. In Java, arrays operate differently from how they do in C++ or C. Following are some significant Java array points.

  • All arrays are allocated dynamically in Java.
  • Since arrays are considered Java objects, we may use member length to determine their length. In C/C++, we find length using size; this is different.
  • Like other variables, the declaration of a Java variable can be done by adding [] after the data type.
  • The array's variables are indexed starting at 0, and they are all ordered. An array in Java can either be a local variable, a static field, or a method argument.
  • An int integer, not long or short, must be used to provide the size of an array.
  • The object is an array type's direct superclass.
  • Every array type supports the Java.io and Cloneable interfaces.
  • Serializable.

Top 30 frequently asked JAVA Interview Questions !

Subscribe to our youtube channel to get new updates..!

Difference Between Primitive and Non-primitive Data Types

  • In Java, primitive types are already specified or predefined.  However, Java does not specify non-primitive types; instead, they are developed by the programmer (except for String).
  • Primitive types cannot invoke methods to carry out specific actions, whereas non-primitive types can.
  • Unlike non-primitive types, which can be null, primitive types always have a value.
  • While non-primitive types begin with an uppercase letter, primitive types begin with a lowercase letter.
  • While non-primitive types all have the same size, the size of a primitive type varies depending on the data type.

Want to know more about Java,visit here Java Tutorial !

Java Certification Training

Weekday / Weekend Batches

Conclusion

In this article, we have talked about Java data types with their types which are primitive and non-primitive data types. Different values and sizes of data types can be saved in variables that are created for convenience and under specific conditions to account for all test cases known as data types. Primitive data types are such as float, int, long, double, etc whereas non-primitive data types are such as string, arrays, classes, etc. We have also discussed the need and advantages of data types in java.

Related Article :

Find our upcoming Java Certification Training Online Classes

  • Batch starts on 2nd Oct 2022, Weekend batch

  • Batch starts on 6th Oct 2022, Weekday batch

  • Batch starts on 10th Oct 2022, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.

5 main data types in java are int, char, float, double, and long.

The main data types in Java are integer, char, float, and boolean.

They are integer, double, boolean, string, object, etc

Primitive data types like int, double, char, and long cannot be stored in an array list.