Copy Link
Add to Bookmark
Report

Java Coffee Break Newsletter Volume 3 Issue 05

eZine's profile picture
Published in 
Java Coffee Break Newsletter
 · 10 months ago

         Java Coffee Break Newsletter Volume 3, Issue 5 
http://www.javacoffeebreak.com/
ISSN 1442-3790

=================================================================

In this issue

* Featured Java Websites
* Article : Is Java driving you loopy?
* Q&A : How do I prevent caching of HTTP requests?
* Q&A : How do I synchronize access to a collection?
* Q&A : How do I convert from a Collection to an array?

=================================================================

/*
* Curious about class loaders? Confused by composition?
* Threatened by thread safety or befuddled by finalizers?
* Check out Design Techniques articles and discussion
* topics for an in-depth treatment of Java design issues.
* --> http://www.artima.com/designtechniques
*/


Featured Java Websites

Here are a selection of websites that may be of interest to
readers.


JAVA IN THE BROWSER? NO - JAVA IS THE BROWSER!

You've probably heard of Java running in a web browser--but
imagine a web browser written entirely in Java! That's the
vision behind the Jazilla Open Source Project: to produce a
freely distributable web browser written completely in Java.

Jazilla is a reference to the Mozilla project, an open source
project based on Netscape's source code. Developers can
download the source code, modify it, and contribute changes.
Dreamed of writing your own version of Netscape or Internet
Explorer? Now's your chance!

http://jazilla.sourceforge.net/


DICK BALDWIN'S JAVA PROGRAMMING TUTORIALS

Dick Baldwin's tutorials on Java are a great way to learn
more about specific Java topics, whether you're a beginner,
intermediate, or at an advanced level (they're grouped into
three streams).

Whether it's as simple as I/O and threads, or as complex as
XML and CORBA, these free tutorials are a great substitute
(or even a supplement) for a book or Java course. Even better,
they can be downloaded for offline reading, such as during lab
classes or while programming.

http://home.att.net/~baldwin.r.g/scoop/index.htm


SUN SECURITY GUIDELINES FOR JAVA DEVELOPERS

Sun has recently released an excellent set of code
guidelines, aimed at making Java systems more secure.
It's all good common sense advice, written in fairly
simple terms but for a technical audience. There are great
tips for watching which methods and variables are made public,
returning the contents of an array or collection, and much,
much more. Every programmer should review these guidelines,
as the information contained within is simple to apply and
useful.

http://java.sun.com/security/seccodeguide.html


VISUAL J++ TECHNICAL FAQ

If you've looking for technical information on Microsoft's
Visual J++ development tool, go straight to the source with
this technical FAQ from Microsoft. There's information on
Visual J++ capabilities and functionalities, how it integrated
with other Visual Studio products and components, as well as
how it compares to other tools like Borland JBuilder. There's
also links to further information from the Microsoft Visual J++
website, and some technical articles.

http://msdn.microsoft.com/visualj/technical/techfaq.asp

=================================================================
Book Review -

Author :
Publisher :
ISBN : 0672315483
Experience: Beginner - Intermediate



For more information about this title, or to order it, visit
http://www.davidreilly.com/goto.cgi?isbn=0672315483

=================================================================

Book Review -

Author :
Publisher :
ISBN : 0471345342
Experience: Beginner-Intermediate




For more information about this title, or to order it, visit
http://www.davidreilly.com/goto.cgi?isbn=

=================================================================

Article : Is Java driving you loopy?

If Java is your first programming language, then loops
(a form of iteration) can be a confusing topic. In this
article, I'll show you the different types of loops that
Java supports, and when to use them. -- David Reilly


There are three basic building blocks of programming :-

* sequence
* selection
* iteration


Most programmers will be familiar with sequence. A sequence of
programming statements are executed one after the other. For
example, the following two lines can be executed sequentially

System.out.println ("hello");
System.out.println ("goodbye");

Selection is also fairly simple. By using if / switch
statements, you can control the execution flow of code. Most
programmers are already familiar with the concept behind if
statements - it is virtually impossible to write any application
without using some form of selection.

The final concept, iteration, is the focus of this article. This
one is a little more tricky, as there are quite a few different
times of iteration statements in Java. The basic concept
behind iteration, is that a sequence of statements is repeated
until a certain condition is met. When this condition is met, the
iteration terminates, and the loop is over.


'for' loops

The simplest type of loop uses the for statement, to iterate
through a sequence of statements. Usually, a for loop will use
a counter, with a precise starting point and a precise ending
point.

// for loop, from 1 to 5
for (int i = 1; i <= 5; i++)
{
System.out.println ("count : " + i);
}

There are several components to a for loop

for ( init   ;  condition ; statement )

A check to see if the condition equates to true is made at the
end of the for loop. When the condition fails to be met, the
loop will terminate. In our previous example, the loop
terminates when our counter (i) is not less than or equal to a
value of five.It is also worth noting that each of the
components of a for loop is entirely optional. For example,
our loop could be rewritten as the following: -


// This may be more useful, as the variable
// i is created outside the scope of the
// loop.
int i = 1;
// for loop, from i to 5
for (; i <= 5; i++)
{
System.out.println ("count : " + i);
}


'while' loops

A while loop differs from the for loop, in that it only
contains a condition, and that the condition is tested at
the beginning of the loop. This means that if the condition
evaluates to false, it will not be executed at all. We call
this a pre-tested loop, because the test is made before
executing the sequence of statements contained within.

int i = 1;

while (i <= 5)
{
System.out.println ("count : " + i);
i++;
}


'do...while' loops

A variation on the while loop is the post-tested version.
Two keywords are used for this loop, the do and the while
keywords.

int i = 1;

do
{
System.out.println ("count : " + i);
i++;
}
while (i <= 5)


There is no difference between a while, and a do...while loop
other than pre-testing and post-testing.



Terminating loops abruptly

Sometimes it is necessary to stop a loop before its terminating
condition is reached. For example, a loop that searched through
an array for a particular entry should not continue once that
entry is found - to do so would only waste CPU time and make
for a slower program. Other times, you may want to skip ahead
to the next iteration of the loop, rather than performing
additional processing. Java provides two keywords for this
purpose : break and continue.

  break

The break keyword is used to terminate a loop early. Consider
the following example, which searches through an array for a
particular value.

// Check to see if "yes" string is stored
boolean foundYes = false;

for (int i = 0; i < array.length; i++)
{
if (array[i].equals("yes"))
{
foundYes = true;
break;
}
}

continue

Unlike the break keyword, continue does not terminate a loop.
Rather, it skips to the next iteration of the loop, and stops
executing any further statements in this iteration. This allows
us to bypass the rest of the statements in the current sequence,
without stopping the next iteration through the loop.

for (int i = 0; i < array.length; i++)
{
if (array[i] == null)
// skip this one, goto next loop
continue;
else
{
// do something with array[i]
.......
}
}

While this example was unnecessary, since we had an if statement
to separate program flow, there are some complex algorithms where
continue and break are extremely useful.


Summary

Loop don't need to be difficult, providing you keep in minds
these simple concepts: -

* loops are for repetition of statements
* some loops are pre-tested (for, while), others are post-tested
(do..while)
* when you have a clearly defined start and finish, a for loop
is preferable
* when you have a more complex termination condition, while
loops are preferable

=================================================================

Q&A: How do I prevent caching of HTTP requests?

By default, caching will be enabled. You must use a URLConnection,
rather than the URL.openStream() method, and explicitly specify that
you do not want to cache the requests. This is achieved by calling
the URLConnection.setUseCaches(boolean) method with a value of false.

// Create a URLConnection object
URLConnection connection = myURL.openConnection();

// Disable caching
connection.setUseCaches(false);

// Connect to remote machine
connection.connect();

=================================================================

Q&A: How do I synchronize access to a collection?

If you need a collection to be accessible to multiple threads, you
must be careful to prevent concurrent access. None of the
collection methods are synchronized, but there is a solution.
The java.util.Collections class contains static methods which
accept a Collection, List, Map, Set, SortedMap or SortedSet, and
return synchronized versions. When creating your collection, you
should immediately create a synchronized version, and use it
instead of the unsynchronized collection.

Collection type| Appropriate synchronization method
|
Collection | Collections.synchronizedCollection (Collection c)
List | Collections.synchronizedList (List l)
Set | Collections.synchronizedSet (Set s)
Map | Collections.synchronizedMap (Map m)
SortedSet | Collections.synchronizedSortedSet (SortedSet ss)
SortedMap | Collections.synchronizedSortedMap (SortedMap sm)

For example, to created a synchronized ArrayList, you'd do the
following: -

ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList (arrayList);

// Use list instead of arrayList
list.add ("some data");


=================================================================

Q&A : How do I convert from a Collection to an array?

The hard way would be to find the length of a collection, create
an array of that size, and manually traverse through every
element then assign it to the array. Fortunately, there's a much
easier solution!

Defined as a method in the java.util.Collection interface, is the
toArray() method. This returns an array of objects, which contain
the contents of a collection. Whether you're dealing with a
Set, List, or Map, any Collection will support this method.

For example, a sorted set is easily converted using the
following code: -

// Create a new sorted set
SortedSet ss = new TreeSet();

// do something with ss
Object[] array = ss.toArray();

=================================================================

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'd like to receive our newsletter, and get the latest
Java news, tips and articles from our site, then get your FREE
subscription & back issues from

http://www.javacoffeebreak.com/newsletter/

If you are an email subscriber and no longer wish to receive
the JCB Newsletter, please unsubscribe by emailing
javacoffeebreak-unsubscribe@listbot.com

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT