Vous êtes sur la page 1sur 7

Java Basics: Object Oriented Programming

Java Basics:
O b j e c t O r i e n t e d P ro g r a m m i n g
Tutorial Document created by Brandon Perkins

In this section:

 Legal information
 What is Java?
 Ethical Values: Professional and Personal
 Basic Java Elements

Legal Information

The information on this document is considered common knowledge within the community of
computer programmers and engineers, in businesses, education, and to hobbyists at home. The
information on this document can be found anywhere, but like other advanced programmers
(career or hobby), I want to try to make it as easy to understand as possible for those that are
interested in learning and for those who have already started but need a push in the right direction.
Although this document was created by me, all of the information that I share has been obtained
through school education and self-exploration. Although the information is considered common
knowledge, I would like to make a few acknowledgements to those I have to thank for giving me
the skills I need to help me study for my future career as a computer engineer:

• Mrs. Elizabeth Haskins, my AP Computer Science teacher


at Oscar Smith High School
• Oracle, who provides more advanced tutorials, at their
site www.oracle.com

A few more legal formalities that I would like to get out of the way. The JAVA
language originated and is owned by Sun Microsystems. Also the compiler
that may be used and referenced in these documents is called JCreator.
JCreator software was developed and is owned by Xinox Software, a company
based in Deft, Netherlands. For more information, the following links are
www.jcreator.com and www.sun.com/java/.

I am not fond of legal formalities like this, and although it’s considered
common knowledge, this is considered a moral ethical choice which will be
covered in this document as well. If someone feels that I have left something
out important regarding the ‘Legal Information’ section, then please contact

Page 1
and inform me so that I may make the appropriate changes. I check my email
as often as I can so I will most likely see any notifications sent. My email is
ingeniare91@gmail.com. Only email me if it is about these documents.

What is JAVA?

Java is a simple but powerful object oriented programming


language and in many respects similar to C++. Unlike other
programming languages, including C/C++, when Java is compiled
it is not compiled into platform specific machine, but into platform
independent byte code. Java was meant to follow the concept
‘design once and run everywhere’. That is where the environment
Java Virtual Machine (JVM) comes into play. The JVM is the
environment in which Java programs execute. It is software that is
implemented on top of real hardware and operating system. The
JVM must be implemented on a particular platform before
compiled programs can run on that platform. I am not going to
go into detail about JVM, but if you would like to know specifically
how it works, then go to www.oracle.com to find more information
on JVM.

Java as a programming language was designed to work based on


the following features:

• Reusability of code
• Focus on data rather than procedure
• Simple, secure, and robust use of code
• Portable
• Dynamic
• Data hidden and protected from external functions
• Etc., www.oracle.com to find more about its features

Object Oriented Programming is a method of implementation in which


programs are organized as cooperative collection of objects, each of which
represents an instance of a class, and whose classes are all members of a
hierarchy of classes united via inheritance relationships. (textbook definition)
(source: Java Software Solutions, AP Version / Edition 2 by John Lewis, William
Loftus, William Loftus, Cara Cocking, Cara Cocking)

Page 2
Java Basics: Object Oriented Programming
Four Principles of OOP

• Abstraction - Abstraction denotes the essential


characteristics of an object that distinguish it from all
other kinds of objects and thus provide crisply defined
conceptual boundaries, relative to the perspective of the
viewer.
• Encapsulation - Encapsulation is the process of
compartmentalizing the elements of an abstraction that
constitute its structure and behavior; encapsulation
serves to separate the contractual interface of an
abstraction and its implementation.

o * Hides the implementation details of a class.


o * Forces the user to use an interface to access
data
o * Makes the code more maintainable.
• Inheritance - Inheritance is the process by which one
object acquires the properties of another object.
• Polymorphism - Polymorphism is the existence of the
classes or methods in different forms or single name
denoting different implementations.

(source: Java Software Solutions, AP Version / Edition 2 by John Lewis, William


Loftus, William Loftus, Cara Cocking, Cara Cocking)

All of this information is better explained as you learn more about the
language and learn how to do more with your programs. All of this
information comes from the textbook named in the sources given that was
provided as study notes given to me from my AP Computer Science teacher. It
is an old book and not considered up to date with the Java language’s current
version.

Ethical Values: Professional and Personal

Remember the legal section at the beginning of this document. Even though
all the information in this document is considered for the most part to be
common knowledge (comes from more than one source or is a commonly
known fact), it is considered ethical to still give credit and recognition where
it’s due. Same goes with computer programming or engineering. When a
group of people has been asked to work on a project, it’s ethical to do your
part and not let everyone else do all the work. It’s also ethical to give credit

Page 3
where it’s due, everyone who does their part would be given proper credit for
the work.

What are ethics? There is always confusion between what is ethical and what
is legal. Simply put they are two different things which I will illustrate below:

Legal Ethical

Legal is when someone is deciding whether to rob a store or not. This is


obviously an illegal action. It is also considered unethical because it is morally
wrong.

Here is another scenario: A company manufactures batteries. Under one of


the production lines they have a 1 in every 150 failure rate, and they develop
about 90,000 batteries a year. Making a battery costs about $7.00 and testing
them costs about $3.00. The manager of the production line wants to just ship
the batteries out without testing them to save money.

Here is what to consider in this situation:


1. Who would be affected by taking this action?
2. How would they be affected?
3. Would this be considered ethical? Legal?

Plenty of people would be affected by this action. The stores and their
customers who buy them could be using the batteries for trivial uses or for
something life changing (like smoke detectors). The employees of the
production line could get in a lot of trouble from this and may lose their job.
The manager who took this action and possibly even the company could get
into trouble as well. Pretend that it was something else instead of batteries,
something more important. The consequences could be worse.

Page 4
Java Basics: Object Oriented Programming
This action would most likely not be illegal since there are not actually any
laws that can regulate this. It is however unethical, because defective products
are knowingly being shipped out and sold to unsuspecting customers.

Ethical situations have many different viewpoints as to what is ethical or


unethical. Many professions and companies create guidelines to better define
what exactly is ethical and what is unethical to their line of work. Whenever
you are presented with situation like this, always think it through to decide
what YOU think is the best ethical course of action.

Basic Java Elements

Java is for the most part, a user-defined language. Objects, data variables,
classes, and more can be named and defined by the programmer. However,
there are special key elements that must be used to help define them.

Reserved words are unique java terms that help tell the compiler how it should
interpret the lines of code in a program. They cannot be used as names of
data variables, classes, objects, and so on. They are case sensitive and are
predefined in the java language. Most of these are used more than others,
and some may hardly be used at all.

abstract assert boolean break byte

case catch char class const

continue default do double

else enum extends false final

finally float for goto

if implements import instanceof int

interface long native new null

package private protected public

return short static strictfp super

switch synchronized this throw

throws transient true try void

volatile while

Comments are placed by the programmer to describe what certain lines of


code in a program do. A programmer would place them in the program to

Page 5
remind him/her what the code, should he/she may need to go back to it for
reference. It also can be placed if the code would be shared with other
programmers and they need to understand what the code is doing.
Comments have no effect on the program itself; it is simply there. This is what
a comment may look like:

//a comment (spans one line) (more commonly used)

/* a comment */ (spans multiple lines) (more commonly used)

/** a comment */ (spans multiple lines, generally used for documention)

Data variables are used to store data that may change throughout the run of a
program. Variables have names given by the programmers, known as
identifiers, and should be unique to the program that the variables are used
for. Variables also have data types which describes what kind of data it stores.
Java has four main primitive data types: Integer, Boolean, Floating Point, and
Character. This table shows default values for built in java data types:

Data Type Default Value (for fields) Range

byte 0 -127 to +128

short 0 -32768 to +32767

int 0

long 0L

float 0.0f

double 0.0d

char ‘\u0000′ 0 to 65535

String (object) null

boolean false

Like reserved words, some of these will be used more than others and some
will hardly be used at all.

IMPORTANT: When giving a data variable a name it is vital that you follow
these rules for naming them. Names can have lower and upper case letters
that can be anywhere in the name (ex: name, Name, naMe, NaMe, etc.). If you

Page 6
Java Basics: Object Oriented Programming
make a data variable with a name in all caps it will be considered to be a
constant variable (will be discussed in a later document). When using variables
in the program they are case sensitive. Variable names may contain numbers,
BUT the name CANNOT start with a number (ex: 3name is incorrect, na3me or
name3 is correct). Variable names cannot not contain spaces. Variable names
can be as long as you want, but it is strongly recommended to make
reasonably small names. Names can contain the special characters ‘$’ and ‘_’
but is recommended to avoid whenever possible (especially the ‘$’). It is also
highly discouraged to place them at the beginning of the variable’s name. If
you are going to use subsequent variable names (name used more than once
but each holds a different value) then just had a number to the end of it (ex:
name1, name2, name3, etc.). The name you use cannot be a java reserved
word or java keyword.

Classes are basically just outlines of objects, defining properties and behaviors.
Sometimes a program itself is just a single class.

Objects are instances of a class using the new operator. They are basically
data variables that store information that the class it came from was designed
to hold.

There are also instances, static, inheritances, and interfaces in java but those
will be discussed, later as you get farther into programming.

Page 7

Vous aimerez peut-être aussi