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.
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:
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:
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
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
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.
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.
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.
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
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.
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).
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 !
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.
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";
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 :
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 :
Explore JQuery Sample Resumes Download & Edit, Get Noticed by Top Employers!
An interface, like a class, can have variables and methods, but by default, the methods stated in the interface are abstract.
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.
Top 30 frequently asked JAVA Interview Questions !
Want to know more about Java,visit here Java Tutorial !
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 :
Batch starts on 6th Feb 2023, Weekday batch
Batch starts on 10th Feb 2023, Fast Track batch
Batch starts on 14th Feb 2023, Weekday batch
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.