Module 1,2,3,4 Java
Module 1,2,3,4 Java
Introduction
ACTION
program
BY
Hire giri
Ardent Computech Pvt Ltd
Procedural & Object Oriented
Programming Technique
An object-oriented application uses a collection
of objects, which communicate by passing
messages to request services. Objects are
capable of passing messages, receiving
messages, and processing data. The aim of
object-oriented programming is to try to
increase the flexibility and maintainability of
programs. Because programs created using an
OO language are modular, they can be easier to
develop, and simpler to understand after
development.
Programs are made up of modules, which
are parts of a program that can be coded
and tested separately, and then assembled
to form a complete program.
In procedural languages (i.e. C) these
modules are procedures, where a procedure
is a sequence of statements. In C for
example, procedures are a sequence of
imperative statements, such as
assignments, tests, loops and invocations of
sub procedures.
These procedures are functions, which map
arguments to return statements.
Features of OOPs
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
CLASSES & OBJECTS
A class is a collection of objects that have common
properties, operations and behaviours. A class is a
combination of state (data) and behaviour
(methods). In object-oriented languages, a class is
a data type, and objects are instances of that data
type
Eg we may design a class Human, which is a
collection of all humans in the world. Humans have
state, such as height, weight, and hair color. They
also have behaviour, such as walking, talking,
eating. All of the state and behaviour of a human is
encapsulated (contained) within the class human.
An object is an instance of a class. Objects are units of
abstraction. An object can communicate with other objects
using messages. An object passes a message to another
object, which results in the invocation of a method. Objects
then perform the actions that are required to get a response
from the system.
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Robust
JAVA ME
JAVA SE JAVA EE
Mobile,gam
Console e,small
Applicatio GUI Web Business devices
n applic applicatio
ation n
Types of Java Applications
1) Standalone Application
It is also known as desktop application
or window-based application. An
application that we need to install on
every machine such as media player,
antivirus etc. AWT and Swing are used
in java for creating standalone
applications.
2) Web Application
An application that runs on the server
side and creates dynamic page, is
called web application. Currently,
servlet, jsp, struts, jsf etc.
technologies are used for creating web
applications in java.
3) Enterprise Application
An application that is distributed in
nature, such as banking applications
etc. It has the advantage of high level
security, load balancing and
clustering. In java, EJB is used for
creating enterprise applications.
4) Mobile Application
An application that is created for
mobile devices. Currently Android
and Java ME are used for creating
mobile applications/embedded
system
JVM (Java Virtual Machine)
It is a virtual machine which work over your
operating system to provide proper
environment for your compiled Java code.
JVM only works with bytecode. Hence you
need to compile your java application(.java)
so that it can be converted to bytecode
format (.class file). Which then will be used
by JVM to run application. JVM only provide
environment it needs other library to run
application properly.
The JVM doesn't understand Java typo,
that's why you compile your *.java files to
obtain *.class files that contain the
bytecodes understandable by the JVM.
The JVM performs following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment
JRE
JRE is an acronym for Java Runtime
Environment.It is used to provide runtime
environment.It is the implementation of
JVM.It physically exists.It contains set of
libraries + other files that JVM uses at
runtime.
Implementation of JVMs are also actively
released by other companies besides Sun
Micro Systems.
JDK
The JDK also called Java
Development Kit is a superset of
the JRE, and contains everything
that is in the JRE, plus tools such
as the compilers and debuggers
necessary for developing applets
and applications.
FIRST JAVA PROGRAM
We can write a simple hello java program
easily after installing the JDK in any
editor like notepad, notepad ++ etc
For executing any java program, you need
to install the JDK
set path of the jdk/bin directory.
create the java program
compile and run the java program
How to set path in Java
The path is required to be set for using
tools such as javac, java etc.
Ifyou are saving the java source file
inside the jdk/bin directory, path is not
required to be set because all the tools
will be available in the current directory.
But If you are having your java file
outside the jdk/bin folder, it is necessary
to set path of JDK.
DIFFERENCE BETWEEN SET PATH AND
SET CLASSPATH
path classpath
path variable is set for provide path classpath variable is set for provide
for all java tools like java, javac, path of all java classes which is used
javap, javah, jar, appletviewer in our application.
Creating hello java example
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Open notepad and add the code as above.
Save the file as: MyFirstJavaProgram.java.
Open a command prompt window and go to the directory
where you saved the class. Assume it's C:\.
Type ' javac MyFirstJavaProgram.java' and press enter to
compile your code. If there are no errors in your code,
the command prompt will take you to the next line
(Assumption : The path variable is set).
Now, type ' java MyFirstJavaProgram ' to run your
program.
You will be able to see ' Hello World ' printed on the
window.
Can you save a java source file by
other name than the class name?
If several words are used to form a name of the class, each inner
word's first letter should be in Upper Case.
If several words are used to form the name of the method, then
each inner word's first letter should be in Upper Case.
When saving the file, you should save it using the class
name (Remember Java is case sensitive) and append
'.java' to the end of the name (if the file name and the
class name do not match your program will not compile).
volatile while
Comments in Java
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line
comments.
*/
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Parameter constructor
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Constructor Overloading in Java
1. public
2. private
3. default
4. protected
ENCAPSULATION
Encapsulation in Java is a mechanism of
wrapping the data (variables) and code
acting on the data (methods) together
as a single unit. In encapsulation, the
variables of a class will be hidden from
other classes, and can be accessed only
through the methods of their current
class. Therefore, it is also known as
data hiding.
To achieve encapsulation in Java −
Declare the variables of a class as
private.
Providepublic setter and getter
methods to modify and view the
variables values.
The static keyword in java is used for
memory management mainly. We can apply
java static keyword with variables,
methods. The static keyword belongs to the
class than instance of the class.
The static can be:
• variable (also known as class variable)
• method (also known as class method)
Java static variable
If you declare any variable as static, it is known
static variable.
The static variable can be used to refer the common
property of all objects (that is not unique for each
object) e.g. company name of employees,college
name of students etc.
The static variable gets memory only once in class
area at the time of class loading
Java static method
If you apply static keyword with any
method, it is known as static method.
A static method belongs to the class
rather than object of a class.
A static method can be invoked without
the need for creating an instance of a
class.
static method can access static data
member and can change the value of it.
1.class Calculate{
2. static int cube(int x){
3. return x*x*x;
4. }
5.
6. public static void main(String args[]){
7. int result=Calculate.cube(5);
8. System.out.println(result);
9. }
10.}
1.class Student{
2. int rollno;
3. String name;
4. static String college = "ITS";
5. //static method to change the value of static variabl
e
6. static void change(){
7. college = "BBDIT";
8. }
9. //constructor to initialize the variable
10. Student(int r, String n){
11. rollno = r;
12. name = n;
13. }
14. //method to display values
15. void display(){System.out.println(rollno+" "+name
+" "+college);}
16.}
17.//Test class to create and display the values of objec
t
18.public class TestStaticMethod{
19. public static void main(String args[]){
20. Student.change();//calling change method
21. //creating objects
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is
a reference variable that refers to the current object.
11.}
12.
13.class TestThis2{
14.public static void main(String args[]){
15.Student s1=new Student(111,"ankit",5000f);
16.Student s2=new Student(112,"sumit",6000f);
17.s1.display();
18.s2.display();
19.}}
20.//
Program where this keyword is not required
1.class Student{
2.int rollno;
3.String name;
4.float fee;
5.Student(int r,String n,float f){
6.rollno=r;
7.name=n;
8.fee=f;
9.}
10.void display(){System.out.println(rollno+" "+name+
" "+fee);}
11.}
12.
13.class TestThis3{
14.public static void main(String args[]){
15.Student s1=new Student(111,"ankit",5000f);
16.Student s2=new Student(112,"sumit",6000f);
17.s1.display();
18.s2.display();
19.}}
1.class A{
2.void m(){System.out.println("hello m");}
3.void n(){
4.System.out.println("hello n");
5.//m();//same as this.m()
6.this.m();
7.}
8.}
9.class TestThis4{
10.public static void main(String args[]){
11.A a=new A();
12.a.n();
13.}}
1.class A{
2.A(){System.out.println("hello a");}
3.A(int x){
4.this();
5.System.out.println(x);
6.}
7.}
8.class TestThis5{
9.public static void main(String args[]){
10.A a=new A(10);
11.}}
1.class Student{
2.int rollno;
3.String name,course;
4.float fee;
5.Student(int rollno,String name,String course){
6.this.rollno=rollno;
7.this.name=name;
8.this.course=course;
9.}
10.Student(int rollno,String name,String course,float fee){
11.this(rollno,name,course);//reusing constructor
12.this.fee=fee;
13.}
14.void display(){System.out.println(rollno+" "+name+" "+cour
se+" "+fee);}
15.}
16.class TestThis7{
17.public static void main(String args[]){
18.Student s1=new Student(111,"ankit","java");
19.Student s2=new Student(112,"sumit","java",6000f);
20.s1.display();
21.s2.display();
22.}}
23.// real use this programe
1.class S2{
2. void m(S2 obj){
3. System.out.println("method is invoked");
4. }
5. void p(){
6. m(this);
7. }
8. public static void main(String args[]){
9. S2 s1 = new S2();
10. s1.p();
11. }
12.}
1.class A{
2.A getA(){
3.return this;
4.}
5.void msg(){System.out.println("Hello java
");}
6.}
7.class Test1{
8.public static void main(String args[]){
9.new A().getA().msg();
10.}
11.}