You are on page 1of 7

JAVA

How to write a program?


Calculate the area of a circle, and the formula is PI * r * r.
Let's develop some programming mindset here.
Whenever you are solving some problem first divide the problem into three parts.
e.g
Input - is r i.e. the radius, given r you will be able to proceed with calculation.
Process - is 3.14159 * r * r
Output - the calculated radius.
Now try to differentiate
What is the input?
What is the process/formula?
What is the output?

Process - Is a program in execution.

Each process is given three standard streams

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

< Less than

>= Greater than or equal to

<= Less than or equal to


== Equal to

!= Not equal to
These operators return a Boolean value i.e. true if the given condition is met otherwise false.

Logical Operators

&& - Logical AND


Returns true if both the expressions are true.

|| - 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.

Short circuiting nature of Logical operators -

&& doesn't evaluate the second expression if first expression evaluates to false.

|| doesn't evaluate the second expression if first evaluates to true.

int a = 10, b = 20;


boolean c = a++ > 10 && b++ > 20;
Here the value of a is 11 because a++ is evaluted.

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 both the bits are 1.


| Bitwise OR

Corresponding bit will be 1 if either one of the bit is 1.


^ Bitwise XOR

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

0 becomes 1 and 1 becomes 0.


Sign bit for integral types -
The left mode bit of the integer variable is considered sign bit. If sign bit is 1 it represents a
negative number and 0 for a positive number.

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.

>>> Unsigned right shift


Almost same as right shift except that the vacant left most bits are filled with zero. Hence if you
perform a unsigned right shift over a negative number it turns positive.
Assignment operators -
They are used to assign the value to a variable. They have right to left associativity. i.e. the right
most operator is evaluted first.

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;

which means add 5 to the value of a and assign the result to a.

One more thing to note, in case of

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 -

Conditional Expression ? expr1 : expr2

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.

Classification based on operands -


In the arithmetic expression a + b , + is called operator and a and b are referred as operands.
Based on the number of operands involved in the expression we can classify operators into three
types.

Unary - Only one operand

e.g. -a, +a, !true, ~a

Binary - Two operands

e.g. a + b, a * b , (a > b), (a > b) && (a > c)

Ternary - Three operands

e.g. (a > b)? a : b

You might also like