0% found this document useful (0 votes)
35 views30 pages

1-Basics of OOP and Java

Java was created in 1991 at Sun Microsystems to be a platform-independent language. It uses bytecode that can run on any Java Virtual Machine (JVM), allowing programs written in Java to run on any device with a JVM regardless of operating system. Some key features of Java include being object-oriented, robust, secure, portable, high-performance, and dynamically compiled.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
35 views30 pages

1-Basics of OOP and Java

Java was created in 1991 at Sun Microsystems to be a platform-independent language. It uses bytecode that can run on any Java Virtual Machine (JVM), allowing programs written in Java to run on any device with a JVM regardless of operating system. Some key features of Java include being object-oriented, robust, secure, portable, high-performance, and dynamically compiled.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 30

Introduction to Java

• History: Java is based on the two lang. C and C++.


• C lang.(by Dennis Ritchie) is the first high level
programming lang. Structured/Procedure oriented lang.
• C++ lang.(by Bjarne Stroustrup) is the extension of C(‘C’ +
‘OOP’).C++ is partially Object oriented lang.
• Java is by James Gosling, patrick
developed chris
naughton, Ed frank and Mike
sheriden
warth, at Sun Microsystems Inc. in 1991. Its
name was “Oak” at that time. Later in 1995 it is
renamed as Java.
• The primary motivation of java was platform
independence lang. that can be used to create
software (embedded s/w) for consumer
electronics devices.
• Bytecode: The security and portability feature of
java comes from bytecodes. The java
compiler(javac) generate output a virtual
machine(JVM)
for not for the real machine / real
platform. The java interpreter (java) executes the
bytecodes and produce output.
• JVM differs platform to platform but all JVM
understand same bytecode. Thus as long as JVM
is installed all java prog. will run irrespective of
h/w platform.
Application
According to Sun, 3 billion devices run Java. There are
many devices where Java is currently used. Some of
them are as follows:
1.Desktop Applications such as acrobat reader, media
player, antivirus, etc.
2.Web Applications such as irctc.co.in, javatpoint.com,
etc.
3.Enterprise Applications such as banking applications.
4.Mobile
5.Embedded System
6.Smart Card
7.Robotics
8.Games, etc.
•Java Platforms / Editions
•There are 4 platforms or editions of Java:
•1) Java SE (Java Standard Edition)
•It is a Java programming platform. It includes Java
programming APIs such as java.lang, java.io, java.net, java.util,
java.sql, java.math etc

•2) Java EE (Java Enterprise Edition)


•It is an enterprise platform that is mainly used to develop
web and enterprise applications. It is built on top of the Java
SE platform. It includes topics like Servlet, JSP, Web Services,
EJB, JPA, etc.

•3) Java ME (Java Micro Edition)


•It is a micro platform that is dedicated to mobile applications.

•4) JavaFX
•It is used to develop rich internet applications. It uses a
lightweight user interface API
Types of Java Applications
There are mainly 4 types of applications that can be created using Java programming:
1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications.
These are traditional software that we need to install on every machine. Examples of
standalone application are Media player, antivirus, etc.

2) Web Application
An application that runs on the server side and creates a dynamic page is called a
web application.

3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is
called an enterprise application. It has advantages like high-level security, load
balancing, and clustering. In Java, EJB is used for creating enterprise applications.

4) Mobile Application
An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
• The latest version of java is Java SE 18
• J2SE:Java 2 standard edition( JRE, Packages)
• J2EE:Java 2 enterprise edition (J2SE+ JSP, Beans,
Framework, API etc)
• J2ME:Java 2 micro edition(for mobile and
portable devices)
• Java Features
a) Simple h)Interpreted
b) Secure i)High performance
c) Portable j)Distributed
d) Object oriented k)Dynamic
e) Robust
f) Multithreaded
g) Architecture neutral
Simple
• Java is very easy to learn, and its syntax is simple, clean and
easy to understand.

• Java has removed many complicated and rarely-used


features, for example, explicit pointers, operator overloading,
etc.

• There is no need to remove unreferenced objects because


there is an Automatic Garbage Collection in Java.
Platform Independent
• Java code can be executed on multiple platforms, for example,
Windows, Linux, Sun Solaris, Mac/OS, etc.
• Java code is compiled by the compiler and converted into
bytecode.
• This bytecode is a platform-independent code because it can
be run on multiple platforms, i.e., Write Once and Run
Anywhere (WORA).
Object-oriented
• Java is an object-oriented programming language.
Everything in Java is an object. Object-oriented means
we organize our software as a combination of different
types of objects that incorporate both data and
behavior.
Secured
• Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because:
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Bytecode Verifier: It checks the code fragments for illegal code that can
violate access rights to objects.
• Security Manager: It determines what resources a class can access such
as reading and writing to the local disk.
Robust

• Java is robust because:


• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• Java provides automatic garbage collection which runs on the
Java Virtual Machine to get rid of objects which are not being
used by a Java application anymore.
• There are exception handling and the type checking mechanism
in Java. All these points make Java robust.
Architecture-neutral

• Java is architecture neutral because there are no


implementation dependent features,
• for example, the size of primitive types is fixed.

• In C programming, int data type occupies 2 bytes of memory


for 32-bit architecture and 4 bytes of memory for 64-bit
architecture.

• However, it occupies 4 bytes of memory for both 32 and 64-


bit architectures in Java.
• Portable
• Java is portable because it facilitates you to carry the Java
bytecode to any platform. It doesn't require any
implementation.

• High-performance
• Java is faster than other traditional interpreted
programming languages because Java bytecode is "close"
to native code.
• It is still a little bit slower than a compiled language (e.g.,
C++).
• Java is an interpreted language that is why it is slower than
compiled languages, e.g., C, C++, etc.
• Distributed
• Java is distributed because it facilitates users to create
distributed applications in Java.

• RMI and EJB are used for creating distributed applications.

• This feature of Java makes us able to access files by calling


the methods from any machine on the internet.
• Multi-threaded
• A thread is like a separate program, executing
concurrently.
• We can write Java programs that deal with many tasks
at once by defining multiple threads.
• The main advantage of multi-threading is that it
doesn't occupy memory for each thread.
• It shares a common memory area.
• Threads are important for multi-media, Web
applications, etc.
• Dynamic
• Java is a dynamic language. It supports the dynamic
loading of classes. It means classes are loaded on
demand.
• It also supports functions from its native languages, i.e.,
C and C++.
• Java supports dynamic compilation and automatic
•Some basic Java tools
javac: Compiler for Java
java: The launcher for Java applications.
javadoc: API documentation generator.
apt: Annotation processing tool.
appletviewer: Run and debug applets
jar: Create and manage Java Archive (JAR) files.
jdb: The Java Debugger.
javah: C header and stub generator.
javap: Class file disassembler
OOP and
Java
• The two approaches of programming
a) Structured/process-oriented: Like in C. Follow
top down approach.
b) Object Oriented: Like in java. Follow bottom
up approach.
OO
P
• The principle of OOP are
a) Abstraction: Hiding complexity.(object in java, Car as
a object not as a collection of various parts like
engine, break, gear etc).
b) Encapsulation: Binding code and data together and
keep safe from outside interference.(A class in java).
c) Inheritance: Reuse of existing class/code.(More
feature are added in subclass then base class).
d) Polymorphism: Different behavior for different inputs.
(One interface shared by multiple methods).
A simple java
Prog.
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
• Save the file with name Example.java(as the
class name is Example which contains the main
method.)
• Compile the program with
>javac Example.java
This will create a class file Example.class.(The compiler
will create a separate class file for each class. A class
file is nothing but the bytecodes for JVM).
• Execute the program with
>java Example
This will run the code and give the output message.
This is a simple Java program.
• If the program contain more then one class the
file name will be the class name which contain
main() method.
• By doing this we will get the class file name
(Example.class) same as the class name it
contain.( class Example).
• When we execute ‘java Example’, then we are
actually specifying the class name that we want
to execute. So the interpreter will search for a file
classname.class(Example.class)
• Comments: // single line comment
/*
Multiline
comment
*/
/** documentation
comments for javadoc
*/
• ‘class Example’ declares a class with name
Example.
• ‘public static void main(String args[])’ declares
main method.
• public is access specifier, which control the
visibility of class member. When a class member
is preceded by public, then that member may be
accessed by code outside the class in which it is
declared. In this case, main( ) must be declared as
public, since it must be called by code outside of
its class when the program is started.
• The keyword static allows main( ) to be called
without having to instantiate a particular instance
of the class. This is necessary since main( ) is
called by the Java interpreter before any objects
are made.
• The keyword void simply tells the compiler that
main( ) does not return a value
• Java is case-sensitive. Thus, Main is different from
main.(with Main() the program will compile but
on execution give error main() not found).
• In main( ), there is only one parameter. String args[ ]
declares a parameter named args, which is an array of
instances of the class String. (Arrays are collections of
similar objects.)

• System.out.println("This is a simple Java program.");


This line outputs the string “This is a simple Java
program.” followed by a new line on the screen.
Output is actually accomplished by the built-in println(
) method. System is a predefined class that provides
access to the system, and out is the output stream that
is connected to the console. (System class, out object
and println() method.)
• Question
• What if the program contain more then two
main methods.(Error/compile, how to execute
that program).
• Is it necessary to keep the file name same as
class name. If no then how we will execute the
program.
class A{
public static void main(String args[]) {
System.out.println("Hi Main A");
}
}
class B{
public static void main(String args[]) {
System.out.println("Hi Main B");
}
}
• Save with file name ex1.java, compile it. It will generate two files
A.class and B.class.
class Example2 {
public static void main(String args[]) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}

You might also like