Introduction
to Programming
Java
Types and Method Arguments/Parameters
A major difference between TurtleTalk and Java is the way things in the languages are typed. Compared to TurtleTalk, Java is a strongly-typed programming language. All fields and methods in Java must be declared to be a specific type. All Literals in Java are inherently a specific type.
In this lesson, you will learn
int and String literals,
We are now living in a digital world. What this means is that we now can represent anything as a bunch of bits. This is very powerful; it allows you exchange things like photographs, songs, videos you record, etc... in e-mail, using peer-to-peer file sharing programs, via WWW servers, FTP, etc... across the Internet. When you are surfing the Web, you can click on a button and hear a bit of a new song by your favorite artist - and the quality of the sound is very good.
But, inside of programs, this "everything is just a bunch of bits" can be very dangerous. Imagine some futuristic computer program that a 9-1-1 operator could have. When a phone call is received, a standard phone could send a digital message containing the GPS location of where the call is coming from. This would allow the 9-1-1 operator to bring up the location of the caller on his computer display. This would, no doubt, help in directing the right aid where its needed. But, what if a non-standard phone sends the caller's phone number instead of GPS coordinates. To the computer it's just a bunch of bits - unless told otherwise, the computer would convert the phone number bits and end up with a bad location. This is not good - it could cost lives.
Mistakes in software (bugs) can have disasterous consequences. Differences that are obvious to humans can't be detected by a computer.
In older programming languages, software developers had no choice other than to represent everything as numbers. Java's support for objects and their explicit types are a dramatic improvement.
In an attempt to produce software that is more reliable, Java forces software developers to specify the type of everything in a program. It is very easy to create a new type whenever one is desireable.
| TurtleTalk type | TurtleTalk Example | Java type | Java Example |
number |
forward 100 |
int |
obj.setFontSize( 48 ); |
word |
if equal? :answer "y |
char |
if ( answer == 'y' ) |
word |
print "word |
String |
System.out.print( "word" ); |
sentence |
println [Two words] |
String |
System.out.println( "Two words" ); |
predicate |
output "true |
boolean |
return true; |
| Table 19.1 | |||
Notes:
Java actually has a bunch of numeric types; I am only introducing int at this point. The limitations of int, i.e., the subset of Integers that is encompasses, will not be a problem for the material I cover.
The first example of a TurtleTalk word that I have mapped to the char Java type is a word made up of a single character. Just an FYI in case you didn't make the connection.
A literal is the source code representation of a
primitive type, the String type, or the
null type...
In Table 19.1, I used literals in
all of the examples. In one case, I had the number "48" and
in a couple of places I had characters surrounded by double-quote
characters. These were int and String
literals, respectively. Time for you to learn more about them.
int Literals
The Java keyword int is an abbreviation for integer.
Integers are whole numbers; the negative whole numbers, zero, and
the positive whole numbers. In mathematics, there is no limit
placed on integers; they go on forever in both the positive and
negative directions. But, Java explicitly defines the limits
of numbers of all types that it supports. An int
covers the range: -2,147,483,648 through 2,147,483,647.
int literals are entered into source code as a sequence
of decimal digits. Multi-digit numbers (i.e. those greater-than
or equal to ten (10)) must start with a digit in the range 1..9 - you
can't have a leading zero. Non-zero int literals
that start with zero are expected to be in either the octal radix or
the hexadecimal radix.
Some examples of int literals are:
0 -99 16384 20050501 -459
Some examples of illegal int literals are:
099 32,768 2147483648
Some examples of String literals are:
"" "A four word String" "numeric operators: + - * /"
Since there will be times when you will want to include a double-quotation
character in a String, Java has <EscapeSequences>
for characters that are special. To include a double-quotation character
in a String literal, you put a backslash character in
front of it. Here's an example:
"String literal with \" in it"
There are a bunch of other <EscapeSequences>. I'll
cover one more you probably will need, the newline. To construct a
String that contains more than one line of characters, you
include a backslash followed immediately by the lowercase 'n'
character ('n' stands for newline). For example:
"A line of text\nfollowed by a second line of text"
That's enough about literals for now. I'll cover boolean, char, null,
and other numeric types when I use them in an exercise or example.
Arguments and Parameters
Time to learn a couple of computer language terms that allow us to be more
specific in our discussion of method declarations and invocations.
In the TurtleTalk lessons, the name input was associated with both the variables that procedures have for values passed to them when they are invoked and the actual values passed at each invocation. These are actually two different things.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| optModifiers | type | methodIdentifier | ( | parameterDeclaration | , ... | ) |
public |
void |
printInWindow |
( |
String message |
) | |
public |
void |
setFontSize |
( |
int size |
) | |
public |
void |
setWindowHeight |
( |
int height |
) | |
public |
void |
setWindowWidth |
( |
int width |
) | |
| Table 19.2 | ||||||
The first row of the table is just the column numbers so that I can refer to them in this explanation. The numbers are much shorter than the names of things.
The second row is a template where I've given names to the things that make up a method header. For instance, column 1 is the optional modifiers component of the method header. Column 2 is the method's type (the type of the value it returns). Column 3 is the method's name, its identifier. And, columns 4 and 7 are punctuation. The parenthesis that surround the parameter declarations.
The column that I'm going to be referring to for the remainder of this section is column 5. This column is the first input parameter that the method is expecting. Column 6 should be interpreted as zero or more occurances of column 5, repeated as needed. It is only here for completeness; so far, you are only working with methods that accept a single parameter.
Rows 3 through 6 are method headers. They are the methods provided in the last lesson's class: ExtendsStuff. Review its method summary documentation.
Check out row 3. The printInWindow( ) method has a
single parameter. Its declaration has two parts to it: a type
(in this case String - ignore the "java.lang.
prefix for now) and an identifier (in this case message).
You don't need to concern yourself with the identifier, at this point you
only need to know the type of the parameter, String in
this case.
The rest of the rows (4-6) are methods that you probably have not invoked
yet: setFontSize( ), setWindowHeight( ),
and setWindowWidth( ). These methods also expect
a single parameter; but, they take a number of type int.
You've seen examples of how to provide a String literal when
a parameter of type String is required. So, now that
you've seen what a literal of type int looks like, you
should be able to figure out how to invoke methods with int
parameters.
As a starting point, take a look at a template for the source code that you need to write to invoke a method, and some examples; you are already familiar with two of them.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| objectVariable | dotOperator | methodIdentifier | ( | argument | , ... | ) |
| System.out | . | println | ( | "Hello World!" | ) | |
| obj | . | printInWindow | ( | "Hello World!" | ) | |
| obj | . | setFontSize | ( | 72 | ) | |
| obj | . | setWindowHeight | ( | 300 | ) | |
| obj | . | setWindowWidth | ( | 800 | ) | |
| Table 19.3 | ||||||
The first row of this table is just the column numbers, so that I can refer to them in this explanation.
The second row is a template where I've given names to the things that make up a method invocation. For instance, column 1 is the object that supports the method. Column 2 is the dotOperator which is used for invocation of a method provided by an object. Column 3 is the name of a method, the method's identifier. And, columns 4 and 7 are punctuation, the parenthesis that surround the parameter declarations. The column that I are going to be talking about for the remainder of this lesson is column 5. This column is the first input argument. Column 6 should be interpreted as zero or more occurances of column 5, repeated as needed. It is only here for completeness; so far, you have only been working with methods that accept a single parameter.
Rows 3 and 4 are the method invocations that you've already used.
For println( ) and printInWindow( ),
each expects one argument, a String. You provided
String literals as your arguments - words surrounded by a pair of
double-quote characters, e.g., "Hello World!" was your first
String.
Rows 5 - 7 are examples of how you would construct invocations of the other methods provided by class: ExtendsStuff. These methods are expecting to get numbers, arguments of type int. In these examples, I am providing int literals.
Just knowing how to use these two types (String and
int) will allow you to write some very interesting programs,
as you'll see in the next lesson. But, practice invoking the new
methods in the table above by completeing the exercises below.
If you want to see examples of proper and improper argument types, click here.
Methods may have arguments (inputs) provided to them when they are invoked. Even if a method does not expect any input, the method header specifies this with a pair of parenthesis with nothing in them. By looking at the documentation for any method, you can determine how to invoke/use it, more specifically, what you need to provide to it.