meljun cortes intro multimedia
Post on 13-Apr-2017
72 Views
Preview:
TRANSCRIPT
JAVA PROGRAMMING
CONCEPTS Prepared by:
Jyr Marie V. Reyes
OBJECTIVES
After completing this unit, you should be able to:
Learn the Java language, how the Java platform works, the Java Development Kit, the Java Runtime Environment and the Garbage Collector.
Know the Structure of a Java Object such as the objects, the package declarations, import statements, class declarations, variables, methods, constructors and non-constructor methods.
Identify Encapsulation, Inheritance and Polymorphism.
2
Pre
pa
red
by: J
yr M
arie
V. R
eyes
THE JAVA LANGUAGE
Like any programming language, the
Java language has its own structure,
syntax rules, and programming
paradigm. The Java language's
programming paradigm is based
on the concept of object-oriented
programming (OOP), which the
language's features support. 3
The Java language is a C-
language derivative, so its syntax
rules look much like C's: for
example, code blocks are
modularized into methods and
delimited by braces ({and }), and
variables are declared before they
are used. 4
THE JAVA LANGUAGE
Structurally, the Java language
starts with packages. A
package is the Java language's
namespace mechanism. Within
packages are classes, and
within classes are methods,
variables, constants, and so
on. 5
THE JAVA LANGUAGE
HOW THE JAVA PLATFORM WORKS
When you write code in the Java
language, like many other languages,
you write source code, then you
compile it; the compiler checks your code
against the syntax rules of the language.
When you compile Java code, you end up
with bytecodes. The Java virtual
machine (JVM) then interprets those
bytecodes at runtime -- that is, when you
tell Java to run a program. 6
HOW THE JAVA PLATFORM WORKS
In file terms, when you write code, you create a
.java file. When you compile that file, the Java
compiler creates a .class file, which contains
bytecodes. The JVM reads and interprets that
file at runtime, and how it does so is based the
platform you're running on. To run on different
platforms, you have to compile your source code
against libraries specific to that platform. As you
might imagine, the promise of Write Once, Run
Anywhere ends up being Write Once, Test
Anywhere. There are subtle or not-so-subtle
platform differences that might make your code
behave differently on different platforms.
7
JAVA DEVELOPMENT KIT
When you download a Java
Development Kit (JDK), you get — in
addition to the compiler and other tools
— a complete class library of prebuilt
utilities that help you accomplish just
about any task common to application
development. The best way to get an
idea of the scope of the JDK packages
and libraries is to check out the JDK
API documentation. 8
JAVA RUNTIME
ENVIRONMENT (JRE)
The Java Runtime Environment (Also
known as the Java runtime) includes the
JVM, code libraries, and components that
are necessary for running programs
written in the Java language. It is
available for multiple platforms. You can
freely redistribute the JRE with your
applications, according to the terms of the
JRE license, to give the application's users
a platform on which to run your software.
The JRE is included in the JDK.
9
THE GARBAGE COLLECTOR
Rather than forcing you to keep up with memory
allocation, the Java platform provides memory
management out of the box. When your Java
application creates an object instance at run time, the
JVM automatically allocates memory space for that
object from the heap, which is a pool of memory set
aside for your program to use. The Java garbage
collector runs in the background, keeping track of
which objects the application no longer needs and
reclaiming memory from them. This approach to
memory handling is called implicit memory
management because it doesn't require you to write
any memory-handling code. Garbage collection is one
of the essential features of Java platform performance.
10
WHAT IS AN OBJECT?
An object is a self-contained entity that
contains attributes and behavior, and
nothing more. Rather than having a data
structure with fields (attributes) and passing
that structure around to all of the program logic
that acts on it (behavior), in an object-
oriented language, data and program logic
are combined. This combination can occur at
vastly different levels of granularity, from fine-
grained objects like a Number, to coarse-
grained objects such as a FundsTransfer service
in a large banking application.
11
A parent object is one that serves as
the structural basis for deriving
more-complex child objects. A child
object looks like its parent but is more
specialized. The object-oriented
paradigm allows you to reuse the
common attributes and behavior of the
parent object, adding to its child objects
attributes and behavior that differ. 12
WHAT IS AN OBJECT?
ENCAPSULATION
Recall that an object is above all discrete, or
self-contained. This is the principle of
encapsulation at work. Hiding is another term
that is sometimes used to express the self-
contained, protected nature of objects.
Regardless of terminology, what's important is
that the object maintains a boundary between
its state and behavior, and the outside world.
Like objects in the real world, objects used in
computer programming have various types of
relationships with different categories of
objects in the applications that use them.
13
ENCAPSULATION
On the Java platform, you can use access
specifiers to vary the nature of object
relationships from public to private. Public
access is wide open, whereas private access
means the object's attributes are accessible only
within the object itself. The public/private
boundary enforces the object-oriented
principle of encapsulation. On the Java
platform, you can vary the strength of that
boundary on an object-by-object basis,
depending on a system of trust. Encapsulation is
a powerful feature of the Java language.
14
INHERITANCE
OOP introduces the concept of inheritance,
whereby specialized objects — without
additional code — can "copy" the attributes
and behavior of the source objects they
specialize. If some of those attributes or
behaviors need to change, then you
simply override them. You only change
what you need to change in order to create
specialized objects. The source object is
called the parent, and the new
specialization is called the child. 15
POLYMORPHISM
Polymorphism is a harder concept to grasp
than encapsulation and inheritance. In
essence, it means that objects that belong to
the same branch of a hierarchy, when sent the
same message (that is, when told to do the
same thing), can manifest that behavior
differently.
16
POLYMORPHISM
Does a Baby "speak" like an Adult? Of course not. A
Baby makes noise, but it's not necessarily
recognizable words like an Adult uses. So, if I
instantiate a Baby object (saying "instantiate a Baby"
means the same thing -- the word "object" is assumed)
and tell it to speak, it might coo or gurgle. One would
hope that an Adult would be coherent. In the
humanity hierarchy, we have Person at the top, with
Baby and Adult beneath it, as subclasses. All People
can speak, so Baby and Adult can, too, but they do it
differently. A Baby gurgles and makes simple
sounds. An Adult says words. That's what
polymorphism is: Objects doing things their own
way.
17
STRUCTURE OF A JAVA
OBJECT
18
PACKAGE DECLARATION
19
IMPORT STATEMENTS
20
IMPORT STATEMENTS
21
CLASS DECLARATION
22
VARIABLES
23
METHODS
24
CONSTRUCTORS
25
NON-CONSTRUCTOR METHODS
26
SOURCES:
Miller, Roy W. Introduction to Java
programming. IBM Developer Works.
Perry, Steven. Introduction to Java
Programming, Part 1: Java Language Basics.
IBM Developer Works.
27
Pre
pa
red
by: J
yr M
arie
V. R
eyes
top related