Java Notes
Java Notes
[ 1 ] Simple :
[ 2 ] Portable :
[ 3 ] Object Oriented :
-java support various object oriented features such as
data encapsulation , inheritance , polymorphism and
dynamic.
[ 4 ] Robust :
[ 5 ] Multi-threaded :
[ 6 ] Interpreted :
[ 7 ] platform independent…
[1] Encapsulation :
- encapsulation is the mechanism that binds together code and
the data , it manipulate and keep both safe from outside
interreference and misuse.
-In java the basic of encapsulation is the class which define the
structure and behaviour (data and code)that will be shared by a
set of object.
[ 2 ] polymorphism :
-polymorphism is the feature that allows us to perform single
action in different ways.
EXAMPLE :
class Animal
[ 3 ] Inheritance :
-Inheritance is the mechanism in java by which derived class
can borrow the properties of base class and at the game time
derived class may have some additional properties.
EXAMPLE:
class Vehicle
System.out.println("Tuut, tuut!");
myFastCar.honk();
OUTPUT :
Tuut, tuut!
Ford Mustang
Output:
OUTPUT :
this is num :100
the value of num*2 is:200
Example :
public class Main
{
public static void main(String[] args)
{
if (20 > 18)
{
System.out.println("20 is greater than 18"); // obviously
}
}
}
OUTPUT:
20 is greater than 18
-in compound if statement more than one statement can be
executed when if condition is true.
-All this executable statement are placed in curly braces({})
EXAMPLE:
If(age>18)
{statement1,statement2,…statementn}
FOR LOOP :
-“For” is a keyword for repeatedly execute a sequence of code
by creating a loop .
-loops are used whenever you need to perform a repetitive task
Syntax :
for(initialization,condition,iteration)
EXAMPLE :
public class Main
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
}
}
OUTPUT:
0
1
2
3
4
Question : 13 Identifiers…
-it is used for class name , methods name and variable names .
- Rules to be followed for identifiers :
- Identifiers should be written using alphabet , digits ,
underscore (_) and dollar sign ($)
- Should not contain any other special character with in them .
- Must not start with a digit .
- the identifiers are case sensitive i.e int a ; or int A; are treated
two different entity.
- can be of any length.
Question : 14 Java class libraries…
-Java has two built-in Methods println() and print().
-These methods are accessed through System class , which is a
class predefined by java that is automatically included in
programs i.e System.out.println()
-class libraries much of the functionality that comes in java .
#Boolean Type :
#Literals :
-Constant values that appear directly in the program .
#Hexa-Decimal :
-Sequence of digit precided by ox or ox is considered as hexa-
decimal point , it may also include , a character from ‘a’ to ‘f’ or
‘A’ to ‘F’ that represent 10 respectively that is oxd ,oxf .
#octal :
-it is a combination of a number that has a digit from 0 to 7 with
a leading Zero .
#operators :
-Operators are used to perform operations on variables and values.
EXAMPLE :
Public class demo
{
Public static void main (String args [])
{
Int a =10;
Int b =5;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}
}
OUTPUT:
15
5
50
2
0
[ 5 ] Assignment operator :
-single equal sign (=) operator .
EXAMPLE :
Int x , y , z ;
x=y=z;
//set x,y and z to 100
[ 6 ] Shorthand Assignment :
-java defines 12 shorthand assignment operators that
combine assignement with 5 arithmetic operators .
(+=,-=,*=,/=,%=)
Example :
X+=2 means x=x+2
Y*=5 means y=y*5
OUTPUT:
# operator precedence :
-precedence relation specifies which operation must be
done first during the expression evalution .
-Associativity tells the direction of execution of
operators that can be either left to right or right to left
Example :
In expression a=b=c=10; the assignment operator is
executed from right to left i.e. c will be assign 10; then
60 will be assigned by c and finally a will be assigned by
b.
#Expression :
-it is a combination of operators and operands in a
specific manner
EXAMPLE :
C = a+b*c ; // expression with arithmetic operators
(a>b)&&(b<c) // expression with logical operator and
relational operator .
-Evaluation of expression is a order of evaluating the
expression using precedence and associativity rules .
Example :
To evaluate 2+3*4 first 3*4=12 then 2 is added and the
result is 14 as multiplication has higher precedence
then addition .