What is type casting ? And how to take user input in java ?
While programming in any language we often use many different data types to store or to use different values in our programme as per our requirements in the program
to handle the values easily in the program we use type casting in the program
Type casting is use to assign the value of one primitive data type to another data type is known as type casting
Means hear we are assigning the values of one data type into another data type.
Also while using type casting in our program we can’t assign boolean value into any other datatype.
Type casting is of two types :-
Widenning type casting
Narrowing type casting
In which one is automatically done by compiler which is widening type casting and another is narrowing type casting which is done by manually
Let's understand by a programm
package type casting ;
public class type casting {
public static void main (string [ ] args ) {
byte x=4;
int y=x;
system. out. print (y);
}
}
The output of the above program will be 4, hear we can see that the value of x which is in byte is assigned in int
Manually type casting or narrowing :-
In this type casting we have to store the values besides the series which is to be done manually
e.g. :- double MyDouble = 3.14 ;
int MyInt = ( int ) MyDouble ;
User input in java :-
To take user input in java we have to use scanner class in java which we are importing through a java file
e.g. :- import java. util. scanner ;
scanner sc = new scanner (system .in )
int x = sc. next int ();
Let's understand by writing a program :-
package userInput ;
import java.util.scanner ;
public class scannerUserInput {
public static void main ( string [ ] args ) {
scanner sc = new scanner (system.in) ;
int x = sc. nextInt() ;
system.out.println (x) ;
}
}
There are two types of data types one is primitive and another is non-primitive data type
There are eight types of primitive data type which is as follows :-
int, float, double, long, char, boolean, byte, short.
whereas string is a non-primitive data type which is also formed by using primitive data types. strings are used to store words in a program.
Let’s understand the process of taking user input by writing a program which calculates simple interest
package userInput ;
import java. util. scanner ;
public class scannerUserInput {
public static void main ( string [ ] args ) {
int principal = 5000 ;
float rate = 12.5f ;
int time = 12 ;
float simple interest = principle * rate * time / 100 ;
system .out .println (“ the simple interest is ”)
}
}
The output of the above program will we 7500.0 which is a float value
also in the above program we can take the input from user also
i.e. :- scanner sc = new scanner ( system.in ) ;
int principal = sc. nextInt ( ) ;
float rate = sc. nextfloat ( ) ;
int time = sc. nextInt ( ) ;
I hope the above tutorial will help you to learn how to take users input in java also you can understand by the programs that how do we take users input in our program besides this you have also learnt about the type casting and it’s requirement in the program and how we can implement type casting in our program.
Thankyou
BY :- ADARSH GUPTA
No comments:
Post a Comment