Download as pdf or txt
Download as pdf or txt
You are on page 1of 74

Java Programming

Fundamentals
JOHN PAUL M. FONTAMILLAS
Lecturer
Key Skills & Concepts
• Know the history and philosophy of Java
• Understand Java’s contribution to the
Internet
• Understand the importance of bytecode
• Know the Java buzzwords
• Understand the foundational principles
of object-oriented programming
• Create, compile, and run a simple Java
program
Key Skills & Concepts
• Use variables
• Use the if and for control statements
• Create blocks of code
• Understand how statements are
positioned, indented, and terminated
• Know the Java keywords
• Understand the rules for Java
identifiers
THE ORIGIN
OF JAVA
The Origins of Java

• Java was conceived by James Gosling,


Patrick Naughton, Chris Warth, Ed Frank,
and Mike Sheridan at Sun Microsystems
in 1991. This language was initially
called “Oak” but was renamed “Java” in
1995.
The Origins of Java
• Somewhat surprisingly, the original
impetus for Java was not the Internet!
Instead, the primary motivation was
the need for a platform-independent
language that could be used to create
software to be embedded in various
consumer electronic devices, such as
toasters, microwave ovens, and remote
controls. As you can probably guess,
many different types of CPUs are used
as controllers.
JAVA’s LINEAGE:
C and C++
Java’s Lineage: C and C++
• The two languages that form Java’s
closest ancestors are C and C++. As you
may know, C and C++ are among the
most important computer languages
ever invented, and are still in
widespread use today. From C, Java
inherits its syntax. Java’s object model
is adapted from C++.
Java’s Lineage: C and C++
• Java’s relationship to C and C++ is
important for a number of reasons.
• First, at the time of Java’s creation,
many programmers were familiar with
the C/C++ syntax.
• Because Java uses a similar syntax, it
was relatively easy for a C/C++
programmer to learn Java.
SYNTAX

• This made it possible for Java to be


readily utilized by the pool of existing
programmers, thus facilitating Java’s
acceptance by the programming
community.
Java’s Lineage: C and C++
• Second, Java’s designers did not
“reinvent the wheel.”

• Instead, they further refined an already


highly successful programming
paradigm. The modern age of
programming began with C. It moved to
C++, and then to Java.
Java’s Lineage: C and C++
• One last point: although C++ and Java are
related, especially in their support for
object-oriented programming, Java is not
simply the “Internet version of C++.”

• Java has significant practical and


philosophical differences from C++.
Furthermore, Java is not an enhanced
version of C++. For example, it is neither
upwardly nor downwardly compatible with
C++.
Java’s Lineage: C and C++
• Moreover, Java was not designed to replace
C++. Java was designed to solve a certain
set of problems. C++ was designed to solve a
different set of problems.
HOW JAVA IMPACTED
THE INTERNET
A. Java Simplified Web-Based Programming

• Java simplified Web-based


programming in a number of ways.
Arguably the most important is found in
its ability to create portable, cross-
platform programs.
• Of nearly equal importance is Java’s
support for networking. Its library of
ready-to-use functionality enabled
programmers to easily write programs
that accessed or made use of the
Internet.
B. Security
• As desirable as dynamic, networked
programs are, they also present serious
problems in the areas of security and
portability. A program that downloads
and executes automatically on the
client computer must be prevented
from doing harm.
B. Security
• It must also be able to run in a variety
of different environments and under
different operating systems. As you will
see, Java addressed these problems in
an effective and elegant way.
B. Security
• Java achieved this protection by
enabling you to confine an application
to the Java execution environment and
prevent it from accessing other parts of
the computer. (You will see how this is
accomplished shortly.) The ability to
download an application with a high
level of confidence that no harm will
be done contributed significantly to
Java’s early success.
C. Portability
• Portability is a major aspect of the
Internet because there are many
different types of computers and
operating systems connected to it.
• If a Java program were to be run on
virtually any computer connected to
the Internet, there needed to be some
way to enable that program to execute
on different types of systems.
C. Portability
• In other words, a mechanism that
allows the same application to be
downloaded and executed by a wide
variety of CPUs, operating systems, and
browsers is required. It is not practical
to have different versions of the same
application for different computers.
The same code must work in all
computers.
C. Portability
• Therefore, some means of generating
portable executable code was needed.
As you will soon see, the same
mechanism that helps ensure security
also helps create portability.
JAVA’s MAGIC:
THE BYTECODE
Java’s Magic: The Bytecode
• The key that allows Java to address
both the security and the portability
problems just described is that the
output of a Java compiler is not
executable code. Rather, it is
bytecode.
• Bytecode is a highly optimized set of
instructions designed to be executed by
the Java run-time system, which is
called the Java Virtual Machine (JVM).
Java’s Magic: The Bytecode
• Translating a Java program into
bytecode makes it much easier to run a
program in a wide variety of
environments because only the JVM
needs to be implemented for each
platform.
OBJECT-ORIENTED
PROGRAMMING
Object-Oriented Programming
• At the center of Java is object-oriented
programming (OOP).

• The object-oriented methodology is


inseparable from Java, and all Java
programs are, to at least some extent,
object-oriented.
Object-Oriented Programming
• OOP is a powerful way to approach the
job of programming. Programming
methodologies have changed
dramatically since the invention of the
computer, primarily to accommodate
the increasing complexity of programs.
A. Encapsulation
• Encapsulation is a programming
mechanism that binds together code
and the data it manipulates, and that
keeps both safe from outside
interference and misuse.
• In an object-oriented language, code
and data can be bound together in such
a way that a self-contained black box is
created.
A. Encapsulation
• Within the box are all necessary data
and code. When code and data are
linked together in this fashion, an
object is created. In other words, an
object is the device that supports
encapsulation.

• Within an object, code, data, or both


may be private to that object or
public.
A. Encapsulation
• Private code or data is known to and
accessible by only another part of the
object.

• That is, private code or data cannot be


accessed by a piece of the program
that exists outside the object.
A. Encapsulation
• When code or data is public, other
parts of your program can access it
even though it is defined within an
object.
• Typically, the public parts of an object
are used to provide a controlled
interface to the private elements of
the object.
B. Polymorphism
• Polymorphism (from Greek, meaning
“many forms”) is the quality that
allows one interface to access a
general class of actions.

• The specific action is determined by


the exact nature of the situation.
B. Polymorphism
• A simple example of polymorphism is
found in the steering wheel of an
automobile.

• The steering wheel (i.e., the interface)


is the same no matter what type of
actual steering mechanism is used.
B. Polymorphism
• The same principle can also apply to
programming.

• For example, consider a stack (which is


a first-in, last-out list). You might have
a program that requires three different
types of stacks.
B. Polymorphism
• Polymorphism helps reduce complexity
by allowing the same interface to be
used to specify a general class of
action.

• It is the compiler’s job to select the


specific action (i.e., method) as it
applies to each situation.
B. Polymorphism
C. Inheritance
• Inheritance is the process by which one
object can acquire the properties of
another object.

• This is important because it supports


the concept of hierarchical
classification.
C. Inheritance
• For example, a Red Delicious apple is
part of the classification apple, which
in turn is of the fruit class, which is
under the larger class food.

• That is, the food class possesses certain


qualities (edible, nutritious, etc.)
which also, logically, apply to its
subclass, fruit.
C. Inheritance
• Without the use of hierarchies, each
object would have to explicitly define
all of its characteristics.

• Using inheritance, an object need only


define those qualities that make it
unique within its class.
C. Inheritance
• It can inherit its general attributes
from its parent.

• Thus, it is the inheritance mechanism


that makes it possible for one object to
be a specific instance of a more
general case.
JAVA – BASIC
SYNTAX
Java - Basic Syntax

• A basic Java program can be broken down


into several constructs and elements.
Typically, it can be characterized as a
collection of objects, which communicate
with each other by calling each other’s
routines. The basic definitions of objects
and classes are:
A. Class

• A class can be described as a blueprint that


portrays the practices/expresses all the
behaviors and states of its objects.
B. Objects
• Objects are characterized by two components
namely, methods and attributes or variables. For
instance, if you consider the example of a puppy,
then it has the following attributes or states:
name, color and breed. Create an object called
"myObj" and print the value of x:
C. Methods
• A method is basically a behavior. A class can cont
ain many methods. It is in methods where the log
ics are written, data is manipulated and all the ac
tions are executed.
C. Methods
• A method must be declared within a class. It is
defined with the name of the method, followed
by parentheses (). Java provides some pre-
defined methods, such as System.out.println(),
but you can also create your own methods to
perform certain actions:
C. Instance Variables
• Each object has its unique set of instance
variable.
An object's state is created by the values
assigned to these instance variables.
D. Class Names
• For all class names, the first letter ought to
be in Upper Case. On the off chance that
few words are utilized to structure a name
of the class, every internal word’s first letter
ought to be in Upper Case. For example, a
standard class name is as follows: class
Sampleclass
E. Case Sensitivity

Java is case sensitive, which implies that the


identifier Hi and hi would have distinctive
importance in Java.
F. Method Names
All system names ought to begin with a Lower Case
letter. In the event that few words are utilized to
structure the name of the method, then every
internal word’s first letter ought to be in Upper
Case. An example of this convention is follows:
public void mysamplemethod ()
G. Filename
The name of the system record ought to precisely
match the class name.
At the point when you are saving the file, you
ought to save it utilizing the class name.
Remember Java is case touchy and affix “.java” to
the end of the name.
If the document name and the class name don’t
match your system won’t assemble. Consider the
example of a class name Sample. In this case, you
must name the file as sample.java.
H. public static void main (string args[])
• Java system handling begins from the
main() function, which is a required piece of
each Java program.
JAVA IDENTIFIER
Java Identifier
All Java components require names. Names
utilized for classes, variables and strategies
are called identifiers. In Java, there are a few
focuses to recall about identifiers. They are
as per the following standard:
Java Identifier
• All identifiers ought to start with a letter (beginning to end
or a to z), underscore
• (_) or special character ($).
• After the first character, identifiers can have any mix of
characters.
• You cannot use a keyword as an identifier.
• Most significantly, identifiers are case sensitive. So, Sample
is not same as sample.
• Examples of identifiers include $salary, age, __1_value and
_value.
• Examples of illicit identifiers include –compensation and
123abc.
JAVA MODIFIERS
Java Modifiers

Like is the case with any programming


language, it is conceivable to alter classes
and systems by utilizing modifiers. There are
two classifications of modifiers:

• Access Modifiers: public, default, protected


and private
• Non-access Modifiers: strictfp, final and
abstract
JAVA VARIABLES
Java Variables
Several types of variables are supported by
Java.
These types of variables include:

• Instance Variables (Non-static variables)


• Class Variables (Static Variables)
• Local Variables
Java Variables
IDE

JOHN PAUL M. FONTAMILLAS


Lecturer
ECLIPSE
Eclipse
• Eclipse is an integrated development
environment (IDE) used in computer
programming.

• It contains a base workspace and an


extensible plug-in system for
customizing the environment.
Eclipse

• Eclipse is written mostly in Java and its


primary use is for developing Java
applications.

• It may also be used to develop


applications in other programming
languages via plug-ins,
Eclipse

• Including Ada, ABAP, C, C++, C#, Clojur


e, COBOL, D, Erlang, Fortran, Groovy, H
askell, JavaScript, Julia, Lasso, Lua, NA
TURAL, Perl, PHP, Prolog, Python, R, Ru
by (including Ruby on
Rails framework), Rust, Scala,
and Scheme
Eclipse

• It can also be used to develop


documents with LaTeX (via a TeXlipse
plug-in) and packages for the
software Mathematica. Development
environments include the Eclipse Java
development tools (JDT) for Java and
Scala, Eclipse CDT for C/C++, and
Eclipse PDT for PHP, among others.
Integrated Development
Environment (IDE)
• An Integrated development
environment (IDE) is a software
application that provides comprehensive
facilities to computer
programmers for software development. An
IDE normally consists of at least a source
code editor, build automation tools and
a debugger. Some IDEs, such
as NetBeans and Eclipse, contain the
necessary compiler and interpreter.
Software Development Kit (SDK)
• A software development kit (SDK) is a
collection of software
development tools in one installable
package.
• They facilitate the creation
of applications by having compiler,
debugger and perhaps a software
framework. They are normally specific
to a hardware platform and operating
system combination.
Software Development Kit (SDK)

• To create applications with advanced


functionalities such as
advertisements, push
notifications, etc; most application
software developers use specific
software development kits.
Software Development Kit (SDK)

• Some SDKs are required for developing


a platform-specific app. For example,
the development of an Android app on
the Java platform requires a Java
Development Kit.
• For iOS applications (apps) the iOS
SDK is required. For Universal Windows
Platform the .NET Framework
SDK might be used.
Java Development Kit(JDK)

• The Java Development Kit (JDK) is an


implementation of either one of
the Java Platform, Standard
Edition, Java Platform, Enterprise
Edition, or Java Platform, Micro
Edition platforms released by Oracle
Corporation in the form of a binary
product aimed at Java developers
on Solaris, Linux, macOS or Windows.
Java Development Kit(JDK)

• The JDK includes a private JVM and a


few other resources to finish the
development of a Java application.

• The JDK also comes with a


complete Java Runtime Environment,
usually called a private runtime, due to
the fact that it is separated from the
"regular" JRE and has extra contents.
Source:

Java For Beginners_A Simple Start To Java Programming: Scott


Sanderson 2016
Thank You

You might also like