![]() Programming Center Supplement Welcome to the Java Developer ConnectionSM New to Java Programming Center Supplement This monthly supplement provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to the JDC's New to Java Programming Center. CONTENTS
1. Java Programming Language Basics Variable Scope
2. Making Sense of the Java Class Libraries The
4. Java Bits: More Programming Shortcuts
5. New to the Programming Center Building an Application, Part 3 Links to articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here.
7. Program Challenge Solution and Explanation
Possible solution to the
Java Programming Language BasicsVariable ScopeA variable declaration tells the compiler the name and data type for a specific piece of memory and where that variable can be legally used. The "where that variable can be legally used" is a variable's scope. When a variable comes into scope, the runtime system creates it. When it goes out of scope, the system destroys it. You can only access a variable when you are within the scope of its declaration.
What does it mean for a variable to come into scope? You define
variables within a pair of squiggly braces, an open brace
For instance, in the following class declaration, the variable
Like
This brings up the point of different types of variables. Here,
the variables
The
The args variable for the
Not shown in the
Here, the
To demonstrate the limits of scope, the following code tries to find
out which month of the year is
There are five variables declared here:
Since
In other words, the variable
Now the program produces the following results:
Since array accesses are zero based, Making Sense of the Java Class LibrariesClasses File and
|
// Import the java.io package to make the File class
// available to you without having to type the package
// name in front of the class name each time.
import java.io.*;
public class TestFileClass
{
public static void main(String[] args)
{
// Creates a reference to a file object.
File aFile = new File("data.txt");
// Creates a file object to use as
// a directory name
File aDir = new File("mydirectory");
// Checks if the file exists and that
// it is a file. If so the name and path
// are printed to the screen.
if(aFile.exists() && aFile.isFile())
System.out.println(aFile.getName() +
" exists and is located at "
+ aFile.getAbsolutePath());
else
System.out.println(aFile + "Does not exist.");
if(aFile.canWrite())
System.out.println(aFile + " can be written to.");
// Sets the file to read only.
aFile.setReadOnly();
System.out.println(aFile + "is now read only.");
// Creates a directory, giving it the
// object name. The if statement tests
// if the directory is successfully
// created.
aDir.mkdir();
if (aDir.isDirectory())
System.out.println(aDir.getName() + " is a directory.");
else
System.out.println("Directory was not created.");
}
}
|
Once you've established that a file does exist and is readable, you
can read the contents of the file a character at a time with the
FileReader class, or better yet, wrap FileReader
in BufferedReader. This is more efficient because it creates a
temporary storage buffer and it reads in a block of characters.
BufferedReader is mainly used to wrap around a Reader object for its readLine method, which reads a line of text and
returns it as a String. To do so follow these three steps:
BufferedReader object that takes a Reader object as an argument.readLine within a loop to step through each line in the
file until null is returned.
The following example demonstrates this process and assumes you've
created a file called texttoread.txt:
import java.io.*;
public class TestBufferedReader
{
public static void main(String[] args)
{
String text;
try
{
// Creates a BufferedReader object that takes a
// FileReader object as an argument. The FileReader
// associates an object with the file to be read.
BufferedReader br =
new BufferedReader(new FileReader("texttoread.txt"));
// The while loop calls the readLine method as long as
// there is a line of characters to be read.
while ((text = br.readLine()) != null)
{
System.out.println(text);
}
// Always close a file after opening.
br.close();
}
// Catch and handle the exception in the
// event that the named file cannot be found.
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
return;
}
// Catch and handle an exception if there is
// a problem reading the file.
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
|
ReadFile
The following program is used to display information about a
File object whose name is passed in from the command line.
If the file exists and is a normal file, its contents are displayed.
File closing happens within the try part of
the try-catch. If an exception happens after the file is
opened, the file doesn't close properly.
Change the scope of the BufferedReader variable below so that file
closing happens in a finally clause for the try-catch block.
import java.io.*;
import java.util.*;
public class ReadFile {
public static void main(String args[]) {
if (args.length == 0) {
System.err.println("Please provide filename to process");
System.exit(-1);
}
String filename = args[0];
try {
File file = new File(filename);
if (file.exists() && file.isFile()) {
long lastModified = file.lastModified();
System.out.println("Date : " + new Date(lastModified));
System.out.println("Length: " + file.length());
System.out.println("-----");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
} catch (IOException e) {
System.err.println("Problem: " + e);
}
}
}
|
In the November 2001 issue, you learned about the use of for loops, and you learned about arrays in the
July 2001 issue. Here, you
learn to combine for loops and arrays for a frequently used programming short cut. This technique is often used to create
a series GUI components.
As you learn GUI programming, you see code similar to this:
JLabel nameLabel = new JLabel("Name: ");
JLabel addressLabel = new JLabel("Address: ");
JLabel phoneLabel = new JLabel("phone: ");
JLabel titleLabel = new JLabel("title: ");
JTextField name = new JTextField();
JTextField address = new JTextField();
JTextField phone = new JTextField();
JTextField title = new JTextField();
|
and so forth to declare and initialize however many components the panel or application contains. Such declarations may require many lines of code and repetition of class names.
Instead, you can use for loops and arrays to do the
same thing, eliminating the need for retyping or cutting and pasting.
The process is similar to listing declarations and instantiations with the variation of setting up arrays to do the work instead:
for loopFor instance, when creating an application with labels and text fields, begin with an array for the text:
String names[] = { "Name: ", "Address: ", "Phone: ", "Title: " }
Next, specify a variable to reference an array of four labels:
JLabel labels[4];
Then set up a variable reference for four text fields:
JTextField textfields[4];
In the class constructor, or from within a method, initialize
each of the label and text field objects using a for loop.
The for loop initializes at 0, checks the length
of the labels array by calling the length method, then steps through each
label, passing each into the labels array in the next line where
each is initialized as a JLabel object:
for (int i = 0; i < labels.length; i++)
labels[i] = new JLabel(names[i]);
The for loop for the text fields works the same way:
for (int i = 0; i < textfields.length; i++)
textfields[i] = new JTextField();
Use a for loop to add each label and text field object to a
panel. In addition, you can register each as a listener using
the for loop:
for (int i = 0; i < 4; i++)
{
panel.add(labels[i]);
panel.add(textfields[i]);
textfield[i].addActionListener(this);
}
|
The application below demonstrates these concepts to compile and
run. Experiment with the code, adding more text to the String
array and creating more labels and text fields:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ForLoopShortcuts extends JFrame
implements ActionListener
{
private String names[] =
{ "Name: ", "Address: ", "Phone: ", "Title: " };
private JLabel labels[];
private JTextField textfields[];
private int size = 4;
private JPanel panel;
public ForLoopShortcuts()
{
// Calls the JFrame constructor, setting
// the title for the frame.
super("Demo of for Loop Shortcuts");
// Gets the content pane for the frame.
Container c = getContentPane();
// Set up arrays to hold
// four labels and
// four the text fields.
labels = new JLabel[size];
textfields = new JTextField[size];
// The for loop initializes at zero,
// checks the size of the labels
// array, then steps through each.
for (int i = 0; i < labels.length; i++)
// Initializes a new label object,
// assigning the Strings from the String
// array.
labels[i] = new JLabel(names[i]);
// Initializes new text field objects.
for (int i = 0; i < textfields.length; i++)
textfields[i] = new JTextField();
panel = new JPanel();
panel.setLayout(new GridLayout(size,2));
panel.setBackground(Color.white);
// Sets up a loop for the labels and
// textfields array to add to the panels
// and to register each as a listener.
for (int i = 0; i < size; i++)
{
panel.add(labels[i]);
panel.add(textfields[i]);
//panel.textfields[i].addActionListener(this);
}
c.setLayout(new GridLayout(1, 1));
c.add(panel);
//The following line ensures that the application
//closes down when exiting. This method only works
//with J2SE 1.3 to 1.4.
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
ForLoopShortcuts fls = new ForLoopShortcuts();
fls.setSize(350, 130);
fls.setVisible(true);
}
}
|
Some developers prefer to declare and initialize objects individually because it can make debugging easier while others prefer to save space and typing stress. Programming using either technique is correct and a matter of style and convenience.
Building an Application, Part 3
Part 3 of this series introduces encapsulation, event handling basics, and inner classes, demonstrating how to retrieve user data and display it on a panel.
FileInputStream and FileOutputStream
Lesson: I/O: Reading and Writing (but no 'rithmetic)
Java 2 Platform, Standard Edition
Product Family
New I/0 Functionality for Java 2 Standard Edition 1.4
J2SE, version 1.4 Summary of New Features and Enhancements
New to Java Programming Center
Building an Application Introduction
This is one possible solution to the Program Challenge:
import java.io.*;
import java.util.*;
public class ReadFile {
public static void main(String args[]) {
BufferedReader br = null;
if (args.length == 0) {
System.err.println("Please provide filename to process");
System.exit(-1);
}
String filename = args[0];
try {
File file = new File(filename);
if (file.exists() && file.isFile()) {
long lastModified = file.lastModified();
System.out.println("Date : " + new Date(lastModified));
System.out.println("Length: " + file.length());
System.out.println("-----");
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
System.err.println("Problem: " + e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignored) {
}
}
}
}
}
|
For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the Java 2, Standard Edition (J2SE) development kit.
IMPORTANT: Please read our Terms of Use and Privacy policies:
Copyright 2002 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA
Have a question about programming? Use Java Online Support.
- Note -
Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems purposes only. You have received this email because you elected to subscribe.
- Feedback -
Comments? Send your feedback on the New to Java Programming Center Supplement to: dana.nourie@sun.com
- Subscribe/Unsubscribe -
To subscribe, go to the subscriptions page, (https://softwarereg.sun.com/registration/developer/en_US/subscriptions), choose the newsletters you want to subscribe to and click Update
To unsubscribe, go to the subscriptions page, (https://softwarereg.sun.com/registration/developer/en_US/subscriptions), uncheck the appropriate check box, and click Update
This document is protected by copyright.
JDC New-to-Java Programming Center Supplement
February 2002
Sun, Sun Microsystems, Java and J2SE are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
|
| ||||||||||||