Showing posts with label Type Casting. Show all posts
Showing posts with label Type Casting. Show all posts

Jul 5, 2020

Type Casting in JAVA | Java Language | Coding Winds

TYPE CASTING

Suppose you have made a tea and your tea cup gets broken, and then you decide to pour the tea inside a coffee mug and then drink it. Similarly in type casting we transfer or store our data values from one type to another, for example if we decide to store our integer value into float.

So type casting are basically of two types, first where we store the values in bigger sized data types into smaller ones (for ex: double to int), secondly where we store values of smaller sized datatypes into bigger ones (ex: int to short). So the two ways are completely different when we go with implementations so without wasting much time let’s dive right into the example of first type of type casting, i.e., storing values of bigger data values into smaller ones.

Ex:

import java.util.*;

public class HackerRank {

     public static void main(String[] args) {

           Scanner sc = new Scanner(System.in);

           double x= 2.312;

           int y= (int) x;

           System.out.println(y); //Output: 2

     }

     }

Now let’s see the example of the second type of type casting in which we store the values of smaller data types into bigger ones. Unlike the first one this one is simpler

Ex:

import java.util.*;

public class HackerRank {

     public static void main(String[] args) {

           Scanner sc = new Scanner(System.in);

           byte x =2;

           float y = x;

           System.out.println(y); //Output: 2.0

     }

     }


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!