DEV Community

loading...
Cover image for Variables and Datatypes with type-casting  !

Variables and Datatypes with type-casting !

DELI
Front End Developer , Web Dev. , AI , AR and Blockchain
・3 min read
Hello my dazzling friend's from the internet ,as the topic says we right away dive into term named as
Variable :

when you here variables, what is the first thing that you think of?
=> IN mathematics it is meant as a symbol to store a value in expression or basically an entity that can change value of itself

Similarly in programming We say Variables are designed to store certain data value that can change itself in future.

In java there are different types of variable , When we say different types than we meant what kind of variable we use i.e.
is it a name , a decimal value or , a integer , or a real number.

Hence in JAVA we have String , int , float , char and boolean (we will see ahead in detail about in this blog in the datatypes section)

How to create variable ?

Datatype variableName = variableData ;

=> Above format is the generalized form of creating variable . Now for example if we want to create a integer variable than we will write as

int var1 = 13 ;
Enter fullscreen mode Exit fullscreen mode

in programming always find the variable name as suitable or convenient to the program details .

Note :

Final keyword is applied before the variable when created or declared hence it mean's that when a final keyword is added the value once initialized will never be overwritten to it's previous value .
for example

final float PI = 3.14f ;
Enter fullscreen mode Exit fullscreen mode
How to display the variables ???

We use here the Println() method , let me show how it is done if we consider the above written final keyword example .

  final float PI = 3.14f ;
System.out.println("The value of PI is " + PI);
Enter fullscreen mode Exit fullscreen mode

This is how we can create as many variables as we want in the java program and display them .

As we have seen how to create and dispaly a variable let us also see What are

DataTypes :

There are two types of Datatypes :
Primitive and Non-Primitive Datatypes

primitive consist of Integer,byte,short,long, float,double, boolean and char
and Non primitive consist of Strings Arrays and Classes .

Primitive DataTypes

These datatypes have fixed size format according to their data -types and it has no other additional methods .

byte 1 byte

short 2 bytes

int 4 bytes

long 8 bytes

float 4 bytes

double 8 bytes

boolean 1 bit (true or false)

char 2 bytes

=> Note whenever use float or double numbers we always initialise by writing 'f' after data value in case of double "d"
for example :

 float PI = 3.14f ;
Enter fullscreen mode Exit fullscreen mode

We will see Non primitive datatypes in different blog as we will see string's in detail .

Type-casting

There are two ways to type-cast in java one way is the Automatic way and second one is a manual one

when we go in order of
byte => short => char => int => long => float => double

it type-cast automatically ;

but when we go in order
double => float => long => int => char => short => byte
i.e.Larger to Smaller we need to do manually .

Now lets see a question on this concepts of java .

2) let us see how to typecast manually a double into integer value having double variable value as 13.3245;

package javablogs;

public class Typecast {

   public static void main(String[] args) {
        // typecast double to integer having double variable value a 13.3245 

        // write your code from here 
    double var1 = 13.3245d;
    int var2  = (int) var1 ;

    System.out.println("The value stored in var1 is double it is :" + var1) ;
    System.out.println("The value stored in var2 is integer it is :" + var2) ;


   }
}
Enter fullscreen mode Exit fullscreen mode

the result is displayed here :

The value stored in var1 is double it is : 13.3245
The value stored in var2 is integer it is : 13


for you self assessment try solving a question which have a typecasting from float having variable value '3.14567f' to double and let me know if you have any queries .

lets conclude :

we have seen what is java datatypes (primitive) and also seen java variables how to create them and how to display them as well Hope you find it amazing learning with me ?:)
Next blog soon !

Happy learning folks !

Discussion (0)