Professional Documents
Culture Documents
JAVAnotes
JAVAnotes
in - input
out - output
err - error
They can be accessed through the class System. But their source or destination won't be
determined until run time.
To keep it simple,
System.in - Standard input stream, assume it is connected to keyboard. Any read() operation
over System.in will fetch you data from keyboard.
System.out - Standard output stream, assume it is connected to output, say console. Any data
printed using this would be printed over the console/terminal.
System.err - Standard error stream, assume it is also connected to output, say console. Any data
sent to this stream is printed over the console/terminal.
Example -
Scanner -
A simple utility to parse the given stream and read the values. In the below example we are using
it to read the data from keyboard.
An example program to read the data from the keyboard and prints it.
Here import specifies that you are using the class Scanner from
the package java.util.
import java.util.Scanner;
Note: Java for public classes the class name and file name should be same.
Relational Operators -
> Greater than
!= Not equal to
These operators return a Boolean value i.e. true if the given condition is met otherwise false.
Logical Operators
|| - Logical OR
Returns true if either one of the expression is true.
! - Logical NOT
Return true if expression is false and returns false if expression is true.
&& doesn't evaluate the second expression if first expression evaluates to false.
Value of b remains 20 because b++ is not evaluated, why? in a++ > 10, a++ means post
increment i.e. a is incremented after substiution i.e. after substituting the current value of a i.e.
10, a is then incremented to 11. it makes it 10 > 10 which is false. Since the first expression is
false, b++ > 20 is not evaluated at all and the result false is assigned to c.
i.e. a is 11
b is 20
c is false.
Bitwise operators -
They operate on the individual bits of the given number.
& Bitwise AND
Corresponding bit will be 1 if bits are opposite i.e. 0 ^ 1 is 1 similarly 1 ^ 0 is 1. For equal bits it
is 0 i.e. 0 ^ 0 is 0 and 1 ^ 1 is 0
~ Bitwise Compliment
In case of compliment we need to know that a positive numbers becomes negative after
compliment and vice versa.
Shift operators -
<< Left shift
Shifts the bits to the left. Note that the left most bits get dropped.
>> Signed Right shift
Shifts the bits to the right. Right most bits gets dropped and the vacant bits to the left are filled
with previous sign bit value.
In the expression
a = 10;
Value 10 is assigned to the variable `a`. We can also use assignment operator in combination
with arithmetic operators
a += 5;
b = c = a;
c = a is evaluated first and then its result i.e. the assigned value itself is then assigned to b. This is
because of right to left associativity i.e. the right most operator is evaluated first.
Ternary operator -
It is also called a conditional operator. It called ternary operator because it is composed of three
expression
Usage -
If the conditional Expression evaluates to true then expr1 is evaluated otherwise expr2 is
evaluated.
e.g.
int a = 5, b = 2, c, d;
c = (a < b) ? a : b; // c will be 2 because a < b is false.
d = (a > b) ? a : b; // d will be 5 because a > b is true.