JAVA: A BEGINNER’S GUIDE Chapter 1: Java Fundamentals 1 (c) Gary R. Smith, MS.
Dec 25, 2015
1
JAVA: A BEGINNER’S GUIDEChapter 1: Java Fundamentals
(c) Gary R. Smith, MS.
2
KEY SKILLS AND 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(c) Gary R. Smith, MS.
3
KEY SKILLS AND CONCEPTS
Use the if and for control statements Create blocks of code Understand how statements are
positioned, indented, and terminated Know the Java keywords Understand Java identifiers
(c) Gary R. Smith, MS.
4
THE ORIGINS OF JAVA
Conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan at Sun Microsystems in 1991.
Originally designed as a platform-independent language to control embedded consumer electronic devices.
Based on C and C++ languages. The advent of the Internet promoted the
change from consumer electronics to the Internet programming.
(c) Gary R. Smith, MS.
5
HOW JAVA RELATES TO C, C++, AND C# Java’s syntax is inherited from C. Java’s object model is adapted from C++. Java was developed by programmers for
programmers. Java is not upwardly or downwardly compatible
with C++. Designed to solve a different set of problems. C#’s syntax and structure are similar to Java. C# was designed for a different computing
platform.
(c) Gary R. Smith, MS.
6
JAVA’S CONTRIBUTION TO THE INTERNET Java Applets – special type of program
that gets automatically executed by a Web browser. Intended to be small programs. Is downloaded upon demand. Use to display data, handle input, and
provide simple functions. Executes locally rather than on a server.
(c) Gary R. Smith, MS.
7
JAVA’S CONTRIBUTION TO THE INTERNET Security – Applets are confined to the
Java execution environment. Cannot access other parts of the computer. Reduces the likelihood of malicious code or
breaches of security.
(c) Gary R. Smith, MS.
8
JAVA’S CONTRIBUTION TO THE INTERNET Portability
Java can run on a variety of different platforms using the same code called Bytecode.
Bytecode Not executable code. Is interpreted by the Java Virtual Machine
run-time environment. Highly optimized for the interpretive
process.(c) Gary R. Smith, MS.
9
JAVA BUZZWORDSSimple Has a concise, cohesive set of features that makes it easy to
learn and use.
Secure Provides a secure means of creating Internet applications.
Portable Programs can execute in any environment for which there is a Java run-time system.
Object-oriented Embodies the modern, object-oriented programming philosophy.
Robust Encourages error-free programming by being strictly typed and performing run-time checks.
Multithreaded Provides integrated support for multithreaded programming.
Architecture-neutral
Is not tied to a specific machine or operating system architecture.
Interpreted Supports cross-platform code through the use of Java bytecode.
High performance
Bytecode is highly optimized for speed of execution.
Distributed Designed with the distributed environment of the Internet.
Dynamic Programs carry with them substantial amounts of run-time type information that is used to verify and resolved accesses of objects at run time.
(c) Gary R. Smith, MS.
10
OBJECT-ORIENTED PROGRAMMING
Object-Oriented Programming is a programming model that focuses on data and the code that manipulates that data.
Key Features: Encapsulation – programming mechanism that
binds together code and the data it manipulates. Polymorphism – programming mechanism that
allows a single interface to have many forms. Inheritance – the process by which one object
can acquire the properties of another object.
(c) Gary R. Smith, MS.
11
OBJECT-ORIENTED PROGRAMMING
Class – Java’s basic unit of encapsulation. Often referred to as a program. May contain both data members (aka instance
fields or instance variables) and code members (aka methods).
A class is the blueprint to construct an object. A class is not the object. Objects are instances of a class.
Data and methods can be private (known only within the class) or public (known to other classes).
(c) Gary R. Smith, MS.
12
ANATOMY OF A JAVA CLASS
// Program: AJavaProgram.java Inline comment
// Author: Gary R. Smith Inline comment
// Date Written: 8/16/2014 Inline comment
/* This program displays a message Block comment (multi-line) on the monitor */
(/* begins comment, */ ends comment)public class AJavaProgram
Class header (name of the class){
Starts a block of code (class)// This is where the program starts and ends. Inline commentpublic static void main(String args[]) Method header{
Starts a block of code (method)// Displays a message on the monitor
Inline commentSystem.out.println(“Java is fun!”);
Executable statement}
Ends a block of code (method)}
Ends a block of code (class)
(c) Gary R. Smith, MS.
13
PROGRAM ELEMENTS
// (two slashes) indicates an inline comment. Not compiled as part of the program Provides in-program documentation Comment goes to the end of the line
/* and */ indicates a block comment Everything between /* and */ are comments Not compiled as part of the program Can be as long as you want.
Don’t be stingy on comments!
(c) Gary R. Smith, MS.
14
PROGRAM ELEMENTS
Class header (public class AJavaProgram) defines the name of the program. Public is an access modifier that defines
whether or not outside classes can access the class.
Must be the same name as the file name public class AJavaProgram must have a file
name of AJavaProgram.java
(c) Gary R. Smith, MS.
15
PROGRAM ELEMENTS
Curly braces: { } Denotes a block of code treated as a unit. Encapsulates classes Encapsulates methods Encapsulates program elements
supporting multiple statements.
(c) Gary R. Smith, MS.
16
PROGRAM ELEMENTS
Method header (public static void main(String args[]) defines a named block of code. Public is an access modifier. Static means the method belongs to the class itself
and not an instance of the class. Void (return type) means the method does not return
any value. main() is the name of the method which must include
the parenthesis, which hold parameters. String args[] is the parameter of the main() method.
Methods may have zero or any number of parameters. String args[] is a parameter of the main() method.
(c) Gary R. Smith, MS.
17
PROGRAM ELEMENTS
System.out.println(“Java is fun!”); is an executable statement. System is a predefined class out is an object of the System class println() is a method of the System.out
object “Java is fun!” is a parameter that is passed
to the println() method. Note it is String data.
The semicolon ‘;’ terminates an executable statement.(c) Gary R. Smith, MS.
18
SYNTAX ERRORS
Syntax errors: violations of the language rules. Caught by the compiler Incorrect punctuation Misspelled keywords and identifiers Not specifying balanced (open and/or
closing) parenthesis, curly braces, or square brackets.
(c) Gary R. Smith, MS.
19
DATA TYPES
Data types define: How data is stored in memory The minimum and maximum values a data item can
hold The amount of memory allocated for the data item.
Each data type is based on a class. Java primitive data types are:
Integer types: byte, short, int, long Floating point types: float, double Boolean type: boolean Character type: char
(c) Gary R. Smith, MS.
20
SOME IMPORTANT DEFINITIONS
Field - An area of a database record, graphical user interface form, data item, or memory into which a particular item of data is stored. Can be subdivided into two general
categories: Variables: Those data items which can be
programmatically changed. Constants: Those data items which cannot be
programmatically changed.
A field is associated with a data type.(c) Gary R. Smith, MS.
21
JAVA KEYWORDS
abstract assert boolean break byte case
catch char class const continue default
do double else enum extends final
finally float for goto if implements
import instanceof
int interface long native
new package private protected public return
short static strictfp super switch synchronized
this throw throws transient try void
volatile while * true * false * null
Keywords have special meaning to the Java compiler. These keywords cannotbe used as names for variables, classes, or methods.
* Although not keywords, true, false and null are also reserved words.
(c) Gary R. Smith, MS.
22
IDENTIFIERS IN JAVA
Identifiers are names given to classes, objects, methods, variables, and other user-defined items. Can start with any letter, the underscore
character, or dollar sign. May contain (after the first character)
letters, numbers, the underscore character, or dollar sign
Cannot have any embedded spaces. Are case sensitive.
(c) Gary R. Smith, MS.
23
IDENTIFIERS IN JAVA
Class identifiers Begin each word with an upper case letter. All other letter are lower case. Example: AJavaProgram, Employee
Variable/object/method identifiers Begin the first word with a lower case letter. All other words begin with an upper case letter. Use camel casing/Hungarian notation. Example: employeeNumber, calculateInvoiceTotal
Note: This is a convention, not a language requirement.
(c) Gary R. Smith, MS.
24
JAVA CLASS LIBRARIES
Java contains hundreds of predefined classes stored in libraries.
System is a class library automatically included in every Java program.
You can create your own class libraries.
(c) Gary R. Smith, MS.
25
THE JAVA PROGRAMMING ENVIRONMENT
(c) Gary R. Smith, MS.
Java source programs end with a .java extension.Compiled Java programs end with a .class extension.
26
PROGRAM DEVELOPMENT CYCLE
(c) Gary R. Smith, MS.
Analyze the requiremen
ts
Develop an algorithm
Code the program
Test the program
Implement the
program
Maintain the
program
27
PROGRAM DEVELOPMENT CYCLE
Analyze the requirements What is the expected result? What data is needed to yield the expected
result? What actions are to take place What output, if any, is to be produced? Clarify calculations with the user. What are the objects?
(c) Gary R. Smith, MS.
28
PROGRAM DEVELOPMENT CYCLE
Develop an algorithm (solution to the problem) Pseudocode Flowchart IPO (Input/Process/Output) Diagram Decision Trees Truth Tables Object Dictionaries Develop testing criteria
(c) Gary R. Smith, MS.
29
PROGRAM DEVELOPMENT CYCLE
Code the program What standards does the organization
have? Naming conventions Program structure Libraries Internal documentation
(c) Gary R. Smith, MS.
30
PROGRAM DEVELOPMENT CYCLE
Test the program Unit tests - testing a single method Program tests – testing the operation of
the program System tests – testing to insure all
programs work properly together Acceptance tests – testing by the user to
verify everything works as specified.
(c) Gary R. Smith, MS.
31
PROGRAM DEVELOPMENT CYCLE
Implement the program File/data conversion User/system documentation Performance tuning
Types of implementations Phased Pilot Parallel Direct cut over
(c) Gary R. Smith, MS.
32
PROGRAM DEVELOPMENT CYCLE
Maintain the program Changes to programs are inevitable.
Governmental regulations Changes in business practices Technological changes Acquisitions and mergers
Follows the first 5 steps of the program development cycle.
(c) Gary R. Smith, MS.
33
NEED MORE HELP?
Tutorials: http://
docs.oracle.com/javase/tutorial/index.html Java™ Platform, Standard Edition 8 API
Specification http://
docs.oracle.com/javase/8/docs/api/index.html
(c) Gary R. Smith, MS.