0% found this document useful (0 votes)
48 views36 pages

Introduction

Java was developed by Sun Microsystems in 1991 and later acquired by Oracle. It is a simple, object-oriented programming language designed to be portable so that code is executed reliably across platforms. The key goals of Java were that it be simple, robust, secure, architecture neutral, high performance, and dynamic.

Uploaded by

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

Introduction

Java was developed by Sun Microsystems in 1991 and later acquired by Oracle. It is a simple, object-oriented programming language designed to be portable so that code is executed reliably across platforms. The key goals of Java were that it be simple, robust, secure, architecture neutral, high performance, and dynamic.

Uploaded by

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

Introduction

Java Language
 JAVA was developed by Sun Microsystems Inc in the year 1991, later acquired by

Oracle Corporation.

 It was developed by James Gosling and Patrick Naughton.

 It is a simple programming language. Java makes writing, compiling, and debugging

programming easy.

 It helps to create reusable code

 A general-purpose programming language made for developers to write once run

anywhere, that is compiled Java code can run on all platforms that support Java.
Java Language
There were five primary goals in the creation of the Java language:

It must be simple, object-oriented, and familiar.

It must be robust and secure.

It must be architecture-neutral and portable.

It must execute with high performance.

It must be interpreted, threaded, and dynamic.


Applications of Java
Desktop GUI Applications - GUI stands for Graphic User Interface, and Java enables GUI development
through resources like Abstract Windowing Toolkit (AWT), JavaFX, and Swings. AWT features several
pre-made components such as button, list, menu, and several third-party components. JavaFX is
composed of a set of graphics and media packages, providing Swing interoperability and 3D graphics
features. Swing is a GUI widget that offers advanced elements such as lists, scroll panes, tables, tabbed
panels, and trees.

Java Mobile Applications - Most of the mobile app developers call Java their official programming
language. Java is compatible with app designing software such as Kotlin and Android Studio. Java
Platform, Micro Edition (alternately called Java ME or J2ME) functions as a cross-platform framework
for creating applications that work across all Java-support devices. Popular Java-based mobile apps:
Netflix, Tinder, Google Earth, Uber

Embedded Systems - A lot of electro mechanical applications use embedded systems like microchips
and mini computers to perform dedicated tasks in a system. Several systems such as SIM cards and
televisions still use Java as their building blocks. Java acts as a core for TV devices and Blu-Ray disc
players .

Java Web-based Applications - Java provides high security and simple coding, two elements that
attract web application developers. Servlets, Struts, and JSPs give effective support to web applications
and offer the potential to develop whatever kind of programs needed. Additionally, open-source e-
commerce platforms like Broadleaf makes designing e-commerce apps a simple task.
Applications of Java

Java Web Servers and Application Servers - The robust Java ecosystem has given rise to
numerous Java web and application servers. Web server space is home to servlets like Apache
Tomcat, Project Jigsaw, and Rimfaxe Web Server (RWS). Meanwhile, servlets like
WebLogic, WebSphere, and JBoss EAP have a firm foothold in the commercial application
server space.

Enterprise Applications – Java is the first choice for developing enterprise programs mainly
because of its powerful features delivering high performance. Apart from performance, Java
also makes applications more powerful, secure, and easily scalable. Java language has the
Java Enterprise Edition (Java EE) platform that comes with API and runtime environment
features for scripting and running enterprise software, network tools, and web services.
According to Oracle, almost 97% of enterprise computers are running on Java. The higher
performance and faster computing provided by Java have resulted in most enterprise
applications to be developed in Java.

Scientific Applications - Java has powerful security and robustness features that make it
popular for developing scientific applications. Java also provides powerful mathematical
calculations that give the same results on different platforms. Some of the most powerful
applications like the MATLAB use Java for interacting user interface as well as part of the
core system.
How Java Changed Internet
• Java innovated a new type of networked program called the APPLET that changed the way the online world thought
about content

• Java also addressed some of the thorniest issues associated with the Internet: portability and security

• Java Applets -An applet is a special kind of Java program that is designed to be transmitted over the Internet and
automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without
further interaction with the user.

• If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser.

• Applets are intended to be small programs. They are typically used to display data provided by the server, handle user
input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server.

• The creation of the applet changed Internet programming because it expanded the universe of objects that can move
about freely in cyberspace.
Security & Portability
• In order for Java to enable applets to be downloaded and executed on the client
computer safely, it was necessary to prevent an applet from launching such an attack.
• Java achieved this protection by confining an applet to the Java execution environment
and not allowing it access to other parts of the computer. The ability to download
applets with confidence that no harm will be done and that no security will be
breached is considered by many to be the single most innovative aspect of Java.
• The same applet must be able to be downloaded and executed by the wide variety of
CPUs, operating systems, and browsers connected to the Internet. It is not practical to
have different versions of the applet for different computers. The same code must
work on all computers.
JDK , JVM,JRE
 JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine
because it doesn't physically exist. It is a specification that provides a runtime
environment in which Java bytecode can be executed.
 JRE is an acronym for Java Runtime Environment. It is a set of software tools which
are used for developing Java applications. It is the implementation of JVM. It
physically exists. It contains a set of libraries + other files that JVM uses at runtime.

• JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications
and applets. It physically exists. It contains JRE + development tools.

• JDK is an implementation of any one of the below given Java Platforms released by
Oracle Corporation:

• Standard Edition Java Platform

• Enterprise Edition Java Platform

• Micro Edition Java Platform


The Byte Code
• JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.

• When you run the Java program, Java compiler first compiles your Java code to byte code. Then, the JVM

translates bytecode into native machine code (set of instructions that a computer's CPU executes directly).

• Java is a platform-independent language. It's because when you write Java code, it's ultimately written for

JVM but not your physical machine (computer). Since JVM executes the Java bytecode, Java is platform-

independent.
Compiling and Executing a Java Program

Appendix A: Introduction to Java 10


Servlets: Java On the server side
• Servlets are the Java programs that run on the Java-enabled web
server or application server. They are used to handle the request
obtained from the webserver, process the request, produce the
response, then send a response back to the webserver.
• Properties of Servlets are : i) Servlets work on the server-side.
ii)Servlets are capable of handling complex requests obtained from
the webserver.
Advantages of Java/Java Buzzwords
• Simple
•Java was designed to be easy for professional programmer to learn and use effectively.
•It’s simple and easy to learn if you already know the basic concepts of Object Oriented
Programming.

•Object oriented
• Almost “Everything is an Object” paradigm. All program code and data reside within
objects and classes.
•The object model in Java is simple and easy to extend.
•Java comes with an extensive set of classes, arranged in packages that can be used in our
programs through inheritance.
Advantages of Java/Java Buzzwords
• Compiled & Interpreted
•Usually a computer language is either compiled or Interpreted. Java combines both this
approach and makes it a two-stage system.
•Compiled : Java enables creation of a cross platform programs by compiling into an
intermediate representation called Java Bytecode.
•Interpreted : Bytecode is then interpreted, which generates machine code that can be
directly executed by the machine that provides a Java Virtual machine.

• Architecture neutral
•Java language and Java Virtual Machine helped in achieving the goal of “write once; run
anywhere, any time, forever.”
•Changes and upgrades in operating systems, processors and system resources will not
force any changes in Java Programs.
Advantages of Java/Java Buzzwords

• Robust & Secure


• It provides many features that make the program execute reliably in a variety of environments.

• Java is a strictly typed language. It checks code both at compile time and runtime.

• Java takes care of all memory management problems with garbage collection.

• Java, with the help of an exception handling, captures all types of serious errors and eliminates any risk of
crashing the system.

• Java provides a “firewall” between a networked application and your computer.

• When a Java Compatible Web browser is used, downloading can be done safely without fear of viral
infection or malicious intent.

• Java achieves this protection by confining a Java program to the Java execution environment and not
allowing it to access other parts of the computer.
Advantages of Java/Java Buzzwords
• Multithreaded & Interactive

•Multithreaded Programs handle multiple tasks simultaneously, which was helpful


in creating interactive, networked programs.

•Java run-time system comes with tools that support multiprocess synchronization
used to construct smoothly interactive systems.

•The multithreaded feature greatly improves the interactive performance of


graphical applications.

• High performance
• Java performance is high because of the use of bytecode.
• The bytecode was used so that it was easily translated into native machine code.
Advantages of Java/Java Buzzwords
• Distributed
•Java is designed for distributed environment of the Internet. Its used for creating
applications on networks.
•Java applications can access remote objects on the Internet as easily as they can do in the
local system.
•Java enables multiple programmers at multiple remote locations to collaborate and work
together on a single project.
•Java is designed for the distributed environment of the Internet because it handles
TCP/IP protocols.
• Dynamic & Extensible code
• Java is capable of linking in new class libraries, methods, and objects.
• Java programs carry with them substantial amounts of run-time type information that is used to verify and
resolve accesses to objects at runtime. This makes it possible to dynamically link code in a safe and
expedient manner.
Object Oriented Programming
There are mainly two approaches
• Procedure oriented Programming(POP)
• Object Oriented Programming (OOP)

Examples of POP – C, COBOL, PASCAL, FORTRAN

Examples of OOP – C++, JAVA, PYTHON, C#

Basic difference in the two approaches is dictated by:


The way the problem is handled.
In POP problem to be solved is broken into a number of distinct functions.
In OOP the problem to be solved is broken into a number of distinct objects
POP Versus OOP
Three Object Oriented Programming Principles
 Encapsulation

 Inheritance

 Polymorphism
Basic Concepts of OOP-Encapsulation

 The wrapping up of data and functions into a single unit is known as


encapsulation.

 The data is not accessible to the outside world, only those function which
are wrapped in the class can access it.

 These functions provide the interface between the object’s data and the program.

 This insulation of the data from direct access by the program is called data hiding or
information hiding.

 The combination of collecting all the attributes of an object into a single class
definition, combined with the ability to hide some definitions and type
information within the class, is known as encapsulation.
Encapsulation

 The variables and methods are Visible Methods


accessible from within the class, but Hidden
not accessible outside it. State
Variables
and
Methods

Visible Variables

Class
Definition
Encapsulation Example – Account Object

Instance balance()
variables

withdraw() theBalance deposit()


acctNumber

Methods
accountNumber()

State variables/Instance variables make up the nucleus of the object. Methods surround and
hide (encapsulate) the state variables from the rest of the program.
Object Oriented principle - INHERITANCE
 Inheritance is the process by which objects of one class acquire the properties of objects of another class.

 In OOP, the concept of inheritance provides the idea of reusability. This means we can add
additional features to an existing class without modifying it.
 Each derived class shares common characteristics with the class from which it is derived.
 The advantage of making a new class a subclass is that it will inherit attributes and methods of its parent
class (also called the superclass).
 Subclasses extend existing classes in three ways:
By defining new (additional) attributes and methods.
By overriding (changing the behavior) existing attributes and methods.

By hiding existing attributes and methods.


INHERITANCE EXAMPLE

Bird
Attributes:
Feathers
Lay eggs

Flying Bird Non-flying Bird

Attributes: Attributes:
------------ ------------
------------ ------------

Robin Swallow Penguin Kiwi


Attributes: Attributes: Attributes: Attributes:
------------ ------------ ------------ ------------
------------ ------------ ------------ ------------
Object Oriented principle - Polymorphism
• Polymorphism, a Greek term means to ability to take more than one form.
• An operation may exhibits different behaviors in different instances. The behavior depends upon the type of
data used in the operation.
• For example consider the operation of addition for two numbers; the operation will generate a sum. If the
operands are string then the operation would produce a third string by concatenation.
• The process of making an operator to exhibit different behavior in different instances is known operator
overloading
First program in Java

/* 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.");
}
}

Steps for execution:


1. Enter the program
2. Compile the program using javac Example.java
3. Run the program using the command java Example
Second program in Java

/* This is a simple Java program.


Call this file "Example2.java". Like other Languages java
*/ support
class Example2 { Arithmetic operators
public static void main(String args[])
+, -,*, /, %
{ int v1;
int v2; Relational operators
v1=1024; <, <=, >, >=, ==, !=
System.out.println(“v1 contains:“ + v1);
v2=v1/2;
System.out.println(“v2 contains:“ + v2);
}
}

Output:
v1 contains:1024
v2 contains:512
Exercise – Convert Gallons to Liters using formula 1 gallon=3.7854 liters

class Galtolit {
public static void main(String args[])
{ double g,l;
g=10;
l= g*3.7854;
System.out.println(g+“ gallons is“ + l+”liters”);
}
}
Output:
10 gallons is 37.8540 liters
Two Control Statements
 If Statement syntax:
Simple form:
If (condition) statement;
Form 2:
If (condition)
{// code block
}
 For Statement Syntax:
for(initialization; termination; increment)
{
//body of the for loop(Code block)
}
Code Blocks in Java
Lexical Issues
Java programs are a collection of whitespace, identifiers, literals, comments, operators, separators, and keywords.
White Space - In Java, whitespace is a space, tab, or newline
Identifiers - Identifiers are used to name things, such as classes, variables, and methods. An identifier may be any
descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. (The
dollar-sign character is not intended for general use.)
They must not begin with a number, lest they be confused with a numeric literal.
Again, Java is case-sensitive, so VALUE is a different identifier than Value.
Some examples of valid identifiers are
AvgTemp
count
a4
$test
this_is_ok
Invalid identifier names include these:
2count
high-temp
Not/ok
NOTE Beginning with JDK 8, the use of an underscore by itself as an identifier is not recommended
Lexical Issues
Java programs are a collection of whitespace, identifiers, literals, comments, operators,
separators, and keywords.
Literals
A constant value in Java is created by using a literal representation of it.
For example, here are some literals: 100 98.6 ‘X’ “This is a test”
Left to right, the first literal specifies an integer, the next is a floating-point value, the
third is a character constant, and the last is a string.
A literal can be used anywhere a value of its type is allowed.

Comments There are three types of comments defined by Java.


single-line and multiline. The third type is called a documentation comment.
This type of comment is used to produce an HTML file that documents your program.
documentation comment begins with a /** and ends with a */.
Lexical Issues
Java programs are a collection of whitespace, identifiers, literals, comments, operators,
separators, and keywords.
Separators
Semicolons & Positioning
 ; is a separator that terminates a statement

 A code block shouldn’t be terminated with a semicolon.

 Java doesn’t recognize \n(end of line ) as terminator.

 So multiple statements can be combined on same line like:

x=y; y=y+10; System.out.println(x+” “+y);

 A single long line can also be written on multiple lines like:

System.out.println(“This is long line” +

“x:” + x +

“y:” + y);
Indentation Practices
 Java is a free form language

 Proper indentation style helps the code more readable

 It becomes easy to recognize, debug and refine the code if it is indented properly

 Indent your code using the following rule. Most structures in Java start and end with curly braces { } (classes, methods, loops,
conditional statements, etc). Everything within a set of curly braces should be indented one level further than the curly braces were.
For example a class has no indentation, a method in a class has 1 level of indentation, a loop in a method in a class has 2 levels of
indentation and so on. A level of indentation is usually 3 or 4 spaces

public class BeautifulIndentationExample


{
public static void beautifullyIndentedMethod()
{
for (int i = 0; i < 5; i++)
{ if (i % 2 == 0)
System.out.println(“Inside 4 braces, Indented 12 spaces”);
}
}
}
Java Keywords
Keywords or Reserved words are the words in a language that are used for some
internal process or represent some predefined actions. These words are therefore
not allowed to use as a variable names or objects.

You might also like