Title: The First Java Program
1The First Java Program
c\My Documents\JustHello.java
/ JustHello.java Steve Hanks 1/4/2005
First Java example for TCSS142, Winter
2005 / public class JustHello //
Signature of the "main" method is dictated //
by the Java Runtime Engine public static void
main( String args) System.out.println("
Hello world")
2Syntax Rules, Coding Conventions, and Personal
Style
- Java is a formal language which means that
there are precise rules that determine whether
any string is a valid Java program - a valid Java program will be accepted by the Java
compiler, though it might not do anything
interesting - the rules are in Appendix L of your text
- There are additional coding conventions that
programmers agree to follow to enhance
readability - these are suggestions for how to name variables,
how to indent, how to space things on a line, how
to split lines, when to comment and what kind of
information to put in comments - these mostly have to do with comments and white
space, neither of which matter to the Java
compiler - these guidelines are in Appendix F of your text
- You will develop your own personal style for
commenting, spacing, and naming things, within
the coding conventions. Consistency is good. - (Lets look at the first program to see about
syntax, conventions and style.)
3Running the Program (Most Basic Version)
- Remember, calling this a program is a little
misleading, in that Java only has classes and
methods. - We call any Java class with an appropriate main
method a program or an "application" because
the Java bytecode interpreter knows how to launch
it. - So our JustHello class is a program.
- First step is to compile the class (program). We
use the javac.exe program (the Java compiler),
and supply the name of the file containing our
program as a command-line argument. - The Java compiler generates a class file with the
same name as the class (which is also the same
name as the file, but with an extension of
.class). This file is created in the same
directory that the .java file appears in. - We can then ask the Java interpreter (java.exe)
to launch the program. We call java.exe with the
name of the class as a command-line argument.
4Compiling Then Executing
JustHello.java
public class JustHello public static void
main (String args) ...
JustHello.class
Computer
Java Compiler(javac.exe)
5TextPad and Integrated Development Environments
(IDE)
- An integrated development environment is a
program designed to help you write programs
effectively and efficiently. It knows both about
the language syntax and about the steps required
to compile and execute a program. - In writing the program, the IDE
- knows about fundamental syntactic elements
comments, strings, reserved words, punctuation
and often color-codes them for readability - knows about coding conventions, so it can do
things like insert a class template automatically
and indent code automatically - more sophisticated IDEs (like Visual Studio)
integrate language reference and debugging aids
into the system - In executing the program, the IDE
- knows how to compile the class file, saving you
the trouble of typing in the javac command every
time - knows how to execute the class file
6Side Issues in Running the Program
- The concept of a working or current directory
- The concept of an execution path
- What happens if the Java program has syntax
errors? - What happens if the Java class isnt actually a
program?
7Summary
- Java is built around the idea of a "class" which
has "methods" - The first classes we will be looking at are
called "programs" or "application" classes - they have a method with the following "signature"
- public static void main(String args)
- the existence of this method allows java.exe to
"execute" the class - There are two separate concepts of whether a
program is working - is it a valid program (Is its syntax ok? Can
javac.exe process it without errors?) - does it do what you intended as the programmer
(Is its logic OK?) - (the first is very easy to verify, the second is
much more tricky)