Developing Programs in Java
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department |
bernstdh@jmu.edu |
|
Compiled Languages (e.g., C)
Tools:
- Editor: Create one or more source files
- Containing the program in human-readable form
- Compiler: Convert the source files to object files
- Containing the machine instructions
- Linker: Combine all of the pieces into an executable
- Containing application code and library code
Important Points:
- These steps are performed "once"
- Executables are machine and OS specific
Interpreted Languages (e.g., Python, ECMAscript)
Tools:
- Editor: Create one or more source files
- Containing the program in human-readable form
- Interpreter: Execute the source file
- Each operation in the source file is "converted to" machine
instructions each time it is encountered
Important Points:
- Editing is performed "once"
- Interpreting is performed every time the program is executed
- The interpreter is machine and OS specific
Hybrid Languages (e.g., Java)
Tools:
- Editor: Create one or more source files
- Containing the program in human-readable form
- Compiler: Convert the source files to
intermediate files (e.g.,
.class
files)
- Containing byte codes that are similar to
machine instructions but not specific to a machine/OS
(hence are often said to be for
a virtual machine)
- Interpreter: Execute the byte codes
- Convert the byte codes to machine instructions (either
up-front or "just-in-time")
Important Points:
- Editing and compiling is done "once"
- The interpreter is used every execution
- The interpreter is machine and OS specific
Java Development
- Editing:
- You can use (almost) any editor
- You can use an integrated development environment (IDE)
like jGRASP, NetBeans, Eclipse, ...
- Compiling:
- Command Shell: javac FileName.java
- Eclipse: Compiles while you edit
- Executing:
- Command Shell (or DrJava Interactions Pane): java FileName
- Eclipse: Run-Run or
Command-Line/Run Arguments
- What They Are:
- Strings that are passed to an application (specifically,
to the
main()
method in the main class) when it
is executed
- Command Shell:
- Eclipse:
-
Run-Run configurations...,
enter the project name and main class on the [Main] tab, and
enter the arguments on the [Arguments] tab in the "Program
arguments" section
An Iterative Development Process
- What Students Often Do:
- Create a source file
- Compile the source file
- If there are compile-time (i.e., syntax) errors then curse
Correct one or more errors (chosen randomly)
Go to Step 2
- Execute the program once without caring about the results
- Use the electronic submission system
- If there are run-time (e.g., logic) errors then curse
Add a derogatory comment about the professor to the code
Make a random change
Go to Step 2
- Some Advice:
- This is not a productive process (though it is
fun to watch!)
- Think before you act
Making Changes
- Be Judicious
- Beginning students often mistakenly change
code that is actually correct. They then become reluctant to
change it back and this can lead to enormous amounts of
wasted time.
- Use Comments
- Don't delete code (initially), comment it out. This will
help you remember what you've changed and make it easier
to change it back if necessary.
- Think About What Needs To Be Re-Compiled
- Not because it saves time (the compiler is very fast) but
because it will help you understand the process better.
Types of Defects
- Syntax Defects
- Use of an inappropriate lexical item
- Use of an inappropriate construct
- Missing punctuation
- Style Defects
- Failure to comply with the style guide
- Logic Defects
You Must Distinguish Between
- Compile-Time Errors:
- Errors identified by the compiler/editor
- Examples: Syntax errors
- Run-Time Errors:
- Errors that occur during execution
- Examples: Divide by zero, missing
main
,
input mismatch
- Style Errors:
- Aspects of the code that don't conform to the course style
guide (which can be detected by inspection or using a tool)
- Examples: Incorrect indentations, misplaced brackets
Syntax Errors
- Examples (in Java):
-
doubl result;
-
result = a $ b;
- Some Thoughts:
- You should never make syntax errors but everyone makes
occasional small mistakes
- They will be "caught" by the compiler or a good editor
Error Messages from the Compiler
- A Necessary Skill:
- Reading and understanding error messages
- A Necessary Discipline:
-
Always correct the first (top-most) error first!
Logic Errors
- Computation Errors:
- A formula's implementation is wrong
- Stress/Overload Errors:
- A variable or data structure is filled past its capacity
- Throughput/Performance Errors:
- The algorithm does not perform at the required "speed"
Run-Time Error Messages
- A Necessary Skill:
- Reading and understanding run-time error messages
- A Necessary Discipline:
-
Always correct the first (top-most) error first!