Notes (JAVA)
C++
è
Partially OOP
Java
è
Fully OOP
Principles
of OOP:
1) Object
– Definitions:
(a)
Entity
which has attributes and behavior
(b) Entity
which occupies space in memory
For e.g. a marker is an
object. Its attributes include –
shape, color, size etc.
And its behavior is writing on board.
2) Class
– Definitions:
(a) Class
is a container which contains similar types of objects.
(b) Class
is a container which contains objects and methods.
3) Encapsulation
– Definition: Wrapping up of data into single unit that can be carried easily.
Benefit: Carrying data becomes easy and
data can be used wherever/whenever we want to.
For e.g. we can carry many things in a bag like
laptop, charger, books etc.
In
case of programming, classes can be considered as an example of encapsulation.
4) Data
abstraction – Definition: Using the
essential features without including the background details.
Benefit: Data hiding. No need to tell others how
something works. They can simply use it without knowing the details.
5) Polymorphism
– Definition: Using a function in many different ways.
Benefit: Reduces time
and increase the speed of compiler.
Types:
a) Compile
time – e.g. function overloading,
operator overloading.
b) Runtime
- Virtual func. à Function with same name,
return-type and parameters but diff. class. At this time, compiler can see only
the child class’s functions but not the parent class. In order to use the
functions of parent class, we use virtual functions.
6) Inheritance
– Definition: Using the properties/attributes of one class by another class.
Benefit: Reusability
Drawback:
Time consuming. This problem was solved by Java using interfaces and through
abstract classes.
7) Dynamic binding
– Execution of main function.
8) Message
communication
When
objects communicate with each other with the help of functions, the process is
known as message communication. For e.g. this concept is used in RMI (Remote Method Invocation), networking
etc.
Language – Mode of communication
The
first requirement for learning any language is – TOKEN.
Token
– Smallest individual unit of a program.
Types:
1. Identifier
– Name of data, method, class, interface etc.
2. Keyword – Reserved / Predefined words
having specific meaning.
These may be different for different
languages.
3. Operator – Symbols that perform action
on data.
Types:
a) Unary
b) Binary
4. Separator – Symbols used to separate
statements.
For e.g. semicolons, {}, (), [], comma etc.
5. Literal – Constant values.
For e.g. integers, float, strings etc.
Statements
– Combination of tokens
Functions
– Group of statements
Programs
– Group of functions
Softwares
– Group of programs
Machine code depends on the machine i.e.
a machine code obtained after compilation on one machine will not necessary
work on another. It is machine dependent.
In order to solve this problem, an
intermediate code is used that can be shared among machines and further
compiled to obtain the required machine code.
C++
à
Machine dependent
Java
à
Platform independent
JAVA
Java is a pure OOP language created by Sun Microsystems. It was developed by James Gosling. It was developed in
1992.
Current Version of Java
– 1.7
Java provides diff. kinds of features
(explained ahead):
1. Platform
independent and portable
2. Dynamic
and Extensible
3. Multithreaded
application and Interactive
4. Compiled
and Interpreted
5. Object
Oriented
6. High
performance
7. Distributed
Format of Java program
import java.io.*; //
importing package
class demo
{
public
static void main(String aa[])
{
System.out.println(“Hello, How are you?”);
}
}
Package
– Package is a container in which we have predefined classes, sub-classes and
methods. We can use package in a program with the help of an import statement.
import
java.io.*;
*
indicates all classes and subclasses
System.out.println is a method of output
stream class where:
·
System.out indicates
“output stream class”
·
out – object
·
println – method
Note:
print/println can be used in 19 ways. This is an example of polymorphism.
Installation of Java
Two
files are essential for installation:
·
java
·
javac
These are kept in bin folder where java
is installed i.e. java files are kept.
After installation of JDK, we need to
give their path for compilation and execution of .java files by command prompt.
Giving path
·
Open System Properties
by right clicking on my computer and choosing properties
·
Go to advanced system
properties
·
Choose Environment
variables
·
Click New to create new
variable
·
Define Variable Name:
PATH and Variable value: (add path of java and javac i.e. path of bin separated
by semi-colon). E.g.
Variable
Name: PATH
Variable
value: C:\Program Files
(x86)\Java\jdk1.5.0_06\bin; C:\Program Files (x86)\Java\jdk1.5.0_06\bin
Compiling and Executing
After writing java program in Notepad,
save the file anywhere in the drive where java is installed with .java extension.
In command prompt:
Compilation:
Select the path where java file is
stored.
Type: javac filename.java and enter
Note:
In order to compile all the java files together à
Type: javac *.java
Execution:
Type: java classname and enter
These type of applications created in
command prompt are known as Console
Applications or Console based or Character based applications.
In case, we want have saved different
classes in different files:
For example, 3 classes are saved as 3
different files:
1. class demo1
{
public void show()
{
System.out.println(“Show”);
}
}
2. class demo2
{
public void show()
{
System.out.println(“Show”);
}
}
3. class maindemo
{
public
static void main(String aa[])
{
demo1 d = new demo1();
demo2 d1 = new demo2();
d.show();
d1.show();
}
}
Now if we want to compile the class
maindemo, we need to compile both the above classes before that else the
compiler will show an error.
Note:
If we make any changes in a program, we need to compile it again.
Features of Java
1. Compiled
and Interpreted
Computer language is
either compiled or interpreted. Java combines both these approaches. Java has 2
stage system:
·
Java compiler
translates source code into byte code instruction. Byte code is not machine
code.
·
Then, Java interpreter
generates machine code from byte code that can be directly run on machine.
2. Platform
independent and portable
Java program can be
easily moved from one computer system to another anywhere and anytime. Changes
and updates in OS, processors and system resources will not force any changes
in java program.
3. Object
oriented
Java is a true object
oriented programming language. Almost everything in java is object. All program
codes and data reside in objects and classes.
e.g.
In this declaration:
int a; à
This can also be written as:
Integer
a = new Integer();
That’s why, a can be
considered as an object of Integer class.
4. Distributed
Java is designed as
distributed language for creating applications on network. It has the ability
to share both data and program. Java applications can access remote objects on
the internet as well as they can be accessed on local system.
e.g. Java supports RMI
(Remote Method Invocation).
5. Multithreaded
and interactive
Multithreading refers
to switching between processes.
Benefit: Multithreading
means handling multiple tasks simultaneously.
Java supports
multithreaded programs. This means that we need not to wait for the application
to finish the task before beginning another.
6. High
performance
High performance is impressive for an interpreted
language. Java is faster than native languages like C, C++ etc. Its
architecture is also designed to reduce overhead during the runtime.
7. Dynamic
and extensible
Java programs support
functions written in other languages such as C, C++ etc. These functions are
known as native methods.
Java is also a dynamic
language. It is capable of dynamically linking new class libraries and methods.
This facility enables the programmer to use the efficient functions available
in languages.
Applications
in Java
Note:
Applications are of two types:
·
CUI (Character User
Interface) – Less user interactive (drawback)
·
GUI (Graphical User
Interface)
We can create 2 types
of applications in Java:
1.
Standalone
applications
Standalone applications
are those programs written in java to carry out certain tasks on a standalone
local computer. We have to perform 2 steps to run these kinds of applications:
a. Compiling
source code into byte code using javac compiler
b. Executing
the java programs using interpreter
2.
Applets
Applets are small
programs developed for internet applications. They use HTML, that’s why, they
can run on internet. These are capable of running on web-browsers and applet
viewer.
No comments:
Post a Comment