CoreJava Day1
CoreJava Day1
Java Features :-
1. Open Source – 100% Free.
2. Object Oriented
a) Encapsulation –
To Bind Together Related Data. Variables are bound in
Method, Methods are bound in a Class, Similar Classes are
bound in a Package and Package are bound in a Jar File which
is called Library.
b) Abstraction –
To Hide. When we use Predefined library (import a package
from a Jar file) and use Predefined Class and Method in Our
Program without knowing How that method is working (Body
is abstract from us)
Note – OOPs Abstraction Concept is different from abstract
keyword and Data abstraction.
c) Inheritance –
Reuse. When a method is already defined in One class and
another class require same method we can Inherit that method
by using extends keyword.
Note – Difference between Abstraction and Inheritance In
Abstraction we use predefined library by import keyword
where as in Inheritance we reuse a method by extending a
class. Inherited method can be over ridden but Predefined
methods are Not over ridden.
d) Polymorphism –
Polymorphism is the ability of an object to take on many
forms. Java Support Polymorphism by Overloading and
Overridding.
Path
C:\Program Files (x86)\Java\jdk1.7.0_21\bin;
Java_Home
C:\Program Files (x86)\Java\jdk1.7.0_21\
Test.java
public class Test
{
public static void main(String[] args)
{
System.out.println(“hello”);
System.out.println(“Doing Sum ”+(2+2));
System.out.println(“Doing Substract ”+(4-2));
} // end of main
} // end of class
About main()
1. Predefined method.
2. Belong to JVM.
3. Does not Belong to class.
4. Automatically called by JVM at run time.
5. Entry point of Program.
What is variable – Variable is a Container to Hold Data.
Data can be of Primitive or Not Primitive Type.
Test.java
public class Test
{
// user defined method belong to class
public void sum(int i,int j)
{
System.out.println(“Doing Sum ”+(i+j));
}
public void substract(int i, int j)
{
System.out.println(“Doing Substract ”+(i-j));
}
// default constructor – No need to define
/* public Test()
{ // empty } */
// predefined method belong to JVM
public static void main(String[] args)
{
System.out.println(“hello”);
Controls :-
Simple if Statement
The simple if statement has the following syntax::-
if (<conditional expression>)
{
<statement>
if-else Statement
if (<conditional expression>)
<statement1>
<statement2>
else
<statement3>
switch Statement
Conceptually the switch statement can be used to choose one among many
alternative actions, based on the value of an expression. Its general form is as
follows:
Syntax :-
while Statement
The while statement executes the loop body as long as the loop condition is true.
The syntax of the while loop is
for-loop statement
The for loop is the most general of all the loops. It is mostly used for counter-
controlled loops, that is, when the number of iterations is known beforehand.
The syntax of the loop is as follows:
Examples as Assignments
Calculator
Even Odd
Profile
Citizenship
Shortlist Resume