Basic Programming Concepts in Java
Basic Programming Concepts in Java
S E D G E W I C K / W A Y N E
2. Basic Programming
Concepts
Sections 1.1 and 1.2
[Link]
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
[Link]
You need to know how to program
in order to be able to tell a computer what you want it to do.
Prepackaged solutions (apps) are great when what they do is what you want.
Programming enables you to make a computer do anything you want. well, almost anything (stay tuned)
Analytical
Ada Lovelace
Engine
3
Programming: telling a computer what to do
Programming
• Is not just for experts.
• Is a natural, satisfying and creative experience.
• Enables accomplishments not otherwise possible.
• The path to a new world of intellectual endeavor.
Challenges
• Need to learn what computers can do.
• Need to learn a programming language. Telling a computer what to do
Java features
• Widely used.
• Widely available.
• Continuously under development since early 1990s.
• Embraces full set of modern abstractions.
• Variety of automatic checks for mistakes in programs. James Gosling
$100 billion,
Java economy 5 million developers
• Mars rover.
• Cell phones.
• Blu-ray Disc.
• Web servers.
• Medical devices.
• Supercomputing.
•…
6
Our Choice: Java
Java features
• Widely used.
• Widely available.
• Continuously under development since early 1990s.
• Embraces full set of modern abstractions.
• Variety of automatic checks for mistakes in programs.
Your programs will primarily consist of these plus identifiers (names) that you make up.
8
Anatomy of your first program
program name
main() method
text file named
[Link] public class HelloWorld
{
public static void main(String[] args)
{
[Link]("Hello, World");
}
}
body of main()
(a single statement)
9
Anatomy of your next several programs
program name
main() method
text file named
[Link] public class MyProgram
{
public static void main(String[] args)
{
...
}
}
body of main()
(a sequence of statements)
10
Pop quiz on "your first program" (easy if you did Exercise 1.1.2)
% javac [Link]
% java MyProgram
Main method not public.
% javac [Link]
[Link]: invalid method declaration; return type required
public static main(String[] args)
^
11
Three versions of the same program.
/*************************************************************************
* Compilation: javac [Link]
* Execution: java HelloWorld
*
* Prints "Hello, World". By tradition, this is everyone's first program.
*
* % java HelloWorld
* Hello, World
*
*************************************************************************/
public class HelloWorld { public static void main(String[] args) { [Link]("Hello, World"); } }
Lesson: Fonts, color, comments, and extra space are not relevant to Java.
12
Note on program style
Different styles are appropriate in different contexts.
• DrJava
• Booksite
• Book
• Your code
Bottom line for this course: Life is easiest if you use DrJava style.
13
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
[Link]
Program development in Java
is a three-step process, with feedback
EDIT
2. COMPILE it to create an executable file
• Use the Java compiler
• Result: a Java bytecode file file such as [Link]
• Mistake? Go back to 1. to fix and recompile. COMPILE RUN
not a legal Java program
15
Software for program development
EDIT
COMPOSE
COMPILE RUN
REHEARSE PLAY
A significant difference with programs: We can use our computers to faciliate the process.
Program development environment: Software for editing, compiling and running programs.
• punched cards/compiler/runtime
1970
• editor/compiler/runtime/terminal
• editor/compiler/runtime/virtual terminal 1980
2000
17
Program development with switches and lights
Circa 1970: Use switches to input binary program code and data, lights to read output.
switches
Mid 1970s: Use punched cards to input program code and data, line printer for output.
Late 1970s: Use terminal for editing program, reading output, and controlling computer.
VT-100 terminal
1980s to present day: Use multiple virtual terminals to interact with computer.
• Edit your program using any text editor in a virtual terminal.
• Compile it by typing javac [Link] in another virtual terminal.
• Run it by typing javac [Link]
virtual TV set
21
Program development with personal computers (another approach)
1980s to present day: Use a customized application for program development tasks.
• Edit your program using the built-in text editor.
• Compile it by clicking the “compile” button.
• Run it by clicking the “run” button or using the pseudo-command line.
“Integrated Development
Environment” (IDE)
[Link]
“compile” button
“run” button
pseudo-command line
22
Software for program development: tradeoffs
Pros Pros
• Approach works with any language. • Easy-to-use language-specific tools.
• Useful beyond programming. • System-independent (in principle).
• Used by professionals. • Used by professionals.
• Has withstood the test of time. • Can be helpful to beginners.
Cons Cons
• Good enough for long programs? • Overkill for short programs?
• Dealing with independent applications. • Big application to learn and maintain.
• Working at too low a level? • Often language- or system-specific.
Two approaches that have served for decades and are still effective:
• multiple virtual terminals.
• integrated development environments.
[Link]
Built-in data types
'A'
char characters compare
'@'
"Hello World"
String sequences of characters concatenate
"CS is fun"
17
int integers add, subtract, multiply, divide
12345
3.1415
double floating-point numbers add, subtract, multiply, divide
6.022e23
true
boolean truth values and, or, not
false
27
Basic Definitions
variables
literals
28
Variables, literals, declarations, and assignments example: exchange values
A. No way for us to confirm that it does the exchange! (Need output, stay tuned).
29
Data type for computing with strings: String
typical literals "Hello, " "1 " " * " Important note:
operator + character
"1234" + " + " + "99" "1234 + 99" Ex 2: spaces "1234" + " + " + "99"
"1234" + "99" "123499"
space
characters
30
Example of computing with strings: subdivisions of a ruler
31
Input and output
is necessary for us to provide data to our programs and to learn the result of computations.
command-line
Humans prefer to work with strings. arguments
Output
standard output
• [Link]() method prints the given string.
• Java automatically converts numbers to strings for output. Bird's eye view of a Java program
Command-line input
• Strings you type after the program name are available as args[0], args[1], ... at run time.
• Q. How do we give an integer as command-line input?
• A. Need to call system method [Link]() to convert the strings to integers.
Stay tuned for many more options for input and output, and more details on type conversion.
32
Input and output warmup: exchange values
A. Reads two integers from the command line, then prints them out in the opposite order.
33
Data type for computing with integers: int
operator + − * / %
not quite the same as integers
35
Data type for computing with floating point numbers: double
[Link](2.0) 1.4142135623730951
"not a number"
values integers between −215 and 215−1 values integers between −263 and 263−1
short
int, float
long, double
37
Excerpts from Java’s Math Library
±
From algebra: the roots of + + are
40
Comparison operators
Fundamental operations that are defined for each built-in type allow us to compare values.
• Operands: two expressions of the same type.
• Result: a value of type boolean.
41
Example of computing with booleans: leap year test
42
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
[Link]
Type checking
Types of variables involved in data-type operations always must match the definitions.
The Java compiler is your friend: it checks for type errors in your code.
% javac [Link]
[Link]: operator * cannot be applied to [Link],int
String s = "123" * 2;
^
1 error
When appropriate, we often convert a value from one type to another to make types match.
44
Type conversion with built-in types
Cast for values that belong to multiple types. (int) 2.71828 int 2
• Ex: small integers can be short, int or long. (int) [Link](2.71828) int 3
• Ex: double values can be truncated to int values. 11 * (int) 0.3 int 0
a. ( 7 / 2 ) * 2.0
b. ( 7 / 2.0 ) * 2
c. "2" + 2
d. 2.0 + "2"
46
An instructive story about type conversion
short
int, float
long, double
48
Summary
The Java compiler is your friend: it will help you identify and fix type errors in your code.
49
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
2. Basic Programming
Concepts
Sections 1.1 and 1.2
[Link]