Chapter 9: Data Types In Java

 Chapter 9: Data Types In Java

NOTE: I'll try uploading 2-3 Chapters everyday. I'm focused on making these chapters more informative and easy to understand with the proper resources and clear concepts and this procedure takes much time so rapid blogs may be delayed. Thanks! and Good Luck 🤠

What are data types?
Well, imagine you are given a box full of coloured balls. Now, you are given the task to sort the balls into separate containers wherein each container has a nameslip on it with the name of the color and you have to put all balls of that colour into the containers respective of the name of the colour written on the container.
    Notice, how you would question yourself to store the balls in their respective containers.
    These containers are what are known as Data Types in programming. Different values are stored in their respective data types.
    There are 2 types of data types: a) Primitive Data Types b) Non-Primitive Data Types]\
    Each variable in Java is stored respective to its data type.
    The syntax to create a variable is:
    <dataType> <variableName> = <value>;
    Example: byte a = 10;
    

1) Primitive Data Types

Primitive data types are the most basic data types which are inbuilt in the java programming language. 
They are:
byte, char, short, int, long, float, double, boolean

a) byte
A byte is the basic-most data type in programming. 
Size: 8 bits (binary-digits). 
Range(storing range): -128 to 127. 
Anything getting stored beyond this limit will throw an error.
Default Value: 0
Syntax: byte a = 10;

b) char
A char (character) 
Size: 2 bytes 
Range: 0-255
Characters in Java are stored in UNICODE which is explained in Chapter 10.
Default Value: \u0000 (blank)
Syntax: char b = 'c'; OR char a = 99;

c) short
As the name suggests, the short data type has a
Size: 2 bytes (same as the character but is used for storing positive or negative integers )
Range: -32768 to 32767.
Default Value: 0
Syntax: short c = -23890;

d) int
An int (integer) is used for storing integers. It has a
Size: 4 bytes
used for storing integers Range: -2³¹ to 2³¹-1
Default Value: 0
Syntax: int c = -238902323;

e) long
As the name suggests, a long stores integers but has a longer size and range. 
Size: 8 bytes. 
Range: -2⁶³ to 2⁶³-1
Default Value: 0L
Syntax: int c = -238902323324523;

f) float
float stores decimal numbers. 
Size: 4 bytes. 
Range: Unlimited
Parse-able Range: 3.4 × 10⁻³⁸ to 3.4 × 10³⁸
Default Value: 0.0f

g) double
double stores decimal number but has a greater size and a greater range. 
Size: 8 bytes 
Range: Unlimited
Parse-able Range: 1.7 × 10⁻³⁰⁸ to 1.7 × 10³⁰⁸
Default Value: 0.0

h) boolean
A boolean stores a conditional output that is either true or false.
Size: 1 byte. 
Range: 0 to 1
Although its size is equal to 8 bits only 1 bit is used to store i.e. either true(1) or false(0)
Default Value: false

2) Non-Primitive Data Types

Non-primitive data types are the user created data types.
For example: String, Array, List, Set, any class etc.
Data Types In Java


❝ Data Types are cool.❞                   

                     ― You 

 

Comments

Post a Comment

Popular posts from this blog

Chapter 10: Divide and Conquer

Chapter 1: Introduction To Java

Resources