class classname
{
public static void main(String args[ ])
{
statements;
}
}
- The main
( ) method contains arguments called as Command-line arguments.
- If a
method is called with respect a class name means it is a static method.
sum
name$
student_123
int a,b;
float f1,f2;
char c1,c2;
Type |
Size in bytes |
Range |
int |
4 |
-231 to 231-1 |
float |
4 |
3.4E-38
to 3.4E+38 |
double |
8 |
1.7E-308
to 1.7E+308 |
char |
2 |
0 to 65535 |
-The complete set of data types in java is depicted as,
Escape Sequence |
Meaning |
\n |
Newline |
\t |
Horizontal
Tab |
\b |
Backspace |
\r |
Carriage
return |
\f |
Form
feed |
\\ |
Backslash |
\’ |
Single
quotes |
\” |
Double
quotes |
a)
Arithmetic operators
b)
Relational operators
c)
Logical operators
d)
Increment or decrement operators
e)
Assignment operators
f)
Conditional operators
g)
Bitwise operators
h)
Special operators
a) Arithmetic operators:
Operator |
Meaning |
+ |
Addition
or Unary plus |
- |
Subtraction
or Unary minus |
* |
Multiplication |
/ |
Division |
% |
Modulo
division |
-Integer division truncates any fractional part.
Examples:
a+b,
a-b, a*b, a/b, a%b
Operator |
Meaning |
< |
Less
than |
<= |
Less than
or equal to |
> |
Greater
than |
>= |
Greater
than or equal to |
== |
Equality |
!= |
Inequality |
Example:
a>b
a>=b
a<b
a<=b
a==b
a!=b
Operator |
Meaning |
&& |
Logical
AND |
|| |
Logical
OR |
! |
Logical
NOT |
Operand1 |
Operand2 |
Operand1&&operand2 |
Operand1||operand2 |
Non-zero |
Non-zero |
1 |
1 |
Non-zero |
0 |
0 |
1 |
0 |
Non-zero |
0 |
1 |
0 |
0 |
0 |
0 |
Example:
(a>b)&&(a>c)
(a>b)||(a>c)
-The logical not is used as a negation or complement of the expression.
iv) Increment or decrement operators:
-The
increment operator is ++ which means +1
-The
decrement operator is - - which means -1
-The
operand must be either incremented or decremented by 1.
Example:
m=5;
y=++m; results m=6 and y=6.
m=5;
y=m++; results m=6 and y=5.
m=5;
y= --m; results m=4 and
y=4.
m=5;
y=m--; results m=4 and
y=5.
v) Assignment operators:
-Assignment
operators are used to assign the result of an expression to a variable.
Operator |
Meaning |
= |
Assignment |
-In
addition, java has a set of short-hand assignment operators of the form
V
OP=EXP;
- It is equivalent to
V=V OP EXP;
-The
short-hand assignment operators are
+= , -= , *= , /= , %=
Example:
a+=b ------------- a=a+b
a-=b ------------- a=a-b
a*=
b -------------- a=a*b
a/=b --------------- a=a/b
a%=b -------------
a=a%b
vi) Conditional operators:
-It is
also known as Ternary operator.
-The
symbols used to construct a conditional expression are ? and :
-A
conditional expression is of the form,
exp1?exp2:exp3;
Example:
(a>b)?System.out.println(“a
is greater”): System.out.println (“b
is greater”);
- It is
equivalent to if-else statement in java
Example:
if(a>b)
System.out.println(“a is greater”);
else
System.out.println (“b is greater”);
vii) Bitwise operators:
-‘C’
provides bitwise operators that operate on data at the bit-level.
-Bitwise
operators interpret operands as string of bits.
-These
bit strings are then interpreted according to datatype.
-Bitwise
operators are of 2 types.
i) Logical bitwise operators.
ii) Shift bitwise operators.
i) Logical bitwise operators:
Operator |
Meaning |
& |
Bitwise
AND |
| |
Bitwise
OR |
^ |
Bitwise
exclusive OR |
~ |
One’s
complement |
Bitwise
AND operator:
-The
Bitwise AND (&) is a binary operator that requires two integral
operands(character or integer).
-It
does a bit-by-bit comparison between two operands.
-The
result of the comparison is 1 only when both bits are 1, otherwise it is 0.
First
operand bit |
Second
operand bit |
Result(&)
|
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Bitwise
OR operator:
-The
Bitwise inclusive OR (|) is a binary operator that requires two integral operands(character
or integer).
-It
does a bit-by-bit comparison between two operands.
-The
result of the comparison is 0 only when both bits are 0, otherwise it is 1.
First
operand bit |
Second
operand bit |
Result(
| ) |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
Bitwise
exclusive OR:
-The
Bitwise exclusive OR (^)is a binary operator that requires two integral
operands(character or integer).
-It
does a bit-by-bit comparison between two operands.
-The
result of the comparison is 1 only if one of the operands is 1, otherwise it is
0.
First
operand bit |
Second
operand bit |
Result(^)
|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
One’s
complement:
-The
one’s complement (~) is a unary operator applied to an integral value.
-The
result is 1 when the original bit is 0 and it is 0 when the original bit is 1.
Original
bit |
Result(
~ ) |
0 |
1 |
1 |
0 |
ii) Shift Bitwise operators:
-The
shift Bitwise operators move bits to the right or left.
Operator |
Meaning |
>> |
Bitwise
shift right |
<< |
Bitwise
shift left |
>>> |
Shift
right with zero fill |
Bitwise shift-right operator:
-It
moves some number of bits from right to left.
-It
requires two integral operands.
Syntax:
Operand1 >> Operand2;
-Here,
Operand1 is value to be shifted.
Operand2
is number of bits to be shifted.
Bitwise
shift-left operator:
-It
moves some number of bits from left to right.
-It
requires two integral operands.
Syntax:
Operand1 << Operand2;
-Here,
Operand1 is value to be shifted.
Operand2
is number of bits to be shifted.
viii) Special operators:
- Java supports some special operators such as,
a)
instanceof operator
b)
member selection operator
a) instanceof operator:
- The instanceof is an object reference operator and
returns true if the object on the left-hand side is an instance of the class
given on the right hand side.
- This operator allows us to determine whether the
object belongs to a particular class or not.
Example:
person instanceof
student
- It is true if the object person belongs to the
class student, otherwise it is false.
b) member selection operator:
- The dot (.) operator is used to access the
instance variables and methods of class objects.
Example:
person.age // Reference to the variable age
person.salary( )
// Reference to the method salary( )
- It is also used to access classes and sub packages
from a package.
6. Arithmetic
Expressions:
- An
arithmetic expression is a combination of variables, constants and operators
arranged as per the syntax of the language.
- Java
does not have an operator for exponentiation.
Evaluation of Expressions:
Syntax:
Variable
= expression;
Rules for evaluation of expressions:
-First,
parenthesized sub-expressions from left to right are evaluated.
-If
parentheses are nested, the evaluation begins with the innermost
sub-expression.
-Then
the precedence rule is applied.
-Then
the Associativity rule is applied.
-Arithmetic
expressions are evaluated from left to right using the rules of precedence.
Precedence of Arithmetic expressions:
High
priority * / %
Low
priority + -
Example:
Z=a-(b/(3+c)*2)-1
where a=9, b=12, c=3
=9-(12/(3+3)*2)-1 (inner parenthesis)
=9-(12/6*2)-1 (precedence for * / is left to
right)
=9-(2*2)-1 (parenthesis)
=9-4-1 (Associativity rule for
-)
=5-1
7. type
conversion and casting:
-conversion
of one data type to another is called as type conversion.
-It is
of two types.
i) Implicit Type conversion
ii)Explicit Type conversion
i) Implicit Type conversion:
-It is
also called as internal type conversion or type coercion or Automatic type
conversion.
-If the
operands are of different types, the lower type is automatically converted into
higher.
Example:
int
a;
float
b,sum;
sum=a+b
- Where
‘a’ is automatically converted into float type.
-
Automatic type conversion chart in java is depicted as,
ii) Explicit Type conversion:
-It is
also called as external type conversion or casting a value.
-The
process of local conversion is known as “Explicit type conversion”.
Syntax:
(datatype) expression;
-Where
expression may be a constant, variable or expression.
Example:
(int)7.5
results 7
(int)a+b
converts the result as int value.
Operator Precedence and Associativity:
- Java
operators are listed in order of precedence (highest to lowest).
- Their
Associativity indicates in what order operators of equal precedence in an
expression are applied.
-The complete set of operators with priority is depicted as,
8. Control Statements: (Flow of control)
- A Control statement is a statement used to
control the flow of execution in a Java Program.
SELECTION STATEMENTS:
-Also
called as conditional or decision-making control statements.
-There
are two types in Selection control statements.
i) Two-way selection
control statements
ii) Multi-way selection
control statements
i) Two-way selection control statements:
-The
different two-way selection statements are,
a) if-else statement
b) null else statement
c) Nested-if statement
a)
if-else statement:
Syntax:
if(condition)
{
true-block statements;
}
else
{
false-block statements;
}
next statement;
Example:
if(a>b)
{
System.out.println(“a
is greater”);
}
else
{
System.out.println(“b
is greater”);
}
b) null else statement:
-Also
called as simple-if statement.
Syntax:
if(condition)
{
statements;
}
next statement;
Example:
if(a==2)
{
p++;
}
System.out.println(“program over”);
c) Nested-if statement:
-if
within if is called as Nested-if.
Syntax:
if(condition-1)
{
if(condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
if(condition-3)
{
Statement-3;
}
else
{
Statement-4;
}
}
next statement;
Example:
if(a>b)
{
if(a>c)
{
System.out.println(“a is greater”);
}
else
{
System.out.println (“c is greater”);
}
}
else
{
if(b>c)
{
System.out.println(“b is greater”);
}
else
{
System.out.println(“c
is greater”);
}
}
ii) Multi-way selection control
statements:
-The
different multi-way selection statements are,
a)
switch statement
b)
else-if ladder statement
a) switch statement;
Syntax:
switch(expression)
{
case
value-1:statement-1;break;
case
value-2:statement-2;break;
………………………….
………………………….
case
value-n:statement-n;break;
default:
default statement;
}
next statement;
Example:
switch(digit)
{
case 0: System.out.println(“ZERO”);break;
case 1: System.out.println(“ONE”);break;
case 2: System.out.println(“TWO”);break;
case 3: System.out.println(“THREE”);break;
case 4: System.out.println(“FOUR”);break;
case 5: System.out.println(“FIVE”);break;
case 6: System.out.println(“SIX”);break;
case 7: System.out.println(“SEVEN”);break;
case 8: System.out.println(“EIGHT”);break;
case 9: System.out.println(“NINE”);break;
default: System.out.println(“Enter
between 0-9”);
}
b)else-if ladder statement:
Syntax:
if(condition-1)
{
Statement-1;
}
else if(condition-2)
{
Statement-2;
}
…………………
……………..…..
else if(condition n-1)
{
Statement-(n-1);
}
else
{
Statement-n;
}
next statement;
Example:
if(a>b&&a>c&&a>d)
{
System.out.println(“a
is greater”);
}
else if(b>a&&b>c&&b>d)
{
System.out.println(“b
is greater”);
}
else
if(c>a&&c>b&&c>d)
{
System.out.println(“c
is greater”);
}
else
{
System.out.println(“d
is greater”);
}
LOOP statements:
-The
iteration control statements are also called as Repetition or Iteration control
statements.
-A looping process includes the following
four steps.
-Setting
and initialization of a counter.
-Execution
of the statements in the loop body.
-Test
for a specified condition (loop control expression) for execution of a loop
-Incrementing
or Decrementing counter.
i)
Pretest and Posttest loops:
-In a Pretest
loop, the condition is checked before we execute a loop body.
-It is also called as entry-controlled loop.
-In the Posttest loop, we always execute the loop body atleast once.
-It is also called as exit-controlled loop.
a)
while statement:
Syntax:
while(condition)
{
loop body;
}
next
statement;
Example:
n=10, i=1,sum=0;
while(i<=n)
{
sum=sum+i;
i++;
}
System.out.println(“sum=”+sum);
b)
do-while statement:
Syntax:
do
{
loop body;
}while(condition);
next
statement;
Example:
n=10,i=1,sum=0;
do
{
sum=sum+i;
i++;
} while(i<=n);
System.out.println(“sum=”+sum);
c) for
statement:
Syntax:
for(initialization;condition;inc
or dec)
{
loop body;
}
next
statement;
Example:
n=10,i,sum=0;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
System.out.println(“sum=”+sum);
Unconditional
control statements:
-The unconditional control statements are,
a)
break statement
b)
continue statement
a)
break statement:
-The break statement skips from the loop
or block in which it is defined.
-The control then automatically goes to
the first statement after the loop or block.
-The general format is
break;
Example:
sum=0,i
for(i=1;i<=10;i++)
{
if(i==3)
break;
sum+=i;
}
System.out.println(“Sum=”+sum);
b)
continue statement:
-The continue statement is used for
continuing next iteration of loop statements.
-When it occurs in the loop, it does not
terminate but it skips the statements after it.
-It is useful when we want to continue the
program without executing any part of the program.
-The general format is
continue;
Example:
sum=0,i
for(i=1;i<=10;i++)
{
if(i==3)
continue;
sum+=i;
}
System.out.println(“Sum=”+sum);
9. Arrays:
Array - Definition:
- An array is a collection of variables of same datatype referenced by a
common name.
OR
- An array is a collection of homogeneous elements
OR
- An array is a group of contiguous or related data items that share a
common name and stores its values in sequential memory locations.
- Array
index starts from 0 (zero).
- Arrays are dynamic in java
Array - Types:
- There are 3 types of arrays
i) 1-D arrays
ii) 2-D arrays
iii) M-D arrays
i) One-Dimensional Arrays:
- Also called as single subscripted variables.
Declaration of 1-D arrays:
datatype arrayname[ ]=new
datatype[size];
Example:
int a[ ]=new int[5];
Initialization of 1-D arrays:
datatype arrayname[ ]={List of
values};
Example:
int a[ ]={10,20,30,40,50};
ii) Two-Dimensional Arrays:
- Also called as Double subscripted variables.
Declaration of 2-D arrays:
datatype arrayname[ ][ ]=new
datatype[rowsize][colsize];
Example:
int a[ ][ ]=new int[2][2];
Initialization of 2-D arrays:
datatype arrayname[ ][ ]={List of
values};
Example:
int a[ ][ ]={{2,4},{6,8}};
iii) Multi Dimensional Arrays:
-
Multidimensional arrays can be defined as array of arrays.
- An
example is 3-D arrays.
Declaration of 3-D arrays:
datatype arrayname[ ][ ][ ]=new
datatype[size1][size2][size3];
Example:
int a[ ][ ][ ]=new int[2][2][2];
Initialization of 3-D arrays:
datatype arrayname[ ][ ][ ]={List
of values};
Example:
int a[ ][ ][ ]={{1,3},{5,7}
{2,4},{6,8}};
10. Math Class in JAVA:
- Java supports basic mathematical functions through Math class
- Math class is defined in the java.lang package
- Mathematical functions sqrt, sin, cos, log are frequently used in the
analysis of real-life problems
Syntax:
Math.function_name();
Example:
Math.sqrt(x);
No comments:
Post a Comment