home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
lect02b
evaluating expressions
and using JOptionPane to get input
quiz02—quiz 2: 2009.Jan.30
Our examples will use based on:
String msg;
msg = "hello";
|
(Remember the memory-diagram that goes with this:
The declaration line yields
+-----------+
msg | |
+-----------+
and then the initialize results in the picture
+-----------+
msg | "hello" |
+-----------+
Evaluation an Expression, step-by-step
Okay, let's consider an example where we use the
result of a method call as an input to another method call:
Math.sqrt( Math.sqrt(16) )
// =>
Math.sqrt( 4.0 )
// =>
2.0
|
We use “=>” to mean
“Java transforms one expression into the next,
in order to evaluate the expression.”.
It's pure coincidence that we used Math.sqrt twice;
here's another example:
Math.sqrt( msg.length() )
// =>
Math.sqrt( 5 )
// =>
2.23606797749979
|
Sometimes, Java does automatic conversions for us.
Consider println, which must be given a String
(and doesn't return an answer at all — it's a void method).
System.out.println( msg.length() );
// =>
System.out.println( 5 );
// This is a bit problematic, since we are passing an int to println,
// but really it wants a String.
// It turns out, Java is inserting a method call for us1:
// =>
System.out.println( Integer.toString(5) );
// =>
System.out.println( "5" );
// =>
// void [no answer, although some screen-pixels got painted.]
|
Question:
Why is the following non-sensical?:
(System.out.println( msg )).length()
|
The truth about variables
If you've been extremely nit-picky (and I hope you are!),
you might be wondering about two things:
-
We've said that whenever you use a variable msg,
Java really goes and fetches the value
stored in the box labeled msg.
-
The syntax for method-call is
“object.methodName(args…)”,
and yet we have always used a variable in front of the dot.
(Gee,
maybe our syntax should actually be
“variable.methodName…”?)
These two are actually consistent:
when evaluating a variable Java really does just fetch the contents
of the indicated box,
and sometimes that box might contain a String,
which is one type of object.
Here's what really happens:
Math.sqrt( msg.length() )
// =>
Math.sqrt( "hello".length() )
// =>
Math.sqrt( 5 )
// =>
2.23606797749979
|
The stop of replacing a variable
with it's value is usually glossed over in books,
but it really does happen.
We can also amend our other example:
System.out.println( Integer.toString(msg.length()) );
// =>
System.out.println( Integer.toString("hello".length()) );
// =>
System.out.println( Integer.toString(5) );
// =>
System.out.println( "5" );
// =>
// void [no answer, although some screen-pixels got painted.]
|
We can also call a method, and then take the result
and call another method:
(msg.toUpperCase()).charAt(3)
|
which is the same as
String msgShouted;
msgShouted = msg.toUpperCase();
msgShouted.charAt(3)
|
You can choose to use variables to hold temporary sub-results
or not;
either way is fine.
JOptionPane
javax.swing.JOptionPane
is (the rather unwieldy) name of an object that
knows how to put up dialog-boxes.
In particular:
javax.swing.JOptionPane.showInputDialog( "What is your name?" )
|
Returns a String.
Most commonly, you want to remember the result for future use:
String theUsersName;
theUsersName = javax.swing.JOptionPane.showInputDialog( "What is your name?" );
|
Of course, you don't need to;
you could even call one method on the result:
javax.swing.JOptionPane.showInputDialog( "What is your name?" ).toUpperCase()
|
converting Strings into numbers
JOptionPane's
showInputDialog
is a nice method,
but it only returns a String.
javax.swing.JOptionPane.showInputDialog( "How old are you?" )
// [after some typing from the user:] =>
"43"
|
What if we want to use this String as a number?
We'll have to call a method, to do this conversion.
It turns out, if you scour the documentation, that there are
two useful methods:
Integer.parseInt,
and
Double.parsedouble.
For example:
Integer.parseInt( "23" )
// =>
23
Double.parseDouble( "-23.45e3" )
// =>
-23450.0
|
Aside: Difference in book terminology:
In lecture, we use the term “argument” to mean
the actual value given to a method (like 5
or "hello"),
and the term “parameter”
will mean the name which the method gives to its argument
(like x or frameNum).
In the book, Horstmann uses the term “parameter” to
mean both these concepts.
I find this a bit confusing, myself, since they really are
distinct concepts.
The terms
“actual parameter”
and
“formal parameter”
are also used for our “argument” and “parameter”
respectively.
1
Technically, Java inserts that method call even before msg.length
gets called.
That is,
Java rewrites
System.out.println( msg.length() ); |
as
System.out.println( Integer.toString(msg.length()) ); |
before it compiles the program.
↩
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs