![]() 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 JavaTM Programming Center. CONTENTS
1. Java Programming Language Basics
2. Making Sense of the Java Class Libraries
4. Java Bits
5. New to the New to Java Programming Center
7. Program Challenge Solution and Explanation
Java Programming Language BasicsConverting a String to a Numerical Value
A common need of the Java technology developer is to convert
strings to numerical values. Once converted, you can manipulate
that value like any other data primitive (for instance, an
To convert a The following list shows the mapping from primitive type to wrapper class. In almost all cases, the wrapper class has the same name as the primitive data type, with the first letter capitalized. Primitive to Wrapper Class
Each of these wrapper classes (except the
Simply call the method from the appropriate wrapper class,
and your For instance: String myString = "12345"; int myInt = Integer.parseInt(myString);
converts the contents of the All but the boolean conversions are done by similarly named methods, but still all the method names are different:
There is one exception: The
// The variable e is the character e in the string Hello
String hello = "Hello";
char e = hello.charAt(1);
If the Making Sense of the Java Class LibrariesThe NumberFormat ClassPrinting numbers to the screen can produce some odd or undesired results. For instance:
results in: He paid 400000.0 for the house.
Adding the symbol
You can control display format and arrange input into the needed
output format by using the This class provides the interface to format and parse numbers, and includes methods to determine which locales have number formats and what their names are. The class has methods for three standard formats:
To format a primitive, start by returning an object of type
NumberFormat nf = NumberFormat.getCurrencyInstance(); To be certain the amount is formatting correctly for a specific country, you specify the locale with: NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); Now, the program above can be rewritten as follows:
Results in: He paid $400,000.00 for the house. Program ChallengeNumbers ConversionUse what you learned to create an application that reads arguments from the command-line. The rules for handling command line arguments are as follows:
Once the arguments are processed, display the resulting value as currency, with the currency symbol, commas between thousands (assuming US locale), decimal point, and two digits after the decimal point. When executed with the following command line: java Numbers 1 2 3 4 5 2.3 3 4 4444.4 a Your program should produce results similar to the following: Arg: 10 invalid (a) The answer is $184,442.60 Java Bits:AWT Versus Project SwingA common misconception is that Project Swing is a replacement for the Abstract Window Toolkit (AWT). In fact, Swing is a set of mostly lightweight components built on top of the AWT. Originally AWT was designed to create simple interfaces for applets. Swing provides replacements for AWT's heavyweight components and also includes components and tools commonly used in more complex user interfaces.
A heavyweight component relies on the local platform's windowing
system to determine the look and feel of components. Each
heavyweight component has a peer class from the package
A lightweight component, on the other hand, is written completely in the Java programming language and does not have a native peer. To accomplish the look and feel of a specific platform, you can use Swing's Pluggable Look and Feel architecture and API. In application programming, the AWT layout managers provide a
To complete a complex application written in the Java programming language, you'll need to understand a little about AWT, and a lot about Project Swing. New to the New to Java Programming CenterBuilding an Application: Part 1Learn about classes, objects, and methods in the first part of this tutorial that teaches Java technolgies and how they fit together in a dive log application. In addition, you'll learn about packages and compiling files. For More InformationFor More Information Essentials Part 2, Lesson 2: Converting Strings to Numbers and Back Program Challenge SolutionThis is one possible solution to the Program Challenge:
Downloading the Java 2 PlatformFor most Java development, you need the class libraries, compiler, tools, and run-time environment provided with the Java 2, Standard Edition development kit.
IMPORTANT: Please read our Terms of Use and Privacy policies: 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. - Unsubscribe - To unsubscribe to this newsletter, go to the subscriptions page uncheck the "JDC New-to-Java Supplement" checkbox, and click "Update". - Subscribe - To subscribe to other JDC mailings, go to the subscriptions page choose the newsletters you want to subscribe, and click "Update". - Feedback - Comments? Send your feedback on the JDC Newsletter to: jdc-webmaster - Copyright -
Copyright 2001 Sun Microsystems, Inc. All rights reserved. This document is protected by copyright.
JDC New-to-Java Programming Center Supplement 1 As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform. --> | |||||||||||||||||
|
| ||||||||||||