Home arrow Java arrow Page 2 - Conversions and Java Print Streams
JAVA

Conversions and Java Print Streams


In the conclusion to this three-part article, we'll discuss data and time conversions, character conversions, and more. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
June 28, 2007
TABLE OF CONTENTS:
  1. · Conversions and Java Print Streams
  2. · Character conversions
  3. · Format Modifiers
  4. · Flags
  5. · Precision

print this article
SEARCH DEVARTICLES

Conversions and Java Print Streams - Character conversions
(Page 2 of 5 )

Character conversions can be applied to char and java.lang.Character objects. They can also be applied to byte, short,int, and the equivalent type-wrapper objects if the integer falls into the range of Unicode code points (0 to 0x10FFFF). These
conversions are:

%c
   A lowercase Unicode character

%C
   An uppercase Unicode character

Boolean conversions

Boolean conversions can be applied to boolean primitive values and java.lang.Boolean objects. They can also be applied to all object types, in which case they’re considered to be true if the object is nonnull and false if it is null. All other primitive types are considered to be true, regardless of value. These conversions are:

%b
   “true” or “false”

%B
   “TRUE” or “FALSE”

These conversions are not localized. Even in France, you’ll see “true” and “false” instead of “vrai” and “faux.”

General conversions

There are two more conversions that can be applied to any object and also to primitive types after autoboxing. These are:

%h/%H
   The lowercase/uppercase hexadecimal form of the 
   object’shashCode, or “null” or “NULL” if the object is
   null.

%s/%S
   The result of invoking the object’sformatTo() 
   method if it implementsFormattable; otherwise, 
   the result of invoking itstoString()method, or  
   “null” if  the object is null. With%S, this value is then 
   converted to uppercase.

Example 7-4 prints a URL (which does not implementFormattable) in all of these formats.

Example 7-4.  General format specifiers

import java.net.*;

public class GeneralFormatExample {

  public static void main(String[] args) throws MalformedURLException{
    URL u = new URL(http://www.example.com/Article.html);
    System.out.printf("boolean:  %b\n", u);
    System.out.printf("BOOLEAN:  %B\n", u);
    System.out.printf("hashcode: %h\n", u);
    System.out.printf("HASHCODE: %H\n", u);
    System.out.printf("string:   %s\n", u);
    System.out.printf("STRING:   %S\n", u);
  }
}

Here’s the output from running this on a U.S.-localized system:

  boolean:    true
  BOOLEAN:    TRUE
  hashcode:   79d2cef0
  HASHCODE:   79D2CEF0
  string:     http://www.example.com/ Article.html
  STRING:     HTTP://WWW.EXAMPLE.COM/ ARTICLE.HTML

Be cautious about uppercasing. URL path components and many other things are case sensitive. HTTP://WWW.EXAMPLE.ORG/ARTICLE.HTML is not the same URL as http://www.example.org/Article.html.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials