Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

CoreJava Day1

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CoreJava Day1

Uploaded by

kewatkuldeep558
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Core Java – Day 1 Introduction

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.

3. Platform Independent and Portable – Platform


Independent and Portable are related to each other. If
Program is Platform Independent i.e. Its can run on any
environment It becomes Portable. Java is Platform
Independent because JVM is Platform dependent and
Every O/S has its own JVM which Interpret java
program as per O/S enviorment.

4. Secure – Java works inside Java Security Firewall.

5. Robust – Java does not have Pointers, Data Structure,


Disconstructor, Multiple Inheritance..

6. Easy – Java is Easy to write and execute.

7. Multithreading – Java Threads makes Pool and Support


Multiple Requests each Request by a Thread.
Note – Multithread is Not Multitasking.

Setting Java Environment

Step 1 - Down load JDK and Install.

Step 2 – Set Enviornment Variables

 Path
 C:\Program Files (x86)\Java\jdk1.7.0_21\bin;

 Java_Home
 C:\Program Files (x86)\Java\jdk1.7.0_21\

Step 3 – Download Eclipse and install.


Note – Eclipse is simply an IDE (Integrated Development
Environment) where we run Java Program using Eclipse
Environment. We can use other IDE also Like Net Beans also
or Simply Notepad, Wordpad..

Wrting Simple Java Program

What is a class – class is a Keyword to create Java File. Class


name First Letter should be in Caps and program should be
saved by name of a class. Main class from whoes name
Program is saved must be public.
Class does not perform any action, class encapsulate
variables and Methods.

To make a class executable it should have main method as


under :-
Note – Java is case sensitive.

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.

What is method – Method encapsulates variables and


performs action on variable. Method can takes arguments
(input to method) and returns void, primitive data or Non
Primitive data.
Note – user defined methods Belong to class and are not
automatically called by JVM. They have to be called by class
refernce variable after called Constructor.

What is Constructor – Constructor are special type of


method which has same name as that of a class and does not
return any thing Not even void.
Note – Java takes one default Constructor on its own. Default
constructor does not take arguments and has empty body.

Constructor is called from main method using new keyword


and returns class reference variable (Non Primitive)

Test t = new Test(); // initialize a class i.e create a memory


for a class and return memory pointer variable using which we
call class method, variable.

What is an Object – Constructor create class Data called class


Object. Like Student sitting in class 5 is a student of class 5.
Room 5 -- Space (Students)
5.Amit (call Amit from class 5)

Amit is student of class 5 because he is in space refer as class


5.

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”);

Test t = new Test(); // calling default constructor


t.sum(3,6); // called method
} // end of main
} // end of class

Controls, Operators and Loop in Java.


Arithmatic Operator :- + , - , * , / , %
Increment Decrement Operator :- ++ , --
Asignment Operator :- = , += , -= , *= , /= , %=
Compare Operator (returns boolean) :-
== , != , < , > , <=, >=
Logical Operator (multiple condition) :-
&& , || , !

Controls :-
Simple if Statement
The simple if statement has the following syntax::-

if (<conditional expression>)
{

<statement>

if-else Statement

The if-else statement has the following syntax:

if (<conditional expression>)

<statement1>

else if(<conditional expression>)

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

switch (<non-long integral expression>) {


case label1: <statement1>
case label2: <statement2>
...
case labenl: <statementn>
default: <statement>
} // end switch

while Statement

The while statement executes the loop body as long as the loop condition is true.
The syntax of the while loop is

while (<loop condition>)


{
<loop body>
}

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:

for (<initialization>; <loop condition>; <increment expression>)


{
<loop body>
}

Examples as Assignments

Calculator

Even Odd

Profile

Citizenship

Shortlist Resume

You might also like