Getting Started HELLO WORLD
Download
Report
Transcript Getting Started HELLO WORLD
Getting Started
Our Java Programming
Environment: DrJava
Home Page: http://drjava.org
DrJava requires Java 2, version 1.4 or later.
It is recommended to download and use Sun's
JDK 1.5.0, currently in beta releases, (from
http://java.sun.com)
Starting to Program in Java
/* & */ surround a
>commented= area
- this part of the program
is NOT run, but is for
some else to understand
what you did
/*
Name
Assignment Number
Date
Description in your own words of what the
assignment does
LIBRARY / PACKAGE attachment
*/
import java.awt.*;
... this allows the use of commands
found in within the >attached=
packages
- java.awt.* for standard Java
commands
Your First Java Program
// The "HelloWorld" class.
public class HelloWorld
{
void ->
static
->
public ->
means the
loads
the
means that
method
method
into
other
does not
memory
and
classes can
return a
keeps
it
call this
value.
there.
method.
Comment line. Any line that starts
with // is a comment line.
Defines the class name. The class name must
agree with the file name, this class must be
saved as HelloWorld.java. It must match
exactly as Java is case sensitive
Defines the start of the class.
main -> is the
method name, this
is the name used
for the method that
starts the program.
String [] args - allows
for use of words /
Strings
-{ } surrounds all
parts of the main
block
- Instructs the
program to print
"Hello World!"
public static void main (String[] args)
{ System.out.println("Hello World!");
... all programming code in the main block
} // main method
} // HelloWorld class
Defines the end of the class.
Printing Techniques
To
print a blank line, we can simply write:
System.out.println();
To
print a VERY LONG LINE, we write the following:
System.out.println(“This will print a Verrrrrrrrry long”
+ “on one line by joining”
+ “these strings into one line”);
The process of joining strings is called Concatenation (+ Sign)
If we don’t wish to jump to a new line after printing a
string, we can use the method print instead of Println
System.out.print(“Hello”);
System.out.print(“There!”);
This will produce:
HelloThere!
If we Want to print a string that contains escape
sequences such as double quotes, backslash, java ‘s
solution is to use \ Immediately infront of the escape
sequence.
System.out.println(“A double quote: \””);
System.out.println(“A backslash: \\”);
This will produce:
A double quote: ”
A backslash:
\
Assignment #1
P. 12
# 5, 6, 7
Due Friday 7th, 2008
Assignment # 1 Rubric