Lesson 12
Expressions and Scanner Class
Expressions in Java are combinations of variables, constants and operators used to perform calculations and evaluations. The Scanner class is used to take input from the user during program execution.
Java Track
Save progress as you learn.
61 lessons
Lesson content
What are Expressions in Java?
An expression is formed by combining variables, constants and operators. Expressions produce a value after evaluation.
Definition List
- Expression: A combination of variables, constants and operators that produces a result.
- Arithmetic Expression: An expression that uses arithmetic operators like +, -, *, / and %.
- Relational Expression: An expression that uses comparison operators such as >, <, >=, <=, == and !=.
- Logical Expression: An expression that uses logical operators like &&, || and !.
Types of Expressions in Java
Java supports different types of expressions based on the operators used.
Arithmetic Expressions in Java
An expression that uses arithmetic operators is called an arithmetic expression.
Examples of Arithmetic Expressions
c = x + y;
a = 10 + 20;
result = x * y + z;Arithmetic expressions are mainly used for mathematical calculations.
Relational Expressions in Java
An expression that uses relational operators is called a relational expression. These expressions return either true or false.
Examples of Relational Expressions
x < 5
x + y > 2
a >= 10Logical Expressions in Java
An expression that uses logical operators is called a logical expression. Logical expressions are used to combine multiple conditions.
Examples of Logical Expressions
x > y && x == 10
x == 10 || y == 5
!(x > 5)Scanner Class in Java
The Scanner class in Java is used to get input from the user. To use the Scanner class, it must be imported into the program.
Importing Scanner Class
import java.util.Scanner;Creating Scanner Object
Scanner in = new Scanner(System.in);Reading Integer Input
int a = in.nextInt();Common Methods of Scanner Class in Java
- next() - Reads a word from the user
- nextBoolean() - Reads a boolean value
- nextByte() - Reads a byte value
- nextDouble() - Reads a double value
- nextFloat() - Reads a float value
- nextInt() - Reads an integer value
- nextLine() - Reads a string value
- nextLong() - Reads a long value
- nextShort() - Reads a short value
Example Program Using Scanner Class
The following Java program displays multiplication tables using the Scanner class.
import java.util.Scanner;
public class Tables {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter The Table : ");
int t = in.nextInt();
System.out.print("Enter The Limit : ");
int n = in.nextInt();
for(int i = 1; i <= n; i++) {
System.out.println(t + " x " + i + " = " + (t * i));
}
}
}Output
Enter The Table : 5
Enter The Limit : 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25Explanation of the Program
Step 1: Import Scanner Class
The program imports the Scanner class to accept user input.
Step 2: Create Scanner Object
A Scanner object named 'in' is created using System.in.
Step 3: Read User Input
The program reads the table number and limit value using nextInt().
Step 4: Generate Multiplication Table
A for loop is used to print the multiplication table up to the specified limit.
Control Structures in Java
Control structures determine the flow of execution in a Java program.
Types of Control Structures
- Sequence Logic - Executes statements one after another in order
- Selection Logic - Selects one statement or block based on conditions
- Iteration Logic - Repeatedly executes a block until a condition becomes false
Sequence Logic in Java
Sequence logic executes statements one after another in order.
Features of Sequence Logic
- Simple execution flow
- Statements execute sequentially
- No branching or looping involved
Selection Logic in Java
Selection logic is also called branching logic. It selects one statement or block of code from multiple options based on conditions.
Examples
- if statement
- if...else statement
- switch statement
Iteration Logic in Java
Iteration logic repeatedly executes a block of code until a condition becomes false.
Examples
- for loop
- while loop
- do...while loop
Advantages of Expressions and Scanner Class in Java
Simplifies Calculations
Expressions help perform mathematical and logical operations easily.
Supports User Interaction
The Scanner class allows programs to accept dynamic user input.
Improves Program Flexibility
Programs become more interactive and reusable.
FAQ
1. What is an expression in Java?
An expression is a combination of variables, constants and operators that produces a value after evaluation.
2. What is the use of the Scanner class in Java?
The Scanner class is used to take input from the user during program execution.
3. What are the three types of control structures in Java?
The three types are sequence logic, selection logic and iteration logic.
Where Java is used
Java in real-world development
Arithmetic Expressions
Expressions using arithmetic operators (+, -, *, /, %) for mathematical calculations.
Relational Expressions
Expressions using comparison operators (>, <, >=, <=, ==, !=) that return true or false.
Logical Expressions
Expressions using logical operators (&&, ||, !) to combine multiple conditions.
Scanner Class
Java utility class used to read user input from the console or standard input stream.
User Interaction
Scanner combined with expressions enables interactive Java programs that respond to user input.
Control Flow
Expressions and control structures work together to manage program execution and decision-making.