How to use the Import Statement
Submitted by GeePee on Thursday, May 14, 2015 - 23:21.
The following is a simple Java application program showing where in a Java program import statements, the method main, and statements such as named constants, declarations, assignment statements, and input and output statements typically appear. I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
The preceding program works as follows: The statement
and assigns the value of this expression to the variable firstNum, which is 60 in the sample run. The statement
- import java.util.*;
- public class ImportJava
- {
- static final int NUMBER = 12;
- {
- int firstNum;
- int secondNum;
- firstNum = 18;
- secondNum = console.nextInt();
- firstNum = firstNum + NUMBER + 2 * secondNum;
- }
- }
import java.util.*;
import the class Scanner. The statement public class ImportJava
names the class containing statements of the program as ImportJava. The left brace {
marks the beginning of the class ImportJava. The statement static final int NUMBER = 12
declares the named constant NUMBER and sets its value to 12. The statement static Scanner console = new Scanner(System.in)
declares and initializes the object console to input the data from the keyboard.
The statement public static void main(String[] args)
contains the heading of the method main, and the left brace {
marks the beginning of the method main. The statements int firstNum
and int secondNum;
declare he variables firstNum and secondNum.
The statement firstNum = 18;
sets the value of firstNum to 18, and the statement System.out.println("firstNum = " + firstNum);
outputs the value of firstNum. Next, the statement System.out.println("Enter an integer; ");
prompts the user to enter an integer. The statement secondNum = console.nextInt();
reads and stores he integer into the variable secondNum, which is 15 in the sample run. The statement System.out.println();
positions the insertion point on the screen at the beginning of the next line. The statement System.out.println("secondNum = " + secondNum);
outputs the value of secondNum. The statement firstNum = firstNum + NUMBER + 2 * secondNum;
evaluates the expression:
firstNum + NUMBER + 2 * secondNum
System.out.println("The new value of " + "firstNum = " + firstNum);
outputs the new value of firstNum. The right brace }
marks the end of the method main, and the right brace }
marks the end of the class ImportJava.
To execute this program, click run.
Sample run:
firstNum = 18
Enter an integer:
12
secondNum=12
The new value of firstNum = 54
Enter an integer:
12
secondNum=12
The new value of firstNum = 54
Add new comment
- 40 views