Any programming language’s most important foundation in Java Tutorials-Data Types. It is the most crucial concept for any newcomer. The data type is required to represent the type, nature, and set of operations associated with the value it stores. So here we will talk about Java Tutorials-Data Types in Java.
A data type is a way of describing the various values that can be stored in a variable. They are divided into four categories: integer, floats, character, and boolean. For a better understanding of the different data types and the memory allotted to them, see the image below.
Java data types are the most fundamental and first thing you should learn before moving on to other Java concepts. These are extremely helpful in all aspects of Java, whether you’re writing a simple program or developing a complex application or software.
What is Data Type ?
A data type is an attribute of data in computer science that tells the compiler or interpreter how the programmer intends to use the data. A data type limits the possible values for an expression, such as a variable or a function. It specifies how that data type values are saved in memory and what operations can be perform on them. Storage categories such as integer, char, boolean , strings are include in data types.
Data Types in Java
Java is a language that is statically typed. Data types and operators are the foundation of any programming language. Java has a large number of data types and operators, making it suitable for a wide range of programming tasks.
In Java, data types are divide into two categories: 1. Primitive 2. Non-Primitive
You can refer more about this from: https://www.w3schools.com/java/java_data_types.asp
1. Primitve Data Types
The primitive data types are pre defined in the programming language, as the name implies. Primitive types are the most fundamental data types in Java. byte, char, short, int, long, float, double, and boolean are the eight primitive data types in Java.
These data types serve as the foundation for data manipulation in Java.
The constraint on primitive data types is that they can only hold data of the same type and have a fixed size. Intrinsic data types are another name for primitive data types. Primitive data types can be use for operations.
Example : A simple program that displays all data types in Java.
Code:
public class DataTypes {
public static void main(String [] args){
// byte;
// short;
// int;
// float;
// long;
// double;
// char;
// boolean;
byte myByte = 127;
System.out.println(myByte);
int myInt = 22/7 ;
float myFloat = 22/7f;
double myDouble =22/7d;
System.out.println("Int value is : " + myInt);
System.out.println("Float value is : " + myFloat);
System.out.println("Double value is : " + myDouble);
boolean isActive = false;
System.out.println(isActive);
char cChar = 'A';
String myValue = Integer.toHexString(cChar);
System.out.println(myValue);
char municode = '\u0076';
System.out.println(municode);
}
}
Output:
127
Int value is : 3
Float value is : 3.142857
Double value is : 3.142857142857143
false
41
v
Process finished with exit code 0
2. Non-Primitive Data Types
The term “non primitive data type” refers to data types that contain “a variable memory address.” Non-primitive data types, in contrast to primitive data types, instead, they are create by programmers.
They cannot store the value of a variable directly in memory, hence also called as Reference data types. Non-primitive data types store a reference or address (memory location) to the value rather than the value itself.
For Example:
long exNumber = 625487543788;
This tutorial taught you about Java data types and how to classify them into Primitive and Non-Primitive data types. We also talked about how to use these data types in real-world applications and Java programme.
If we miss anything in this blog, please do share your thoughts in comment Section. We will be glad to add your Suggestions!!!
Please Go through this blog page also: https://mechomotive.com/java-tutorials-for-beginners/