![]() Technology Fundamentals Welcome to the Java Developer Connection Java Technology Fundamentals Newsletter. This monthly newsletter 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 Defining Static or Instance Variables and Methods
2. Making Sense of the Java Class Libraries Class Character Application Examine
4. Java Bits Storing Information
5. New to the Programming Center Unraveling Java Technology Read articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here. Java Programming Language BasicsDefining Static or Instance Variables and MethodsClass definitions begin with a basic structure of what a class extends and which interfaces are implemented. That's the basic framework. The interesting stuff happens beyond these basics. Classes consist of variables and methods. Variables hold the state of the class and methods define the behavior. For instance, if you want to define a class that maintains a counter, you might define a class as such:
Here, there is one piece of state information, the count variable, and two pieces of behavior, the two methods. The variable is named count and has
The code defines the type of state and behavior information available for a class. In the definition of the
To demonstrate, the following code fragment creates two instances of the
The second state is class variables, and second behavior is class methods. With these types of variables, one copy of the variable exists for all instances of the class. For class methods, they can only access class variables. The following class definition shows a counter maintained as a static value.
If you create multiple instances of the The following fragment demonstrates this updating:
Why might you use class, or static, variables instead of instance variables? The key reason for using static data members is to maintain a value across all instances of a class. For instance, one key reason to use them is for constants. static final int LENGTH = 4;
By using the The use of static methods can be confusing at times. Since static methods work at the class level, they should be called with the class identifier, not with an instance of the class. A better way to write the earlier usage counter example is as follows:
By replacing the instance names with the class name you more clearly see why the results print out as they do. A third print statement is added to show the proper way of getting the count. True, you don't need to use the class name when calling static methods. However, there are times where certain method calls can be confusing without it. For instance, what's the answer to the following statement?
System.out.println("12345".valueOf(54321));
Since "
When defining your own classes, examine the purpose of state and behavior information and use the Test your knowledge about static and instance variables and methods with this online quiz. Making Sense of the Java Class LibrariesClass Character
The In addition, this class provides several methods for determining a character's category: lowercase letter, digit, and so forth for converting characters from uppercase to lowercase and vice versa.
The methods and data of class
Class
In addition, the Character class has many useful static fields you can use:
Read the class documentation for more methods and fields.
The following sample application demonstrates a few of the
Results in the following: %java Charactertest Is this letter lowercase? true Is this letter uppercase? false Is this a letter? true This class is useful for verifying data before writing to files or databases, and has many other applications. Program ChallengeCreate a program that examines each character from a command line argument.
The result of your application should produce something similar to the following:
See a possible solution to Challenge. Java BitsStoring InformationMost applications store information created or configured by the user. The Java platform provides convenient packages that enable an application to write or store data, then read the information back into the application.
The classes in the The classes you need to store data and objects on a file system come in the Java 2 Platform, Standard Edition J2SE download.
You can also write data or objects to a database. To do this you need to understand the JDBC technology, an API that provides a framework for accessing any data source using the Java programming language. The
To store information in and retrieve from a database, you need the J2SE download, which includes the New to Java Programming CenterUnraveling Java Technology, an article that defines many Java acronyms and frequently used terminology, has been updated. http://java.sun.com/jdc/onlineTraining/new2java/programming/learn/unravelingjava.html
Building an Application Introduction For More InformationUnderstanding Instance and Class Members 20.5 The Class java.lang.Character Program Challenge Solution
See one possible solution to the May Program Challenge. Downloading the Java 2 PlatformFor most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit. IMPORTANT: Please read our Terms of Use and Privacy policies:
Copyright 2002 Sun Microsystems, Inc. All rights reserved. FEEDBACKTell us what you think of this supplement.
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. - Subscribe/Unsubscribe - To subscribe, go to the subscriptions page, choose the newsletters you want to subscribe to and click Update To unsubscribe, go to the subscriptions page, uncheck the appropriate check box, and click Update This document is protected by copyright.
New to Java Programming Center Supplement
Sun, Sun Microsystems, Java and J2SE are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. | |||||||||||||||||||||
|
| ||||||||||||