Introduction
Introduction
Java Language
JAVA was developed by Sun Microsystems Inc in the year 1991, later acquired by
Oracle Corporation.
programming easy.
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:
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:
• 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
•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
• 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.
• 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
•Java run-time system comes with tools that support multiprocess synchronization
used to construct smoothly interactive systems.
• 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)
Inheritance
Polymorphism
Basic Concepts of OOP-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
Visible Variables
Class
Definition
Encapsulation Example – Account Object
Instance balance()
variables
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.
Bird
Attributes:
Feathers
Lay eggs
Attributes: Attributes:
------------ ------------
------------ ------------
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.
“x:” + x +
“y:” + y);
Indentation Practices
Java is a free form language
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