Professional Documents
Culture Documents
class CommandLine1{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}
String args[]
saurabh jain 1234 @@@@
0 1 2 3 4 n-1
}
}
args
23.5 44.6
0 1
All the primitive data types are wrapped in the wrapper classes and are wrapped as an object of it.
1 byte 1 Byte
2 short 2 Short
3 int 4 Int
4 long 8 Long
5 float 4 Float
6 double 8 Double
7 char 2 Char
Accepting input from the user in an interactive manner. A separate Scanner class is defined in
‘java.util.Scanner’ package which defines various methods for taking different data values from
user. Some of the methods of Scanner class are as follows:
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.
To create an object of Scanner class, we usually pass the predefined object System.in, which
represents the standard input stream. We may pass an object of class File if we want to read input
from a file.
To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For
example, to read a value of type short, we can use nextShort()
To read a single character, we use next().charAt(0). next() function returns the next token/word in
the input as a string and charAt(0) function returns the first character in that string.
The Scanner class reads an entire line and divides the line into tokens. Tokens are small elements
that have some meaning to the Java compiler.
For example, Suppose there is an input string: How are you
In this case, the Scanner object will read the entire line and divides the string into tokens: “How”,
“are” and “you”. The object then iterates over each token and reads each token using its different
methods.
import java.util.*;
class UsingScanner
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int a,b;
System.out.println("Enter first number");
a=s.nextInt();
System.out.println("Enter Second number");
b=s.nextInt();
int c=a+b;
System.out.println("Addition is:"+c);
}
}
import java.util.*;
class Largest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Three Integer values:");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
if(a>b && a>c)
{
// String input
String name = sc.nextLine();
// Character input
char gender = sc.next().charAt(0);
Output :
Name: Geek
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9