Transcript PowerPoint

Today!
• Build “HelloWorld” yourself in BlueJ and Eclipse.
• Look at all the Java keywords.
• Primitive Types.
Spring 2006
CISC101 - Prof. McLeod
1
“HelloWorld” in BlueJ
1. Find “BlueJ” in the start menu, but start the
“Select VM” program instead (you will only need
to do this once for every installation of BlueJ.)
2. Make sure the JDK selected is version 1.5.0… or
better. If it is not, you may have to open the
bottom of the window and browse or search for a
newer JDK. If you can’t find one, you will need
to install a newer JDK from java.sun.com.
3. Close this window, and start BlueJ as you
normally would.
Spring 2006
CISC101 - Prof. McLeod
2
“HelloWorld” in BlueJ, Cont.
4. Click on “Project”, then “New Project…”.
5. Browse to a convenient folder (where you can
find the project again), and type in a name for
the Project – don’t use any spaces (always a
good idea with Java filenames and folders!).
6. Click on “Create” to close the New Project
window. The new project name is now at the top
of the window.
7. Click on the “New Class” button at the left.
8. Type in the class’s name: HelloWorld and leave
the “class” checked off.
Spring 2006
CISC101 - Prof. McLeod
3
“HelloWorld” in BlueJ, Cont.
9. Click on “OK” to close the New Class window.
10. You can now see a new little box representing
your class. You can enlarge the box and/or drag
it around the screen (entertaining, but not
useful!).
11. Double click on the little box, to open the code
editor window.
12. Delete all the junk in the editor window by
pressing <Cntrl>A followed by <Del>. We don’t
need all this stuff! (code “auto-generation”).
Spring 2006
CISC101 - Prof. McLeod
4
“HelloWorld” in BlueJ, Cont.
13. Type in the class’ container code:
public class HelloWorld {
}
14. Save your program by typing <Cntrl>s or go to
“Class” then choose “Save”. (Save your code
often!!!)
Spring 2006
CISC101 - Prof. McLeod
5
“HelloWorld” in BlueJ, Cont.
15. You can choose to add a comment at the top of
your program, and another comment by the “}”:
// My very first Java program!
// By me!
public class HelloWorld {
} // end HelloWorld
Spring 2006
CISC101 - Prof. McLeod
6
“HelloWorld” in BlueJ, Cont.
16. Add the container for the main method:
// My very first Java program!
// By me!
public class HelloWorld {
public static void main (String[] args) {
} // end main
} // end HelloWorld
Spring 2006
CISC101 - Prof. McLeod
7
“HelloWorld” in BlueJ, Cont.
17. Save your program again. Note message at
bottom right corner – saying “Saved”.
18. Click on the “Compile” at the top left. Fix any
compilation errors.
19. Just in case you did not have any – make an
error (like removing a “}” or “{“) and compile
again to see what happens. Click on the “?” to
see a semi-useful message about the
compilation error.
20. Fix all errors and re-compile.
Spring 2006
CISC101 - Prof. McLeod
8
“HelloWorld” in BlueJ, Cont.
21. Leave the editor window open, but move to the
original “Project” window. Note how the box
looks different if your class has not been
compiled. You cannot run your program until it
has been compiled.
22. Right click on your little box and choose “void
main(String[] args)” to run your program.
23. Click on “OK” without changing any parameters
in the little text box.
24. Nothing happens right?
Spring 2006
CISC101 - Prof. McLeod
9
“HelloWorld” in BlueJ, Cont.
25. Our program did what we told it to do – nothing!!
26. Go back to the editor window and add the
following output statement:
System.out.println("Hello World!");
27. Save, compile, and then run your program, as
before. (Always, always – Save before
running!)
Spring 2006
CISC101 - Prof. McLeod
10
“HelloWorld” in BlueJ, Cont.
28. You should see the following little exciting
window – called the “Terminal” or “Console”
window:
29. On your own, from the project window, choose
“Help” then “BlueJ Tutorial”, if you wish to know
more.
Spring 2006
CISC101 - Prof. McLeod
11
“HelloWorld in Eclipse
1. The first time you start Eclipse, you will see the
“Welcome” page. You should check out some of
the information here – particularly one of the
simple “Tutorials” and the “Overview”.
2. For now, click on the “Workbench” link at the top
right.
3. Before proceeding, and for the first time only,
you should check the configuration of the
program, to make sure it is running the correct
Java JDK.
Spring 2006
CISC101 - Prof. McLeod
12
“HelloWorld in Eclipse, Cont.
4. In the top menu bar, go to “Window” then
“Preferences”.
5. Click on the “+” beside “Java” and then choose
“Installed JRE’s”. You should see the window on
the following slide:
Spring 2006
CISC101 - Prof. McLeod
13
Spring 2006
CISC101 - Prof. McLeod
14
“HelloWorld in Eclipse, Cont.
6. Make sure the 1.5.0… JRE is checked off.
7. Now choose “Compiler” and make sure the
“Compiler Compliance Level” is set to “5.0”.
8. You should see the following window:
Spring 2006
CISC101 - Prof. McLeod
15
Spring 2006
CISC101 - Prof. McLeod
16
“HelloWorld in Eclipse, Cont.
9. There are lots of other options in “Preferences”
but we do not have to worry about any of them
right now!
10. Click on “OK” to close the Preferences window.
This might take a little while and if you are asked
to re-compile, say “OK”.
11. Just as in BlueJ, you need to create a new
project. Projects can contain many java files,
however. Click on the little folder thing at the top
left:
Spring 2006
CISC101 - Prof. McLeod
17
“HelloWorld in Eclipse, Cont.
12. Click on “Project”
– you will see the
Project creation
wizard:
13. Click on the
“Next >” button.
Spring 2006
CISC101 - Prof. McLeod
18
“HelloWorld in Eclipse, Cont.
14. On this screen
enter a name for
your project. You
will see:
15. Click on “Finish”.
Spring 2006
CISC101 - Prof. McLeod
19
“HelloWorld in Eclipse, Cont.
16. Your new project will now be listed at the left in
the “Package Explorer”. Click on the “+” beside
the project’s name.
17. There is a link to the JRE System Library, that
we are going to ignore.
18. Click on that little folder thing at the top left, and
choose “Class” this time to start the New Java
Class wizard. You should see the window on the
next slide:
Spring 2006
CISC101 - Prof. McLeod
20
19. Type in
“HelloWorld” for
the name of your
class. Make sure
the method stub
checkbox for
“main” is checked
(to make your life
easier!).
Spring 2006
CISC101 - Prof. McLeod
21
“HelloWorld in Eclipse, Cont.
20. Click on “Finish” and your new class will show up
in the editor window.
21. Delete comments and/or add your own.
22. Add the System.out.println() line inside the main
method, as you did before.
23. You will notice some advanced editor features
when you hit period for example, and other
things such as auto indentation.
24. Save your program using <Cntrl>s or by clicking
on the little floppy disk button at the top left.
Spring 2006
CISC101 - Prof. McLeod
22
“HelloWorld in Eclipse, Cont.
25. Click on the little green arrow thing at the top, as
shown on the next slide.
26. Note that Eclipse compiles your program when it
saves it.
Spring 2006
CISC101 - Prof. McLeod
23
“HelloWorld in Eclipse, Cont.
Spring 2006
CISC101 - Prof. McLeod
24
“HelloWorld in Eclipse, Cont.
27. Provided you have not made any compiler
errors, you should see your output in the
Console window at the bottom.
28. Make some deliberate errors in your code to see
what happens. Eclipse pre-compiles your
program as you write it, and will not run it until
you have fixed all the errors it has detected.
Spring 2006
CISC101 - Prof. McLeod
25
Development Environments
• You can choose BlueJ or Eclipse (or any other
tool you like!).
• I will usually use Eclipse for class demonstrations.
• (I’m not going to try the command prompt
again…)
Spring 2006
CISC101 - Prof. McLeod
26
Java Keywords
abstract
double
int
super
assert
else
interface
switch
boolean
enum
long
synchronized
break
extends
native
this
byte
for
new
throw
case
final
package
throws
catch
finally
private
transient
char
float
protected
try
class
goto
public
void
const
if
return
volatile
continue
implements
short
while
default
import
static
do
instanceof
strictfg
Spring 2006
CISC101 - Prof. McLeod
27
Java Keywords, Cont.
• The goto keyword is reserved but does not do
anything in java.
• On the next slide I have highlighted the
keywords that we will be using in this course.
Spring 2006
CISC101 - Prof. McLeod
28
Java Keywords for CISC101
abstract
double
int
super
assert
else
interface
switch
boolean
enum
long
synchronized
break
extends
native
this
byte
for
new
throw
case
final
package
throws
catch
finally
private
transient
char
float
protected
try
class
goto
public
void
const
if
return
volatile
continue
implements
short
while
default
import
static
do
instanceof
strictfg
Spring 2006
CISC101 - Prof. McLeod
29
Java Keywords for CISC101, by Category
Primitive Types
Conditionals
Loops
Structural
boolean
byte
if
else
do
for
class
import
char
double
float
int
switch
case
while
continue
break
new
public
return
static
long
short
Spring 2006
final
void
CISC101 - Prof. McLeod
30
Other “Things” in Java Programs
•
•
•
•
•
Comments
Variable names
Literal values
“Punctuation” like: ; . , ( ) { } [ ]
Operators like: + - / % > < == = ! && ||
• We will also see lots of other object names along
with their “members” – attributes or methods.
Spring 2006
CISC101 - Prof. McLeod
31
Expressions
• A line of code in java often contains expressions.
• They are combinations of any or all of:
–
–
–
–
Variables
Literal Values
Operators
Method Invocations
• A line of code in java is terminated by a “;”
Spring 2006
CISC101 - Prof. McLeod
32
Variables
• Java is a “declarative” language.
• This means that you have to declare a variable
before you can use it.
• What is a variable anyways?
•A name for a piece of memory used to store
something.
•The “something” can be a primitive type value or
a pointer to an object (a memory reference).
•(Don’t worry about pointers, yet!!)
Spring 2006
CISC101 - Prof. McLeod
33
Variables, Cont.
• To create a variable, you must state what type it is
going to be.
• For example, to create a variable called aNum of
type int, you would use the following line of code:
int aNum;
• You could assign a value to aNum using
something like:
aNum = 200;
Spring 2006
CISC101 - Prof. McLeod
34
Back to Expressions
• What is a literal value?
• We just saw one: 200
• What is an operator?
• We just saw one of those too: =
• Other operators, for example, are: + - / *
• And, what is an expression?
• We just saw one of those too!: aNum = 200;
Spring 2006
CISC101 - Prof. McLeod
35
Expressions
• Now, let’s consider each of these elements again,
in more detail: variables, literal values, operators.
Spring 2006
CISC101 - Prof. McLeod
36
Java Primitive Types
• “Primitive Type” variables are those that are not
“Objects” in Java.
• Primitive Type Variables fall into the categories of
integer types, real types, characters and
booleans.
Spring 2006
CISC101 - Prof. McLeod
37
Primitive Types - Cont.
Integer
Real
byte
float
short
double
Character Boolean
char
boolean
int
long
Spring 2006
CISC101 - Prof. McLeod
38
An Integer Primitive Type Variable
• Declare using the “int” keyword.
• From -2147483648 to 2147483647, inclusive (4
bytes).
• (A “byte” is 8 bits, where a “bit” is either 1 or 0.)
• For example:
int aNum;
Spring 2006
CISC101 - Prof. McLeod
39
More Integer Primitive Types
• Other primitive types: byte, short and long:
• For byte, from -128 to 127, inclusive (1 byte).
• For short, from -32768 to 32767, inclusive (2
bytes).
• (For int, from -2147483648 to 2147483647,
inclusive (4 bytes). )
• For long, from -9223372036854775808 to
9223372036854775807, inclusive (8 bytes).
Spring 2006
CISC101 - Prof. McLeod
40
Aside - Storage of Integers
• Computers like to store numbers in binary - a
memory location is either “on” or “off”.
• An “un-signed” 8 digit binary number can range
from 00000000 to 11111111
• 00000000 is 0 in base 10.
• 11111111 is 1x20 + 1x21 + 1x22 + … + 1x27 = 255,
base 10.
Spring 2006
CISC101 - Prof. McLeod
41
Storage of Integers - Cont.
•
•
•
•
So, how can a negative binary number be stored?
Use the “two’s complement” system of storage.
Make the most significant bit a negative number:
So, the lowest “signed” binary 8 digit number is
now: 10000000, which is -1x27, or -128 base 10.
Spring 2006
CISC101 - Prof. McLeod
42
Storage of Integers - Cont.
Spring 2006
binary
base 10
10000000
-128
10000001
-127
11111111
-1
00000000
0
00000001
1
01111111
127
CISC101 - Prof. McLeod
43
Storage of Integers - Cont.
• For example, the binary number
10010101 is
1x20 + 1x22 + 1x24 - 1x27
= 1 + 4 + 16 - 128
= -107 base 10
• Now you can see how the primitive integer type,
byte, ranges from -128 to 127.
Spring 2006
CISC101 - Prof. McLeod
44
Storage of Integers - Cont.
• Suppose we wish to add 1 to the largest byte
value:
01111111
+00000001
• This would be equivalent to adding 1 to 127 in
base 10 - the result would normally be 128.
• In base 2, using two’s complement, the result of
the addition is 10000000, which is -128 in base
10!
• So integer numbers wrap around, in the case of
overflow - no warning is given!
Spring 2006
CISC101 - Prof. McLeod
45
Storage of Integers - Cont.
• An int is stored in 4 bytes using “two’s
complement”.
• An int ranges from:
10000000 00000000 00000000 00000000
to
01111111 11111111 11111111 11111111
or -2147483648 to 2147483647 in base 10
Spring 2006
CISC101 - Prof. McLeod
46
A Double Primitive Type Variable
• Declare using the “double” keyword.
• For double, (8 bytes) roughly ±4.9 x 10-308 to
±1.7 x 10308 to 15 significant digits.
• For example:
double aVal;
Spring 2006
CISC101 - Prof. McLeod
47
The Other Real Primitive Type
• Also have float:
• For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4
x 1038 to 7 significant digits.
• For double, (8 bytes) roughly ±4.9 x 10-308 to
±1.7 x 10308 to 15 significant digits.
Spring 2006
CISC101 - Prof. McLeod
48
Integer Literals
• A literal integer value is assumed to be of type
“int”.
• If you want the literal to be a long, then you must
add the letter “L” to the end of the literal value.
Spring 2006
CISC101 - Prof. McLeod
49
Double Literals
• Java assumes a literal number like “2.5” or
“3.45e-7” is a double literal.
• If you want a literal to be recognized as a float
type, then you must add the letter “F” to the end of
the number.
Spring 2006
CISC101 - Prof. McLeod
50
char Primitive Type
• Declared as:
char aChar;
• Char literals look like:
‘a’ ‘B’ ‘1’ ‘ ’ (a space)
Spring 2006
CISC101 - Prof. McLeod
51
char Primitive Type - Cont.
• A char primitive type can also refer to a “Unicode”
character. For example:
char aChar = ‘\u03C0’;
Where 03C0 is the hexadecimal (base 16) value for
the Unicode character: 
• Since two bytes are used to designate the
character value, it is possible to refer to 216 =
65,536 different characters.
Spring 2006
CISC101 - Prof. McLeod
52
Aside - Unicode Characters
• The ASCII code system only goes to 255 since it
only uses one byte.
• At the moment, just over 49,000 of the Unicode
positions are in use.
• See www.unicode.org
Spring 2006
CISC101 - Prof. McLeod
53
A Boolean Primitive Type Variable
• Declare using the “boolean” keyword.
• Either true or false
• For example:
boolean aFlag;
• boolean literals are: true, false
Spring 2006
CISC101 - Prof. McLeod
54
Aside - String’s
• String’s are not primitive data types, but are
Objects.
• We will discuss the difference later…
• A String can be declared in the same way as a
primitive type using the keyword: String.
• For example:
String response;
Spring 2006
CISC101 - Prof. McLeod
55
Variable Declaration
• To declare a variable, use the Java “keyword”
appropriate for the type of variable you are
declaring followed by a variable name you have
created, followed by a semicolon.
• Examples:
int aNum;
double totalVolume;
String userPrompt;
Spring 2006
CISC101 - Prof. McLeod
56
Legal Variable Names
• Java names may contain any number of letters,
numbers and underscore (“_”) characters, but
they must begin with a letter.
• Standard Java Naming Convention:
– Names beginning with lowercase letters are variables
or methods.
– Names beginning with uppercase letters are class
names.
– Successive words within a name are capitalized.
– Names in all capital letters are constants.
• (We’ll get to “constants” shortly).
Spring 2006
CISC101 - Prof. McLeod
57
Legal Variable Names - Cont.
Legal names:
myData
x1
AnotherName
TWO_PI
Variable or method names
Class names
Constant
Illegal names:
_toUpper
1Day
Spring 2006
CISC101 - Prof. McLeod
58
Literal Values
• int’s, for example:
12
-142
0
333444891
• double numbers, for example:
4.5
-1.0
3.457E-10
• boolean, for example:
• String literals:
“Hello!” “
Spring 2006
-3.4E45
true
spaces”
CISC101 - Prof. McLeod
59
Variable Declaration - Cont.
• int and double variables initially are given a
value of zero unless they are initialized to a value.
• Java may prevent you from using variables that
are not initialized.
• So, it is often necessary to initialize your variables
before use, for example:
int numDaysInYear = 365;
double avgNumDaysInYear = 365.25;
String greetingLine = “Hello there!”;
Spring 2006
CISC101 - Prof. McLeod
60
Variable Declaration - Cont.
• All these statements could be carried out in two
lines, for example:
int numDaysInWeek = 7;
Is the same as:
int numDaysInWeek;
numDaysInWeek = 7;
Spring 2006
CISC101 - Prof. McLeod
61
Constants
• The Java keyword, final can be used to make
sure a variable value is no longer “variable”.
• It becomes a constant, because Java will not
allow your program to change its value once it has
been declared:
final int NUM_DAYS_IN_YEAR = 365;
final double MM_PER_INCH = 25.4;
Spring 2006
CISC101 - Prof. McLeod
62
Arithmetic Operators
• The standard arithmetic operators in Java are:
– Addition (+)
– Subtraction (-)
– Multiplication (*)
– Division (/)
– Modulo (or “remainder”) (%)
• All of these operations apply to all numeric data
types.
• All require values on both sides of the operator.
Spring 2006
CISC101 - Prof. McLeod
63
Aside - Strings and the “+” Operator
• Not only can “+” operate on numeric values, but it
can also handle String’s on either or both sides.
• If one side is not a String, it will be changed to
one, and then it will be concatenated to the
String on the other side:
4 + “you” evaluates to “4you”
“apples” + “oranges” + 99 evaluates to
“applesoranges99”
3 + 7 + “little piggies” evaluates to
“10little piggies”
Spring 2006
CISC101 - Prof. McLeod
64
Logical Operators
• Return either true or false. All these require values on
both sides of the operator.
•
•
•
•
•
•
==
!=
>
<
>=
<=
equals to
not equals to
greater than
less than
greater than or equal to
less than or equal to
• && logical “And”
• || logical “Or”
Spring 2006
CISC101 - Prof. McLeod
65
Logical Operators - Cont.
• “Truth” tables for the “And” and “Or” operators:
boolean result = testa && testb;
&&
testa
testb true
false
true
false
true
false false false
||
testa
testb true
false
true
true
true
false true
false
boolean result = testa || testb;
Spring 2006
CISC101 - Prof. McLeod
66
Assignment Operator
=
set equal to
• This is all we need to be able to build Java
expressions.
• There are other assignment operators (+=, -=, *=,
/=), but we’ll look at these later.
• We will also consider “unary” operators later too.
Spring 2006
CISC101 - Prof. McLeod
67
Expressions
• Expressions are combinations of variables, literal
values, and operators.
• For example:
int aNum = 4 + 3 * 7; // aNum
int aNum = (4 + 3) * 7;
//
(4 > 7) || (10 > -1)
//
(5.5 >= 5.0) && (4.0 != 1.0)//
double circ = 3.14 * 2.0 * r;
is 25
aNum is 49
yields true
yields true
– (“//” precedes a comment in Java)
Spring 2006
CISC101 - Prof. McLeod
68
Precedence Rules
• Operator precedence rules determine which
operations take place in what order:
–
–
–
–
–
–
First *, /, %
Then +, Then <, >, <=, >=
Then ==, !=
Then &&, ||
Last =
• Use “( )” to control order of operations, as the
expression inside “( )” will be evaluated before
stuff outside of “( )”.
Spring 2006
CISC101 - Prof. McLeod
69