Datatypes in Java

What is a data type in Java?
A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
There are two types of datatypes in Java :-
Primitive datatypes
Non- Primitives datatypes
Primitive datatypes
Primitives datatypes are those datatypes which are already defined in a programming language . They are the simplest data types available and can represent a single value.
For e.g char , int , float , string
Integer : Integers are whole numbers without any fractional part. They can be either signed (positive, negative, or zero) or unsigned (positive or zero only). In most programming languages, the integer datatype usually takes up 2, 4, or 8 bytes of memory.They are usually represented as int .
Floating-Point Numbers: Floating-point numbers are used to represent real numbers. They can have fractional parts or decimal points. Floating-point datatypes usually take up 4 or 8 bytes of memory.They are usually represented as float .
Characters: Characters represent a single character or symbol. They are usually represented by 'char' datatype and take up 2 bytes of memory.
Boolean: Boolean datatypes are used to represent true or false values. They typically take up 1 byte of memory.They are usually represented as boolean .
Note : True is represented as 1
False is represented as 0
Non-Primitive Datatypes
Non-primitive datatypes are used to store a collection of values. They are not already defined in any programming language but they are defined by user .They include arrays, strings, and objects.
1. Arrays: Arrays are a collection of similar data types, and they allow you to store multiple values under a single variable name. Each element in the array is indexed and can be accessed by using its index.
For declaring arrays :
public class Arrays {
public static void main(String[] args) {
int [] arr= new int(n);
// Here the array is of integer datatype and n is the length of the array .The length of an aray can be 0, 1.....n
//Another way of declaring array if you know the elements present in array
int []arr1= {2, 3, 4, 5, 6};
// Here the length of array is 5
}
}
- Strings: Strings are used to represent a sequence of characters. They are typically implemented as an array of characters, and they are immutable, which means that once created, their value cannot be changed.
For declaring String:
import java.util.* // This is ud=sed to import Scanner class
public class Strings {
public static void main(String[] args) {
Scanner sc= new Scanner (System.in); // Is used to take input from user
String s= sc.next(); // here user will give input as String
}
}
Objects: Objects are complex datatypes that allow you to group related data and functions. They are usually created using class and are instances of that class.
In summary, the datatype determines the type of data that can be stored in a variable and what operations can be performed on that variable. It is essential to understand datatypes in programming for creating efficient, bug-free programs.
Stay tuned for more blogs and posts on Java and other programming language...
About the author
I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .
Comments
Join the discussionNo comments yet. Be the first to comment.




