A data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected.
Recap: Start the class with Revising the previous session rules:
There are a lot of quizzes in this session, please take some time to think about the solution on your own before reading further.....
int a = 10;
long b = a;
System.out.print(b); --> 10
long a = 100;
int b = a;
System.out.print(b); Error
long a = 100;
int b = (int)a;
System.out.print(b) --> 100
Q1. What is the output?
int a = 10000;
long b = a;
System.out.print(b);
Explanation: First line we create a variable of type int then we are creating a long type variable "b" and trying to store the value of "a" in it. This is Implicit Typecasting.
Q2. What is the output?
long x = 10000;
System.out.print(x);
Explanation: First line automatic typecasting is happening between Int and Long.
Q3. What is the output?
long x = 10000;
int y = x;
System.out.print(y);
Explanation: First line we create a variable of type long then we are creating a int type variable "y" and trying to store the value of "x" in it. In this acse there is a possiblilty of Data Loss.
Error- Possible lossy conversion from long to int
.
Q4. What is the output?
long x = 1000;
int y = (int)x;
System.out.print(y);
Explanation: Now with this line we are forcing the compiler to typecast it to int. It is explicit Typecasting.
int y = (int)x;
Q5. What is the output?
long a = 10000000000L;
int b = (int)a;
System.out.print(b);
Explanation: Here we are forcing the compiler to store the value 10^10 into int. Because of that overflow will happen. Ans= Some random value.
int b = (int)a;
Tool to take input from the user: Scanner.
Syntax of Scanner:
Scanner scn = new Scanner(System. in);
The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:
Now in order to use scanner also we need to write one line:
import java.util.*;
Consider this like in order to play pubg we need to import some files, some packages similarly to use scanner we need to import java files.
int x = scn.nextInt();
System.out.print(x);
Explanation: Here we are taking the help of scanner by using its name scn and asking the user for an integer value which we will store in "x" varaible.
Just try to give different integer values in the custom input and explain how it got printed.
Take input and print twice the number:
int y = scn.nextInt();
System.out.print(2 * y);
Q1 Predict the output for given input:
Input: 100
scanner sc = new scanner(System.in);
int xyz = sc.nextInt();
System.out.print(xyz);
Explanation: At line 1, scanner is in small letter. Because Java is case sensitive.
Q2 Predict the output for given input:
Input: 594
Scanner sc = new Scanner(system.in);
int abc = sc.nextInt();
System.out.print(abc);
Explanation: Error: S in System should be capital.
Q3 Predict the output for given input:
Input: 5000
Scanner scn = new Scanner(System.in);
int a = scn.nextInt();
System.out.print(a);
Explanation: Here we are creating a variable "a" and taking integer from the user.
Q4 Predict the output for given input:
Input: 24 30
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.print(a);
Explanation: The first value will be stored in first variable and second value will be stored in second variable.
Q5 Predict the output for given input:
Input: 33 11
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
int d = sc.nextInt();
System.out.print(c + d);
Explanation: "c" variable will have value 33, and "d" variablle will have value 11.
Q6 What will be the output for the following input?
Input: 15 21
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.print(a + b + c);
Explanation: "a" variable will have value 15, then variable "b" will have value 21, but for "c" variable user is not giving any input.
Error, No such element exception.
Scanner scn = new Scanner(System .in);
long c = scn.nextLong();
Q1 How to take input for a long variable?
Q2 Predict the output for the following input:
Input: 10000000000
Scanner scn = new Scanner(System.in);
long N = scn.nextLong();
System.out.println(N);
Q3 Predict the output for the following input:
Input: 10000000000L
Scanner scn = new Scanner(System.in):
long N = scn.nextLong();
System.out.println(N);
Explanation: Here, when we give L in the input, then the whole input is not a number anymore.
Error, Input Mismatch. Do not write L in the input section to give a long value.
Q4 3 Predict the output for the following input
Input: 2500
long x = scn.nextInt();
System.out.print(x);
Explanation: First 2500 is considered an integer value, ans we can store an integer value into long. It is implicit typecasting.
Q5 Predict the output for the following input
Input: 2500
int x = scn.nextLong();
System.out.print(x);
Explanation: Now here from long to int, it cannot happen automatically.
Error, possible lossy conversion from long to int.
Q6 Predict the output for the following input
Input: 2500
int x = (int)scn.nextLong();
System.out.print(x);
Declare a variable of any Type Syntax: type name = value;
Q1 Predict the output for the following input
double d = 6.17;
System.out.print(d);
Explanation: We are creating a variable of type double.
Q2 Predict the output for the following input
float x = 3.14;
System.out.print(x);
Explanation:
Error-> Possible lossy conversion from double to float.
Rule : In JAVA, Any decimal number is considered as double
Q3 Predict the output for the following input
float a = 3.14f;
System.out.print(a);
Explanation: Now when we add "f" in front of it, Basically we are trying to tell compiler, consider this as float.
float a = 10.0f;
float b = 3.0f;
float c = (a/b);
System.out.println(c);
double x = 10.0;
double y = 3.0;
double z = x/y;
System.out.println(z);
Output:
3.3333333
3.3333333333333335
Explanation:
Same Rules of int vs long apply here,
double d = 3.14
float f = d // Error
double d = 3.14
float f = (float)d; // doubtle --> Explicilty --> float
System.out.print(f); // 3.14
Q1 Predict the output for the following input
double x = 3.14;
float y = x;
System.out.print(y);
Explanation: Here we are trying to store a double type value into float.
Error- Possible lossy conversion from double to float.
Q2 Predict the output for the following input
double x = 17.67;
float y = (float)x;
System.out.print(y);
Explanation: In this case, we are forcing the compiler to convert double to float. This is known as Explicit Typecasting.
No data loss -> No error
For typecasting just remember 2 rules:
Q1 Predict the output for the following input
double x = 3.45;
int y = x;
System.out.print(y);
Output:
Error- Possible lossy conversion from double to int.
Q2 Predict the output for the following input
double x = 3.45;
int y = (int)x;
System.out.print(y);
Explanation: Here we are forcing the compiler to convert 3.14 to int, We will only get the integer part.
Q3 Predict the output for the following input
int x = 40;
double y = x;
System.out.print(y);
Explanation: In this example, we are trying to store a int type value into double. Double stores decimal values, and here we can easily convert 40 to 40.0, therefore it is called Implicit Typecasting.
How to take input for a float variable?
Scanner scn = new Scanner(System.in);
float a = scn.nextFloat();
How to take input for a double variable?
Scanner scn = new Scanner(System.in);
double a = scn.nextDouble();
Q1 Predict the output for the following input
float x = sc.nextFloat();
System.out.println(x);
Explain we don't need to write "f" while taking inputs for float.
Q2 Predict the output for the following input
Input : 3.14
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
System.out.print(2 * a);
Explanation: Now this 3.14 is stored on variable "a", Then we are trying to print 2a-> 2 3.14.
Q3 Predict the output for the following input
Input : 3.14 20
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.print(a + b);
Explanation: In the first line, we are trying to take an integer type input, But the user is not giving an integer value for the first time.
Error-> Input mismatch.
Q1 Predict the output for the following input
Input : 3.14
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
float b = sc.nextFloat()
System.out.print(2 * a);
Explanation: There are 2 errors, We are only giving one input.
Error- No such element exeception. & semicolon is missing at float b = sc.nextFloat()
Q1 Predict the output for the following input
Input: 3.45
int x = sc.nextDouble();
System.out.println(x);
Explanation: According to rules of typecasting, we cannot do it there is a chance of data loss.
Ans = Error
Correct Code:
Input: 3.45
int x = (int)sc.nextDouble();
System.out.println(x);
Explanation: In this case, we are forcing the compiler to do it, But int can only store integer value, so we will only get the integer part as output.
Ans = 3
Q2 Predict the output for the following input
Input: 3
double y = sc.nextInt();
System.out.println(y);
Explanation: We can easily Typecast from integer to decimal.
Ans = 3.0
System.out.println(4 / 0);
Output: Error
System.out.println(4.0 / 0);
Output: Infinity
System.out.println(4.0f / 0);
Output: infinity
System.out.println(0 / 0);
Output: Error.
System.out.println(0.0 / 0);
Output: NAN[Not A Number].
boolean x = false;
System.out.println(x);
Output: false.
boolean -> true / false only, it will work on True/False, but give answer in lowercase only.
Input: true
Scanner sc = new Scanner(System.in);
boolean y = sc.nextBoolean();
System.out.println(y);
Output: true
We can take inputs like True/False/false also.
+, -, *, / are very basic arithmetic operators. Confirm whether the students know about them. And directly give the below quiz.
Q1 What will be the output?
int a = 10;
int b = 24;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(b/a);
Explanation:
a + b -> 10 + 24 = 34
a - b -> 10 - 24 = -14
a * b -> 10 * 24 = 240
b / a -> 24 / 10 = 2
(Because both are integers, so the result should be an integer.)
One more arithmetic operator: % -> Modulus Operator (Gives remainder of divison of two numbers as output)
12 % 4 = 0
9 % 7 = 2
24 % 5 = 4
Now, give the following quiz.
Q2 What will be the output?
System.out.print(36 % 6);
Q3 What will be the output?
System.out.print(5 % 3);
If necessary, take some more examples.
What are Relational operators?
Ans: Relational operators are used to check the relations between two operands. After comparison, the relational operators return a boolean value.
operand_1 relational_operator operand_2
Relation between a and b | Syntax | a = 45, b = 16 | a = 5, b = 5 |
---|---|---|---|
a is greater than b | a > b | True | False |
a is less than b | a < b | False | False |
a is greater than or equal to b | a >= b | True | True |
a is less than or equal to b | a <= b | False | True |
a is equal to b | a == b | False | True |
a is not equal to b | a != b | True | False |
Note: Explain the difference between assignment operator (=) and equality operator (==).
Thank you so much for reading. If you found it valuable, consider subscribing for more such content every week. If you have any questions or suggestions, please email me your comments or feel free to improve it.
I'm Rahul, a Indian Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about meExplore Other Topics
Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. In this article, we will learn about Java Operators and learn all their types.