0% found this document useful (0 votes)
88 views

Important Notes On JAVA

Java is an object-oriented programming language developed by Sun Microsystems in 1995 that is similar to C++. It is compiled to bytecode that can run on any Java Virtual Machine, making it platform independent. Key features of Java include being compiled and interpreted, platform independent, object-oriented, robust and secure, distributed, simple and familiar, multithreaded and interactive, and dynamically extensible. The basic concepts of object-oriented programming in Java include classes, objects, inheritance, polymorphism, abstraction, and encapsulation.

Uploaded by

Dipesh Adhikari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Important Notes On JAVA

Java is an object-oriented programming language developed by Sun Microsystems in 1995 that is similar to C++. It is compiled to bytecode that can run on any Java Virtual Machine, making it platform independent. Key features of Java include being compiled and interpreted, platform independent, object-oriented, robust and secure, distributed, simple and familiar, multithreaded and interactive, and dynamically extensible. The basic concepts of object-oriented programming in Java include classes, objects, inheritance, polymorphism, abstraction, and encapsulation.

Uploaded by

Dipesh Adhikari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit :1

Introduction to Java
Java is the object oriented programming language similar to C++ high level
programming language which was developed by Sun Microsystems in 1995. Java
was originally called OAK.
Object oriented means the capability to reuse the code.Secondly it is platform
independent because it is possible to develop a single application which can run on
multiple platforms like windows,UNIX systems.
The java compiler converts the source code of file extension .java to bytecode of
file extension .class and then java interpreter convert Bytecode to Machine code.
Features of java:
1. Compiled and Interpreter
2. Platform Independent
3. Object-oriented
4. Robust and Secure
5. Distributed
6. Simple, small and familiar
7. Multithreaded and Interactive
8. Dynamic and extensible code
9. Architectural Neutral
10. Portable
11. Interpreted
12. High performance

Object oriented programming concepts:


1) Class
The class is one of the Basic concepts of OOPs which is a group of similar entities. It
is only a logical component and not the physical entity. Lets understand this one of
the OOPs Concepts with example, if you had a class called “Expensive Cars” it could
have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or
speed of these cars. While the methods may be performed with these cars are
driving, reverse, braking etc.
2) Object
An object can be defined as an instance of a class, and there can be multiple
instances of a class in a program. An Object is one of the Java OOPs concepts
which contains both the data and the function, which operates on the data. For
example - chair, bike, marker, pen, table, car, etc.
3) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object acquires the properties
and behaviors of the parent object. It’s creating a parent-child relationship between two
classes. It offers robust and natural mechanism for organizing and structure of any software.
4) Polymorphism
Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable,
object or function to take on multiple forms. For example, in English, the verb run has a
different meaning if you use it with a laptop, a foot race, and business. Here, we understand
the meaning of run based on the other words used along with it. The same also applied to
Polymorphism.
5) Abstraction
Abstraction is one of the OOP Concepts in Java which is an act of representing essential
features without including background details. It is a technique of creating a new data type
that is suited for a specific application. Lets understand this one of the OOPs Concepts with
example, while driving a car, you do not have to be concerned with its internal working.
Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.
6) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data and code. In this
OOPs concept, the variables of a class are always hidden from other classes. It can only be
accessed using the methods of their current class. For example - in school, a student cannot
exist without a class.
Advantages of OOPs (Object-Oriented Programming System):
• OOPs Concepts in Java offer easy to understand and a clear modular structure for
programs.
• Objects created for Object-Oriented Programs can be reused in other programs. Thus it
saves significant development cost.
• Large programs are difficult to write, but if the development and designing team follow
OOPS concepts, then they can better design with minimum flaws.
• It enhances program modularity because every object exists independently.
History of Java:
Java was originally developed by james Gosling at Sun Microsystems and releases in 1995 as
a core component.
Java was originally designed for interatcive television.java team members(known as Green
Team) initiated the project to develop a language for digital devices such as set top boxes,
televisions etc.
Java was earlied called as OAK which was developed by the Grean Team as a Green Project.
In 1995 Oak was renamed as java because it was already a trademark by Oak technologies.
Difference between Java application and Java Applet

Java Application Java Applet


Applets are small Java programs that are
Applications are just like a Java programs that
can be execute independently without using designed to be included with the HTML web
the web browser. document. They require a Java-enabled web
browser for execution.

Application program requires a main function Applet does not require a main function for its
for its execution. execution.

Java application programs have the full access Applets don’t have local disk and network
to the local file system and network. access.

Applets can only access the browser specific


Applications can access all kinds of resources
available on the system. services. They don’t have access to the local
system.

Applications can executes the programs from Applets cannot execute programs from the local
the local system. machine.

An application program is needed to perform An applet program is needed to perform small


some task directly for the user. tasks or the part of it.
Java Virtual Machine:
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed.
The key that allows java to solve the both security and portability problems is that the
output of a java compiler is not executable code rather it is byte code.
Byte code is highly optimized set of instructions designed to be executed by java runtime
systems, which is called Java Virtual Machine (JVM). JVM is interpreter for byte code.
Translating a java program into byte code helps makes it much easier to run a Program in
a wide variety of environments. The reason is straightforward: only the JVM needs to be
implemented for each platform. Once the run-time package exists for a given system, any
Java program can run on it.
Practical Examples
//program to take input and print value
import java.util.Scanner;
public class simple
{
public static void main(String args[])
{
int a;
float b;
Scanner sc=new Scanner(System.in);
System.out.println("Input value of a and b");
a=sc.nextInt();
b=sc.nextFloat();
System.out.println("Valuof a:"+a);
System.out.println("Valuof b:"+b);
}
}
//Program to find area of rectangle
class Rectangle
{
private int len;
private int bred;
public void Setdata(int l,int b)
{
len=l;
bred=b;
}
public int FindArea()
{
return(len*bred);
}
}
class MainRectangle
{
public static void main(String arg[])
{
Rectangle r=new Rectangle();
r.Setdata(3,4);
int A=r.FindArea();
System.out.println("Area="+A);
}
}
//Program to find the sum of two values
class Test
{
int a,b;
Test(int x, int y) //parametirezed constructor
{
a=x;
b=y;
}
void display()
{
System.out.println("Value of a:"+a);
System.out.println("Value of b:"+b);
}
void addition()
{
int c;
c=a+b;
System.out.println("Value after addition:"+c);
}
}
class Add

{
public static void main(String arg[])
{
Test t=new Test(2,3);// parametirzed constructor
t.display();
t.addition();
}
}
//Program to show Method overloading
public class MainOverload
{
public int sum(int x,int y)
{
return(x+y);
}
public double sum(double x,double y)
{
return(x+y);
}

public static void main(String arg[])


{
MainOverload a=new MainOverload();
System.out.println(a.sum(3,4));
System.out.println(a.sum(3.5,4.5));

}
}
//program to show the use of static keyword
class Counter
{
static int count=0;
Counter() //Default constructor
{
count++;
}
void Display()
{
System.out.println(count);
}

public static void main(String arg[])


{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
c3.Display();
}
}
//Program to show the Multiple inheritance
interface A
{
void methodA();
}
interface B
{
void methodB();
}
class C implements A,B
{
public void methodA()
{
System.out.println("Within class C of method A");
}
public void methodB()
{
System.out.println("Within class C of method B");
}
}
class MainMultiple
{
public static void main(String arg[])
{
C obj=new C();
obj.methodA();
obj.methodB();
}
}
//program to show the use of execution without thread concept
class test1
{
public void start()
{
for(int i=1;i<5;i++)
{
System.out.println("Hi:"+i);
}
}
}
class test2
{
public void start()
{
for(int j=1;j<5;j++)
{
System.out.println("Hello:"+j);
}
}
}
public class simplethread
{
public static void main(String arg[])
{
test1 t1=new test1();
test2 t2=new test2();
t1.start();
t2.start();
}
}
//program to show the use of MultiThreading by extending Thread
class thread1 extends Thread
{
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println("Hi"+i);
try{ Thread.sleep(1000); } ////1000 milisec is nearly equals to 1 sec
catch(Exception e){}
}
}
}
class thread2 extends Thread
{
public void run()
{
for(int j=1;j<5;j++)
{
System.out.println("Hello"+j);
try{ Thread.sleep(1000); }
catch(Exception e){}
}
}
}
public class threadtest
{
public static void main(String arg[])
{
thread1 t1=new thread1();
thread2 t2=new thread2();
t1.start();
try{ Thread.sleep(5); }
catch(Exception e){}
t2.start();
}
}
//program to show the use of MultiThreading by implementing Thread
class thread1 implements Runnable
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread1:"+i);
try{ Thread.sleep(1000); } //1000 milisec nearly equals to 1 sec to pause the display of output
catch(Exception e){}
}
}
}
class thread2 implements Runnable
{
public void run()
{
for(int j=0;j<5;j++)
{
System.out.println("Thread2:"+j);
try{ Thread.sleep(1000); }
catch(Exception e){}
}
}
}
class threadtest2
{
public static void main(String arg[])
{
thread1 t1=new thread1();
thread2 t2=new thread2();
Thread tobj1=new Thread(t1);
Thread tobj2=new Thread(t2);
tobj1.start();
try{ Thread.sleep(500); }
catch(Exception e){}
tobj2.start();
}
}

You might also like