In this lesson we will discuss about precedence of operators, after that we will see how to read inputs from user and then a small example to show how to operators based on requirements.
Operator Precedence
When we have multiple operations in single expression, then result of that expression depends on weightage of operators in that exression. This is called operator precedence. For example consider an expression 10 + 4 * 3, result might be 42 or 22. Correct answer is 22, because multiplication has higher precedence than addition, hence first 4 * 3 happens then addition operation executes.
Below table shows precedence of operators in decreasing order.
Type | Operator | Associativity |
---|---|---|
Postfix | () [] . (dot operator) | Left to right |
Unary | ++ – – ! ~ | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Shift | >> << | Left to right |
Relational | > >= < <= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Till now all our programs just execute and prints output. They do not accept any input from user. But majority of software applications receive input from user. In this lesson we are going to learn about simple way of taking user input.
Java provides a rich set of libraries of I/O operations. Now we will look at simple library Scanner. This library is present in java.util library. To use scanner class we need to import library into our program. This is achieved by keyword import.
import java.util.Scanner;
Next step we need to do is to create an object or instance of Scanner class. While creating object we need to specify from where it has read inputs that is whether from standard input (console window) or files etc. In our case standard input is sufficient, to do this we need to send System.in has to pass in constructor.
Scanner input = new Scanner(System.in);
To read different types of data Scanner class provides different methods such to read string use input.nextLine() and to read integer input.nextInt() etc. And the return value we can store in a variable.
String name; name = input.nextLine();
When the above line executing, program waits for user input. On entering input it stores that data into name variable.
Below program demonstrates this method.
/* This is a simple Java program about User Input. Call this file KH_UserInput.java. */ import java.util.Scanner; public class KH_UserInput { // A Java program begins with a call to main(). public static void main(String args[]) { String name; int age; Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); name = input.nextLine(); System.out.print("Enter your age: "); age = input.nextInt(); System.out.println("You name is " + name + " and you are " + age + " old."); } }
Download the code
Run the code
Enter your name: Khan Enter your age: 30 You name is Khan and you are 30 old.