CS 201: Introduction to Programming With Java

Download Report

Transcript CS 201: Introduction to Programming With Java

Lecture 2: Introduction To Computers,
Programming, and Java
John Hurley
CS 201
Cal State LA
Computer Hardware
Bus
Storage
Devices
e.g., Disk, CD,
and Tape
Memory
CPU
Communication
Devices
Input
Devices
Output
Devices
e.g., Modem,
and NIC
e.g., Keyboard,
Mouse
e.g., Monitor,
Printer
Hardware
Visible, physical elements of the computer. Programmers need to know a little,
not a lot, about hardware
 Main components:
 CPU
 retrieves instructions from memory and executes them
 Memory
 temporarily stores data and program instructions
 "volatile" = information is lost if power goes off
 Storage Devices
 non-volatile storage; data remains after a power interruption
 Communication Devices
 devices that allow computers to communicate with each other
 Input and output devices
 devices that let the user communicate with the computer
Components are connected through a subsystem called a bus
Software
Software
The instructions and data that are necessary for operating a computer
Program
A software application, or a collection of software applications, designed to
perform a specific task
Algorithm
A set of instructions that will solve a problem in a finite amount of time
A program normally implements one or more algorithms
Software
 Programming
 the preparation of programs!
 requires a programming language
 From Wikipedia:
 A programming language is an artificial language designed to express
computations that can be performed by a machine, particularly a computer.
Programming
 Computers execute machine code, also called machine language, which
consists of instructions expressed as base 2 numbers
 different machine languages for different types of processors
 very few programmers can write machine language (but hardware
engineers who design chips occasionally do)
 some programming is done in assembly language, which is a humanunderstandable set of mnemonic codes that corresponds closely to
machine instructions
 Different for different types of CPUs
 Assembly language is fun to write, but is only occasionally used in practical
programming today. CS majors study it to understand some fundamental concepts in
the field, rather than to use it on the job.
Typical Assembly Language Code
LOAD r1, X ;
load the variable x into r1
SET r2, 10 ;
set r2 to immediate value 10
CMP r1, r2 JMP NEQ, ELSE1 ;
If r1 <> r2, then jump to the else part,
;
otherwise do the then part
... ;
then part 1 (code omitted)
JUMP END1 ; jump over the else part
ELSE1:
... ;
else part 1 (code omitted)
END1:
LOAD r1, X ;
load the variable x into r1
SET r2, 10 ;
set r2 to immediate value 10
CMP r1, r2 JMP NEQ, TEST2 ;
JUMP END2 ;
if x <> 10 go on to next test
otherwise skip past then part 2
TEST2:
LOAD r1,Y ;
load the variable y into r1
SET r2, 20 ;
set r2 to immediate value 20
CMP r1, r2
JMP GT, THEN2 ;
if y > 20 go on to then part 2
JUMP END2 ;
otherwise skip past
THEN2: ... ;
then part 2
END2:
Programming Languages
 Most programmers would need a few minutes just to understand
the code on the last slide.
 Assembly language programming involves manipulation of very small
machine operations.
 Over time, easier-to-use programming languages have evolved to allow
greater programmer productivity
Programming Languages
Memorize this slide!
 Low level language: few machine instructions per line of
programming-language code.
 a fancier way to put this: little abstraction from the machine operations
 High level language: many machine instructions per line of code
 greater abstraction from machine operations
 Because high-level languages do not directly manipulate machine
operations, they can be standardized across different operating systems,
and even across different hardware (that uses different sets of machine
operations.)
 Lower level languages often generate programs that run more
efficiently (faster or with less memory use) than higher level languages
 Higher level languages usually offer better programmer productivity
Low-level Vs. High-Level
 Typical assembly language instruction:
 mov cx,digits
 Sets the value held in CPU register cx to the current value of a variable
called “digits”
 Typical high-level language statement:
 area = 5 * 5 * 3.1415;
 Sets the value of a variable called “area” to 5* 5 * pi
 would take many lines of assembly language to do this
Programming Languages
 High level languages
 first widely-used ones were COBOL, FORTRAN, and LISP, used for
business applications, scientific applications, and academic research.
 BASIC 1964 used for teaching programming
 Thousands of other high level languages are or have been in use
 Ancestry of Java:
 ALGOL developed 1950s by a committee of professors
mostly used to describe algorithms, not for actual programs
 C 1969 Bell Labs, drew on ALGOL
 relatively low level
 originally designed for programming the UNIX operating system, but widely used
for other software
 C++ 1979 Bell Labs
 superset of C, adding support for object-oriented programming

Expense
 Resources are not free; in programming lingo, they are said
to be expensive
 Programmers’ time
 CPU usage
 Memory
 Storage
 Minimizing expense is a key part of programming
 use as little CPU capacity, memory, storage, etc. as possible
 Use programmers’ time as productively as possible
Expense
 Hardware has become much less expensive over the history of
software engineering.
 In 1970, a computer and its peripherals cost millions of dollars and
filled a small building. Today most code runs on computers that cost
less than $2,000 each.
 Programmers in developed countries get paid about the same
now in constant dollars as they did 45 years ago
 Thus, programmers have become more expensive relative to
hardware. The expense of paying us is a much larger part of the
total expense of developing software than it was, for example, in
1970.
 This favors programming languages and techniques that make
programmers more productive, rather than those that execute
most efficiently
Java
 Java 1995
 developed at Sun Microsystems
 easier to use than C++ in many ways
 gives up some of C’s low-level capabilities
 approximately tied with C++ as the most widely used programming
language today
 Designed to allow identical code to produce the same results on many
different platforms (types of hardware and operating systems)
Java
Main uses of Java today:
 Used in many web-based applications
 Used for programming devices like video game
consoles, Android phones, BluRay players, etc.
 Desktop applications that need to be easily ported
(moved) to various operating systems.
 Originally expected to find widespread use in embedded
systems (software that runs on computers included in
appliances, cars, etc), but this did not pan out
Siblings of Java
 C#
 Microsoft’s proprietary answer to Java
 proprietary = controlled by a company
 Very easily learned by programmers who already know Java
 Visual Basic
 Another MS proprietary language, descended from both C++
and BASIC
 Objective C
 Apple proprietary language, used for Mac and iOS platform
programming
Programming Language to Machine
Instructions
 source code = instructions written in a programming language
 Assembly language is translated to machine code using an assembler
 High level languages are translated using interpreters and/or compilers
 interpreter translates into machine language at runtime; no need to store
extra files
 compiler translates into machine language ahead of time and stores the
executable file: faster at runtime than in interpreter
Programming Language to Machine
Instructions
Source File
Compiler
Machine-language
File
Library Code
Linker
Executable File
Programming Language to Machine Instructions
 Traditional BASIC and JavaScript (language used for writing programs
that run in your web browser) use interpreters, as do many newer
languages like PHP.
 C and FORTRAN use compilers
 Java uses a two-step process that attempts to make the best of both
MEMORIZE THIS!
 Java is compiled before runtime to an intermediate language called Java
bytecode
 The Java Virtual Machine interprets bytecode to machine language at
runtime
 All forms of JVM run the same bytecode, but each platform (operating
system or hardware type) requires a different type of JVM. Everything
that is specific to the operating system or hardware is contained within the
JVM, not the compiler.
 Therefore, Java bytecode is analogous to an assembly language for the JVM
Pros and Cons of JVM
 Main Advantage
 Java is highly platform-independent
 Write Once, Run Anywhere
 Usually easy to run the same Java code in UNIX/OSX/Windows/
Android/Toaster/Antilock Brake System/Whatever
 reduces the amount of knowledge the programmer needs to have about the specific
platform
 adds long-term robustness. Your Java code will still run in Windows 18.7 as long as
there is a JVM.
 Main Drawbacks:
 JVM itself uses some memory, CPU capacity, and other system resources
 Adds one extra step at runtime.
 Java code often runs more slowly than compiled C++
Statements
A statement represents an action or a sequence of actions,
which may include decisions. This is different from the usual
English meaning of the word “statement”
Example:
System.out.println(“Hello, World!");
prints the message “Hello, World!”
Every statement in Java ends with a semicolon (;).
24
Variable and Constant Declarations
Programs often use variables and constants
You already know these concepts from algebra
You must tell the computer to set aside memory space for
any variable or constant you use
int radius = 0;
finds space to store an integer, names it radius, and sets the
value of radius to 0
25
Memory And Data
 Memory
 volatile = data is normally lost when power lost (reboot or power
interruption)
 every location in memory has a numeric address
 all information is measured in bits
 A bit has two possible values which you can conceptualize as true or
false, on or off, or 0 or 1
 a bit is a unit of information, not a physical device.
o often corresponds to the state of a particular transistor, but
programmers rarely need to think about this.
 values of single or multiple bits are conveniently represented as base
2 (binary) numbers
Base 2
Decimal
Binary
Decimal
Binary
0
0
7
111
1
1
8
1000
2
10
9
1001
3
11
4
100
15
1111
5
101
16
10000
6
110
Bits
 You do not have to become a whiz at base 2 arithmetic,
but you do need to develop basic familiarity with it
 Base 2 math in this course is intended to
 make the point that programming is just a fancy way to flip billions of
switches
 ease the learning curve for later courses in which you will need to
understand binary
 There may be a few easy questions on base 2 arithmetic on the
quizzes. They will only be a little harder than the examples below.
Binary Arithmetic Rules
0+0=0
0+1=1
1+0=1
1 + 1 = 10
1+1+1 = 11
1+1+1+1 = 100
1+1+1+1+1 = 101
20/07/2015
29
Binary Arithmetic
01 + 01 = 10
11 + 11 = 110
101 + 101 = 1010
1000 + 1 = 1001
1000 + 11 = 1011
1111 + 1 = 10000
11 – 10 = 1
100 – 10 = 10
101 – 11 = 10
Bytes
 1 byte = 8 bits
 28 = 256, so the value of 1 byte ranges from
00000000 to 11111111 binary = 0 to 255 decimal
 1 byte is usually the minimum amount of information that you can
manipulate
 Terms like kilobyte and megabyte refer to quantities of bytes in units that
are powers of 2 close to the decimal numbers implied in the names, 1K =
1024 bytes; 1 MB = 1048576 bytes
 Most of the time, the actual storage units are invisible to the programmer.
For example, you will usually deal with an integer without worrying about
the fact that the computer sees it as 4 bytes
Data Types
 Every data item (variable, constant, or even a literal,
such as the integer 3 or the string “Godzilla”) has a
specific data type.
 Each type of data is stored with a specific amount of
memory and handled differently by the compiler
 Thus, you must think carefully about the type of every
piece of data you use.
•a character takes less space to store than an integer
•you can’t find the product of two variables unless they are both
numbers
•If you divide one integer by another and store the quotient as an
integer, you may lose information. It may not be good enough to say
that 5 / 4 = 1
Data Types
Storage affects the possible values for a piece of data.
 Java uses 4 bytes = 32 bits to store the value of an integer
 The (complex) method for tracking the sign uses one
bit.
 231 = 2147483648
 a Java integer has possible values from -2147483648 to
+2147483647
 2147483647 + 1 = garbage
 Memorize this: “the minimum value for a Java integer is
approximately negative 2 billion. The maximum value
is approximately positive 2 billion.”
Variables and Constants
 You must tell the JVM in advance when you will use a
variable or constant
 Must specify what kind of data the value will use
 JVM knows how much (expensive) memory to use to track the
value
 Example:
 int answer=1;
 creates a variable called answer, sets aside space to store it, sets the initial
value to 1
Data Types
 Primitive Data Types
 primitive = built in to the programming language as a basic
building block
 Many other data types are built into Java, but are made up of
primitive types plus methods
 Later you will learn how to build your own data types using
primitive and other built-in types as building blocks
Numeric Data Types
Name
Range
Storage Size
byte
–27 (-128) to 27–1 (127)
8-bit signed
short
–215 (-32768) to 215–1 (32767)
16-bit signed
int
–231 (-2147483648) to 231–1 (2147483647) 32-bit signed
long
–263 to 263–1
(i.e., -9223372036854775808
to 9223372036854775807)
64-bit signed
float
Negative range:
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
32-bit IEEE 754
double
Negative range:
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
64-bit IEEE 754
Floating Point Types
 “Floating point types” inlude the types float and double
 Internal representation of floating points types includes an
exponent, much as in scientific notation
 These types store values with limited precision, essentially
rounding to the closest value they can hold
 double doesn’t just hold a larger range of values than float, it
also provides greater precision
 If you want to learn more about this, see
 https://en.wikipedia.org/wiki/Floating_point_type
37
Numeric Data Types
 Tradeoffs in choosing numeric data types
 Minimize resource usage; use smallest type that will hold the values
you expect to use
 Minimize risk of error; pick safest choice
 Most of the time, programmers use int for integers and double for
floating point numbers
 Use long if the value may be larger than an int can store
 Rarely use byte or short
38
Numeric Data Types
 Declare a double:
 double gpa;
 declare a double and set its value to 1
 double gpa = 3.75;
 double gpa = 4d;
39
Methods
Procedures for performing particular tasks
Java methods are equivalent to functions, subroutines, or
procedures in other languages
All you need to understand right now about methods is how
to see what is inside one and what isn’t. Most of our code will
be inside methods.
A method:
public int get Product(int firstInt, int secondInt) {
return x * y;
}
40
Classes
A class is a template or blueprint for objects.
You will learn to understand classes in more depth later in
this course, but all of our work in this class will be done inside
classes.
For now, understand that a program is defined by using one
or more classes.
41
Classes
Classes often (not always) model real-world entities or abstractions
that already exist outside the program
A Ship object might have
data variables including weight, country of registration, and engine displacement
procedures (methods/ functions/ subroutines) including accelerate, show speed, drop anchor, fire
cannon, prepare to be boarded, sink
A Rectangle object might have
three data variables that represent coordinates
methods to calculate area and perimeter and to determine whether a given
coordinate is inside the rectangle
42
Objects
An object is a self-contained entity that consists of both
data and procedures to manipulate the data.
In Java and most (not all) other object-oriented languages,
objects are instances of classes (think Plato.)
Objects may have values for the variables defined in the
class.
Multiple objects of the same class have the same variables,
but the actual data (value of the variables) may be different,
in the same way that all ships have weights but there may be
no two ships with the same weight.
Accordingly, running the same methods for different objects
43 same class may produce different results.
of the
main()
The main() method is the starting point for the flow of
control in the program. The JVM executes the application by
invoking (running) the main method.
For the time being, all our classes will contain main()
methods
main() often calls other methods, including ones that are in
other classes
 main() looks like this:
public static void main(String[] args) {
// Statements;
44
}
Skeleton Java class
Start with this code:
public class Skeleton{
public static void main(String[] args){
// early in the course, all your code will go inside main()
} // end main method
} // end class
System.out.println
The simplest way to show output
By default, prints to the command console
System.out.println("Hello, World!");
prints the message “Hello, World!”
System.out.println(i)
prints the value of a variable named i
System.out.println("i = " + i);
prints the literal text "i =" followed by the value of i
The + means "concatenate the next item to this output," ie "add the value of the
variable i to the end of the text in the quotes"
if the value of i is 1 at the time the statement executes, prints “i = 1”
46
Comments
 Ignored by the compiler and JVM
 Used for documentation intended for programmers or other
people involved in development
Single line comment: A line comment is preceded by two slashes
(//) in a line. The rest of the line becomes a comment.
Multiline comment: A paragraph comment is enclosed between
/* and */ in one or multiple lines.
javadoc comment: javadoc comments begin with /** and end
with */. They are used for documenting classes, data, and
methods. They can be extracted into an HTML file using JDK's
javadoc
command.
47
Running A Java Program
 Later in the course, we will use Eclipse, an Integrated Development
Environment
 IDE = a software package that provides tools for programming
 For now, we will run programs from a command line
 Use the lab computers in class, at least for lab 1, but get the latest
release of the Java Development Kit for your homework. Allow plenty
of time for installation in case things go wrong!
 After Lab 1, you may use your own laptop in class, but make sure you
can use the lab computers since you will need to use them on the
exams.
 Mac users: OSX has a version of the JDK preinstalled. It may not be current,
but it should work fine for this class
JDK Versions
 Java Standard Edition (J2SE)
 J2SE is used to develop client-side standalone
applications or applets. We will use this edition.
 Java Enterprise Edition (J2EE)
 J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages. It is OK to
get this one, but the download is bigger and we won’t use
the additional functionality.
 Java Micro Edition (J2ME).
 J2ME can be used to develop applications for mobile and
small embedded systems
Running A Java Program
 Java source code is stored in plain text files with .java file name
extension. You can save them on a flash drive or your server space (ask
IT, not me, where that is!) or email them to yourself.
 For the time being, write your code with a plain text editor
 Recommended for Windows: Notepad++ or Geany. Notepad, TextEdit,
etc. will work too, but Notepad++ is better. You can use a word
processor, but if you do you will soon realize why I don’t recommend it.
 Save as plain text using extension .java. If your editor contains a “save
as java source” option, that will work fine, too.
Compile From Command Line
 Instructions for running a Java program from the command line are in
Section 1.8 of the book
 Key steps:
 Add JDK bin directory to path, e.g.
set path=c:\java\jdk1.7.0\bin
Or, for Windows, use control panel/System and
Settings/System/Advanced Settings/Environment Variables
Be *careful* editing your path this way!
 Open a command prompt and navigate to the folder where you saved the
file
 Type javac filename.java
Compiles Java source to
Bytecode
 for example, if the program file is called Hello.java, type

javac Hello.java
Notice that there is now a file called
filename.class, eg Hello.class. This file contains the bytecode
Running From The Command Line
Runs the JVM with the bytecode as input
 Type java –cp [path] Classname
Where [path] is the full path to the directory where the .class file is, usually
the same directory where you saved the .java file
 for example, java –cp C:\Users\godzilla\cs201\Hello
 The –cp part sets the classpath, the path where the JVM searches for class files. You
can also edit the classpath (CLASSPATH in all caps) using the environment variables
editor described in the last slide; if you do this you can skip the –cp and path
parameter
NoClassDefFoundErrors
 NoClassDefFoundErrors are usually caused by incorrect settings for
the classpath variable.
 More instructions and an explanation of classpath are at
http://en.wikipedia.org/wiki/Classpath_(Java)
Operating Systems
An operating system (OS) is software that manages hardware and software
resources and provides common services for computer applications.
(Wikipedia)
Other than very small embedded systems, like the ones that run digital
watches, almost all computers use operating systems.
OSs save programmers from the necessity to work with low-level aspects of
computing like
 Choosing specific blocs of memory to use
 Deciding which program can use the CPU at a particular time
 Working directly with storage devices
The most common operating systems in general usage are
 Windows
 UNIX
 Linux
 Mac OSX