05 InputInJava9
05 InputInJava9
5. Input in Java
Introduction
Input in general means giving values to a program. In this lesson two ways of input are discussed:
1. Initialising values: Giving values when program writing: E.g.: int a = 5;
2. Input using methods of an Input Stream class Scanner: E.g.: int a = sc.nextInt();
1. Basic Structure of a Program
[import statement]
class definition
{
method definition
{
statements;
}
}
Note: The [ ] means the import statement is optional, i.e., if it is required use it, otherwise
no need. In the following example it is not used as it is not necessary.
An Example Program: Initializing Values
public class Square
{
public static void main()
{
int n = 5;
int sq=n*n;
System.out.println("Square = "+sq);
}
}
Note: In this program no need of import statement, so it is not written.
Description of the Above Program
The whole program part is class definition. A class definition includes method definition,
and a method includes statements in it.
Syntax of Class Definition
[access specifier] class classname
{
method definition
{
statements;
}
}
Note 1: The [ ] means optional, it is not necessary to write the access specifier. The public
keyword is an access specifier. In simple programs it is not necessary to write public.
Instead of public class Sum we can write class Square
Class Definition
public class Square
It is known as class prototype or class header. A class prototype is first line of a class
definition.
public
The public keyword is used to declare that a class can be executed by all other classes (A
program can contain more than one classes). Keywords are words that are reserved for
special purposes. Keywords should be written in small letters; otherwise, syntax errors
will happen. Keywords should not be used as identifiers such as class names, method
names, variable names, etc.
Raju Xavier | 9446748197 | www.rajuxavier.com 1 Input in Java
ICSE Computer Applications 2 Input In Java
class
The class also is a keyword. The class keyword is reserved to define classes only.
Square
The Square is the name that we have given to the class, i.e., the class name. You can give
any name, but to write a meaningful name is better. Writing the first letter of a class name in
capital letter is a better practice, not a rule. Names given to program parts are known as
identifiers. Class names are identifiers.
{
}
The pair of curly braces are known as punctuators. The { } are essential for a class definition.
The main method and statements inside the braces are known as class body.
Method Definition
public static void main()
It is method prototype or method header. A method prototype is first line of a method definition.
public static void
These are keywords that are essential for the main method definition.
main()
It is the name of the method. Every Java program requires main method. The
puntuator () indicates that it is a method.
{}
The pair of curly braces is essential for a method definition. The statements inside the
braces are known as method body.
Statements
int n = 5;
It is an initialisation statement that means value is given to the program when the program
is written. The value 5 will be stored in the memory cell named n when program compiles.
int
The int means a memory cell named n can store only integers. The int is known as
keyword.
n
The n is a variable name. Variables are identifiers. The names given to various program
parts such as class, method, variables etc. are known as Identifiers.
=
The = is known as assignment operator. This operator stores a value in a variable.
5
The 5 is a value. It is termd as literal, constant, data etc.
;
The ; is a punctuator that is used to separate two statements. So it is also known as
separator.
int sq=n*n;
This is a process statement to calculate square of the input number.
*
The * is multiplication operator.
System.out.println("Square: "+sq);
The System.out.println( ); is a print (output) statement to display output on monitor.
“Square: ”
It is a string value. The sequence of characters in “” (double quotes) is a string value or
string literal. The Square: is printed on the monitor without “”.
+
The + with a string is concatenate operator. The concatenate operator is used to join a
value to the string. Here the value in sq is joined to Square: