Unit-1 Java Console input
Unit-1 Java Console input
After this statement executes, br is a character-based stream that is linked to the console through
System.in.
Reading Characters
To read a character from a BufferedReader, use read( ).
The version of read( ) that we will be using is int read( ) throws IOException
Each time that read( ) is called, it reads a character from the input stream and returns it as
an integer value. It returns –1 when the end of the stream is encountered.
Example
Reading Strings
To read a string from the keyboard, use the version of readLine( ) that is a member of the
BufferedReader class.
import java.io.*;
class BRReadLines {
do {
str = br.readLine();
System.out.println(str); } while(!str.equals("stop"));
}}
Here is a sample run:
stop
Operators
Java operators are special symbols that perform various operations on variables or values.
1. Arithmetic
2. Bitwise
3. Relational and
4. Logical
Arithmetic Operators
Example
int a = 1 + 1; 2
int b = a * 3; 6
int c = b / 4; 1
int d = c - a; -1
int e = -d; 1
Example
int x = 42;
double y = 42.25
x%10; 2
y%10; 2.25
Java provides special operators that can be used to combine an arithmetic operation with an
assignment.
General form:
can be rewritten as
Example
a = a + 4;
can be written
a+=4;
The %= obtains the remainder of a/2 and puts that result back into a.
Benefits
x = x + 1;
can be written
x++;
Y=y-1;
Can be written
y--;
Prefix form
Postfix form
In the prefix form, the operand is incremented or decremented before the value is obtained for
use in the expression.
In postfix form, the previous value is obtained for use in the expression, and then the operand is
modified.
Example:
x = 42;
y = ++x;
x = x + 1;
y = x;
In postfix form, the previous value is obtained for use in the expression, and then the operand is
modified.
Example:
x = 42;
y = x++;
Y=x;
x = x + 1;
The bitwise logical operators are &, |, ^, and ~. The following table shows the outcome of each
operation.
Example
42=00101010
becomes
11010101
The AND operator, &, produces a 1 bit if both operands are also 1. A zero is produced in all
other cases.
The Bitwise OR
The OR operator, |, combines bits such that if either of the bits in the operands is a 1, then the
resultant bit is a 1, as shown here:
Example
int c = a | b;
int d = a & b;
int e = a ^ b;
The Left Shift
The left shift operator, << ,shifts all of the bits in a value to the left a specified number of times.
It has this general form:
Here, num specifies the number of positions to left-shift the value in value.
The << moves all of the bits in the specified value to the left by the number of bit positions
specified by num. For each shift left, the high-order bit is shifted out (and lost), and a zero is
brought in on the right.
Example
byte a = 64;
int i;
i = a << 2;
output
i=256
Since a is promoted to int for the purposes of evaluation, left-shifting the value 64 (0100 0000)
twice results in i containing the value 256 (1 0000 0000).
The right shift operator, >>, shifts all of the bits in a value to the right a specified number of
times. Its general form is shown here:
Here, num specifies the number of positions to right-shift the value in value. That is, the >>
moves all of the bits in the specified value to the right the number of bit positions specified by
num.
The following code fragment shifts the value 32 to the right by two positions, resulting in a being
set to 8: int a = 32; a = a >> 2; // a now contains 8
The >> operator automatically fills the high-order bit with its previous contents each time a shift
occurs. This preserves the sign of the value.
To shift a zero into the high-order bit no matter what its initial value was. This is known as an
unsigned shift.
shift-right operator, >>>, which always shifts zeros into the high-order bit.
Here, a is set to –1, which sets all 32 bits to 1 in binary. This value is then shifted right 24 bits,
filling the top 24 bits with zeros, ignoring normal sign extension. This sets a to 255.
int a = -1;
a = a >>> 24;
The binary bitwise operator combines the assignment with the bitwise operation
Example
a = a >> 4;
a >>= 4;
a = a | b;
a |= b;
Relational Operators
The relational operators determine the relationship that one operand has to the other.
Any type in Java, including integers, floating-point numbers, characters, and Booleans can be
compared using the equality test, ==, and the inequality test, !=.
int a = 4;
int b = 1;
boolean c = a < b;
The Boolean logical operators shown here operate only on boolean operands.
All of the binary logical operators combine two boolean values to form a resultant boolean value
The logical Boolean operators, &, |, and ^, operate on boolean values in the same way that they
operate on the bits of an integer. The logical ! operator inverts the Boolean state: !true == false
and !false == true.
Secondary versions of the Boolean AND and OR operators, and are known as short-
circuit logical operators.
The OR operator results in true when A is true, no matter what B is.
Similarly, the AND operator results in false when A is false, no matter what B is.
If use the || and && forms, rather than the | and & forms of these operators, Java will not
bother to evaluate the right-hand operand when the outcome of the expression can be
determined by the left operand alone.
Here, using a single & ensures that the increment operation will be applied to e whether c is equal
to 1 or not.
var = expression;
Here, the type of var must be compatible with the type of expression.
Example
Z=100;
Using a “chain of assignment” is an easy way to set a group of variables to a common value.
The ? Operator
Java includes a special ternary (three-way) operator that can replace certain types of if-then-
else statements. This operator is the ?.
Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is
true, then expression2 is evaluated; otherwise, expression3 is evaluated. The result of the ?
operation is that of the expression evaluated. Both expression2 and expression3 are required
to return the same type, which can’t be void.
Example
When Java evaluates this assignment expression, it first looks at the expression to the left of
the question mark. If denom equals zero, then the expression between the question mark and
the colon is evaluated and used as the value of the entire ? expression. If denom does not
equal zero, then the expression after the colon is evaluated and used for the value of the
entire ? expression.