COMP10 - LESSON 10 - Input in Java
COMP10 - LESSON 10 - Input in Java
COMPUTER APPLICATIONS
CLASS – X
Initialization:
Syntax:
<variable>= <expression / constant value>
Example :
int A,B;
A=10;
B=5+A;
Static initialization
The process uses direct assignment of a constant to a declared variable. The
variable is initialized by the programmer in the coding.
Example:
int x;
x=5;
double y;
y=9.4;
Dynamic initialization
When a variable gets initialised at runtime, i.e, during the time of execution of
program logic is termed as dynamic initialization. Under this situation a
variable is assigned with the outcome of any arithmetical operation or function.
Example:
int x=5,y=9,z;
z=x+y*10;
Page 1
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)
Parameters
Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes two int variables called a,b as
parameters. When the method is called, we pass the values, which is used
inside the method to print the sum of two values.
Example
Java Package
Package in java can be categorized in two form, built-in package and user-
defined package.
Page 2
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)
Types of packag
Built-in Packages
These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines
primitive data types, math operations). This package is automatically
imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical
user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
In Java, the import statement is used to bring certain classes or the entire
packages, into visibility. As soon as imported, a class can be referred to directly
by using only its name.
Here, pkg1 is the name of top-level package, and pkg2 is the name of
subordinate package inside outer package separated by dot (.). There is no
practical limit on the depth of a package hierarchy, except that imposed by the
file system. Now, you specify either an explicit classname or a star (*), indicates
that the Java compiler should import the full package.
Page 3
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)
User-defined packages
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being
the package names.
Scanner Class :
Scanner is a class in java.util package used for obtaining the input of the
primitive types like int, double, etc. and strings. It is the easiest way to read
input in a Java program.
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the use
Page 4
COMPUTER APPLICATIONS Class X
TOPIC :- Input in Java (Lesson 10)
Example:
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
Page 5