Professional Documents
Culture Documents
• Primitive types
-Directly contains values
• Reference types
-References to objects
Primitive Data Types
Variables
A variable is a container which holds • int a; //declaration
the value. A variable is assigned
with a data type. Variable is a name
• a = 20; //initialization or
of memory location. assignment
• int b=0; //declaration and
Ex: int num=10;
initialization
10
num
Variables
Program to find the sum of two numbers
Ex:
int a;
a = 10;
Bitwise Operators
Command Line Arguments
Passing command line arguments
class Simple {
static public void main(String[] args) {
System.out.println(args[0]);
}
}
Compile : javac Simple.java
Execute : Java Simple ECE ECE
Accessing numeric command line arguments
class Simple {
static public void main(String[] args) {
int i1 = Integer.parseInt(args[0]);
int i2 = Integer.parseInt(args[1]);
System.out.println(i1+i2);
}
} 30
Compile : javac Simple.java
Execute : Java Simple 10 20
Finding length of an Array
class Simple {
static public void main(String[] args) {
int len = args.length;
System.out.println(len);
}
}
5
Compile : javac Simple.java
Execute : Java Simple A B C D E
IO
Java 1.5 introduced the Scanner class which simplifies console
input.
It can also be used to read from files and Strings.
Scanner is available in the java.util package.
Scanner
Class
Scanner Class
The Scanner class parses input from the source into tokens by using
delimiters to identify the token boundaries.
- Example: John,Tim,Adam,Jack
- Token 1: John Token 2: Tim Token 3: Adam
The default set of delimiters consists of the whitespace characters,
space, tab, newline and carriage return.
Scanner will read a line of input from its source.
Syntax:
Scanner <object name>= new Scanner(System.in);
Scanner Class
Scanner Class
QUIZ
Which of the following is used to load a .class file?
A. Class Loader
B. Byte Code Verifier
C. JIT Compiler
D. Interpreter
A
QUIZ
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar) {
int for=2;
System.out.println(for);
}
}
Error
QUIZ
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String [ ] ar) {
int a=2 + 5.0;
System.out.println(a);
}
}
Error
QUIZ
What will be the result if you try to compile and execute the following
code without passing any command line argument?
class Sample {
public static void main(String [ ] args) {
int len = args.length; d
System.out.println(len);
}
}
a. Compilation Error b. Runtime Error
c. The program compiles and executes successfully but prints nothing.
d. The program compiles and executes successfully & prints 0.