Learning Java Step By Step

This blog would cover concepts of type casting including its types, working and would end up discussing about user Input and various ways to take user input for different data types.

Originally published in en
Reactions 0
849
DT
DT 04 Nov, 2019 | 3 mins read


Chap 02

·       Type Casting


Type casting is a process of assigning value of one primitive data type to another data type.

Type casting is of two types:


1.    Automatic Type Casting : In this type casting the lower data type is automatically converted into higher data types . There is no need to add external keyword in this type conversion. This type of casting is also called implicit conversion . This is even called Whitening process.


NOTE:

BYTE CAN BE CONVERTED INTO SHORT , SHORT CAN BE CONVERTED INTO CHAR , CHAR CAN BE CONVERTED INTO INT , INT CAN BE CONVERTED INTO LONG , LONG CAN BE CONVERTED INTO FLOAT AND FLOAT CAN BE CONVERTED INTO DOUBLE.


They follow a conversion hierarchy in which lower data type can easily be converted into higher data type.


2.    Manual Type Casting: In this type of type casting we explicitly store the value from higher data type to lower data type . This type casting is also called Narrowing.


For Eg :

double myDouble = 2.233 ; // Declaring a value in double datatype

int myInt = (int)myDouble; //Converting data type into int using int keyword

System.out.println(myDouble); //Printing double

System.out.println(myInt); //Printing Int Value

This type of conversion is called Explicit Conversion. In this typecasting is externally done using a keyword.


EXCEPTION IN TYPE CASTING: BOOLEAN VALUE CANNOT BE TYPECASTED INTO ANY DATA TYPE.

 

·       WHAT ARE COMMENTS ?

Comments are that portion of program which are ignored by the compiler.


·       How to Take Input from User ?

Scanner is used to take input from user in Java . Scanner class is found inside a java util folder in java.

FOR EG :

import java.util.Scanner;

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();


What we learned in this code?


1.    Import keyword is used to import any folder from java. Scanner class is in util folder of java.

2.    The syntax of scanner is second line of example. This command makes a object in Scanner Class which is used to take the input in the next line of code.

3.    sc command contain many function which could be used .

4.    sc.nextInt() wait for us to give input and print the value.

5.    Syntax to input integer value.


VARIOUS SCANNER FUNCTION TO INPUT VALUES FOR DIFFERENT DATA TYPES.


1.    INTEGER : sc.nextInt()

2.    LIST OF STRING : sc.nextline()

3.    DOUBLE: sc.nextDouble()

4.    FLOAT: sc.nextFloat()

5.    LONG: sc.nextlong()

6.    Single String: sc.next()

7.    Boolean : sc.nextBoolean()

8.    SHORT : sc.nextShort()

 

PROGRAM THAT YOU SHOULD PRACTICE AND WOULD BE DISCCUSSED IN NEXT BLOG.


·       WRITE A PROGRAM TO CALCULATE SIMPLE INTREST TAKING VALID INPUT FROM USER


·       WRITE A PROGRAM TO CALCULATE THE SUM OF TWO NUMBERS TAKING INPUT FROM USER


·       WRITE A PROGRAM TO TAKE A USER INPUT OF 5 NUMBERS AND PRINT AVERAGE OF IT IN ITS DECIMAL FORM. 


Various Coding Platforms to begin competitive coding.


1. HackerRank

2. HackerEarth

3. CodeShef


These are the basic platform for comparative coding for beginner. I would personally recommend hackerrank to newbies.


In the next blog we would discuss following topics :


Operators and Types of Operators.


Till then, I would recommend you to know what are Operators so that you all can connect well with it when we would discuss it.


Thank you for Reading this blog.

Keep reading.

Keep supporting.



0 likes

Published By

DT

DT

Comments

Appreciate the author by telling what you feel about the post 💓

Please Login or Create a free account to comment.