Copy Link
Add to Bookmark
Report
Java Coffee Break Newsletter Volume 1 Issue 03
Java Coffee Break Newsletter Vol 1, Issue 3
ISSN 1442-3790
Welcome to the third Java Coffee Break Newsletter. This issue has
some information about Swing, free offers from the Java Lobby, and
two new Q&A articles. Enjoy!
- - - - - - - - - - - - - - - - - - - - - - - -
Java Coffee Break Updates
* Java Tutorials
* Free offers - Join the Java Lobby
* Swing/JFC namespace news
* Q&A : How can I generate random numbers?
* Q&A : What's the difference between = & ==?
1. Java Tutorials
If you're new to the Java programming language, and need a little
help, then try some of the free tutorials available from the
Java Coffee Break. These aren't your normal tutorials - they don't
require extensive Java experience or knowledge.
All our Java tutorials can be found at
http://www.davidreilly.com/jcb/tutorials.html
2. Free offers - Join the Java Lobby
The Java Lobby is an independent group of Java software developers.
They help promote the Java language, and offer benefits and
special promotions to their members. Membership is free!
Recent offers include a free subscription to Java Developer's
Journal, and a copy of ZeroG's InstallAnywhere tool. For more
details, check out http://www.javalobby.org/
3. Swing/JFC namespace news
For developers who have used the Swing / Java Foundation Classes
you may have been surprised to find that the package names have
changed in Java 1.2. A new proposal, to place swing in a new
javax.swing package has been made. For more information, see
Sun's statement at
http://java.sun.com/products/jfc/package.html
- - - - - - - - - - - - - - - - - - - - - - - -
Q&A : How can I generate random numbers?
Generating random numbers is simple. Provided as part of the java.util
package, the Random class makes it easy to generate numbers.
Start by creating an instance of the Random class
// Create an instance of the random class
Random r = new Random();
Now, you must request a number from the generator. Supposing we
wanted an integer number, we'd call the nextInt() method.
// Get an integer
int number = r.nextInt();
Often you'll want numbers to fall in a specific range (for example,
from one to ten). We can use the modulus operator to limit the range of
the numbers. This will give us the remainder of the number when divided
by ten (giving us a range -9 to +9). To eliminate negative numbers, if
the number is less than one we'll add ten to it. This gives us random
numbers between one and ten!
// Accept only up to ten
int random_number = number % 10;
if (random_number < 1) random_number = random_number + 10;
Creating random numbers in an application or applet really isn't that
difficult. Just remember to limit the range of your numbers, and to
import the java.util.Random class.
- - - - - - - - - - - - - - - - - - - - - - - -
Q&A : What's the difference between = & ==?
One of the most common programming mistakes that one can make is to
confuse the equal sign (=) with a double equal sign (==). While
experienced programmers can still make the same mistake, knowing the
difference between the two is a good way to avoid it.
When we wish to assign a value to a variable or member, we use the
equals sign (=). As an example, the following would assign the
value of three to the variable a.
a = 1 + 2; // assignment operation
When we wish to make a comparison, such as in an if statement, we use
the double equals sign (==). A simple example would be the
following
if ( a == b ) then System.out.println ("Match found!");
Now consider what would have happened if we used an assignment
operator instead. A would be assigned the value of B - destroying
the current contents of A and giving us an incorrect comparison.
Finding this type of error can be difficult, because the two
operators look so similar. So why does Java use such confusing
operators? Java draws its roots from C, which shares the same
problem. Unfortunately, its something that all Java programmers must
learn to live with.
- - - - - - - - - - - - - - - - - - - - - - - -
The Java Coffee Break Newsletter is only sent out to email subscribers
who have requested it, and to readers of the comp.lang.java.programmer
and comp.lang.java.help newsgroups. If you are an email subscriber and
no longer wish to receive the JCB Newsletter, please unsubscribe using
the WWW form located at
http://www.davidreilly.com/jcb/newsletter/unsubscribe.html