Showing posts with label toString. Show all posts
Showing posts with label toString. Show all posts

Sunday, 15 January 2012

Playing with double datatype using Java

Do you guess the output values of the following code snippets (in  Java) ? .


                        double d5 =99999.99;
                        double d6 =999999.99;
                        double d7 =9999999.99;
                        double d8 =99999999.99;
                        double d9 =999999999.99;
                        double d10=9999999999.99;

                        System.out.println("d5 ="+d5);
                        System.out.println("d6 ="+d6);
                        System.out.println("d7 ="+d7);
                        System.out.println("d8 ="+d8);
                        System.out.println("d9 ="+d9);
                        System.out.println("d10="+d10);

Here is the output:




d5 =99999.99
d6 =999999.99
d7 =9999999.99
d8 =9.999999999E7
d9 =9.9999999999E8
d10=9.99999999999E9

Have you noticed the values of d8, d9 and d10?. The output value is in  E (exponential) format. Java is internally doing this conversion while convert the datatype double to String.  If the value is  greater than or equal to 10-3 but less than 107, then the output will be normal. Otherwise the value will be in Exponential.

To print the values all in normal format, use the "java.text.DecimalFormat" class. Check the following code snippet.


                        DecimalFormat d = new DecimalFormat("##.##");
                        System.out.println("d5 ="+d.format(d5));
                        System.out.println("d6 ="+d.format(d6));
                        System.out.println("d7 ="+d.format(d7));
                        System.out.println("d8 ="+d.format(d8));
                        System.out.println("d9 ="+d.format(d9));
                        System.out.println("d10="+d.format(d10));
The output is


d5 =99999.99
d6 =999999.99
d7 =9999999.99
d8 =99999999.99
d9 =999999999.99
d10=9999999999.99

Refer the following URL to get the more details about the Double,.

Thursday, 14 July 2011

Stringifying Java Arrays

J2SE 5 provided significant new language features that made Java significantly easier to use and more expressive than it had ever been. Although "big" new J2SE 5 features such as enums, generics, and annotations, there was a plethora of new APIs and methods that have made our lives easier ever since. In this post, I briefly look at two of my favorites when dealing with Java arrays: the Arrays.toString(Object[]) method and the Arrays.deepToString(Object[]) method.

The Arrays class has been around since JDK 1.2, but the methods for conveniently and simply converting arrays to a readable String including relevant array content without using Arrays.asList() were added with J2SE 5. Both Arrays.toString(Object[]) and Arrays.deepToString(Object[]) are static methods that act upon the arrays provided to them. The former, Arrays.toString(Object[]), is intended for single-dimension arrays while the latter, Arrays.deepToString(Object[]), is intended for multi-dimensional arrays. As my example later in this post will demonstrate, the multi-dimension deepToString will produce expected results even for single-dimension arrays. The two methods also provide easy and safe null handling, something that I appreciate.

The next code listing is for a simple demonstration class that demonstrates trying to put the contents of various types of Java arrays into a String format. The types of arrays demonstrated are a single dimension array, a double dimensional array representing multi-dimensional arrays of various sizes, and an array that is really just null. The three methods demonstrated for getting a String out of these three types of arrays are (1) simple Object.toString() on each array (implicitly in case of null array to avoid the dreaded NullPointerException), (2) Arrays.toString(Object[]), and (3) Arrays.deepToString(Object[]).
package com.vaani.arraytest;

import java.util.Arrays;
import static java.lang.System.out;

/**
* Simple demonstration of Arrays.toString(Object[]) method and the
* Arrays.deepToString(Object[]) method.
*/
public class Array2String
{
/**
* Demonstrate usage and behavior of Arrays.toString(Object[]).
*/
private static void demonstrateArraysToString()
{
out.println(
"Single Dimension Arrays.toString: "
+ Arrays.toString(prepareSingleDimensionArray()));
out.println(
"Double Dimension Arrays.toString: "
+ Arrays.toString(prepareDoubleDimensionArray()));
out.println(
"Null Array Arrays.toString: "
+ Arrays.toString(prepareNullArray()));
}

/**
* Demonstrate usage and behavior of Arrays.deepToString(Object[]).
*/
private static void demonstrateArraysDeepToString()
{
out.println(
"Single Dimension Arrays.deepToString: "
+ Arrays.deepToString(prepareSingleDimensionArray()));
out.println(
"Double Dimension Arrays.deepToString: "
+ Arrays.deepToString(prepareDoubleDimensionArray()));
out.println(
"Null Array Arrays.deepToString: "
+ Arrays.deepToString(prepareNullArray()));
}

/**
* Demonstrate attempting to get String version of array with simple toString()
* call (not using Arrays class).
*/
private static void demonstrateDirectArrayString()
{
out.println("Single Dimension toString(): " + prepareSingleDimensionArray().toString());
out.println("Double Dimension toString(): " + prepareDoubleDimensionArray());
out.println("Null Array toString(): " + prepareNullArray());
}

/**
* Prepare a single-dimensional array to be used in demonstrations.
*
* @return Single-dimensional array.
*/
private static Object[] prepareSingleDimensionArray()
{
final String[] names = {"Aaron", "Bianca", "Charles", "Denise", "Elmer"};
return names;
}

/**
* Prepare a double-dimension array to be used in demonstrations.
*
* @return Double-dimensional array.
*/
private static Object[] prepareDoubleDimensionArray()
{
final Object[][] namesAndAges = {
{"Aaron", 10}, {"Bianca", 25}, {"Charles", 32}, {"Denise", 29}, {"Elmer", 67}};
return namesAndAges;
}

/**
* Prepare a null array.
*
* @return Array that is really null.
*/
private static Object[] prepareNullArray()
{
return null;
}

/**
* Main executable function for demonstrating Arrays.toString(Object[]) and
* Arrays.deepToString(Object[]) methods.
*/
public static void main(final String[] arguments)
{
out.println("\n\n\nDemonstrating direct array to string using toString():");
demonstrateDirectArrayString();

out.println("\n\n\nDemonstrating Arrays.toString() to get arrays as String:");
demonstrateArraysToString();

out.println("\n\n\nDemonstrating Arrays.deepToString() to get the String:");
demonstrateArraysDeepToString();
}
}


The above code exercises the three mentioned approaches for getting a String out of an array on the three different types of arrays: single dimension, multi dimension, and null array. The output from running this code demonstrates the utility of the different approaches. That output is shown next.

Demonstrating direct array to string using toString():
Single Dimension toString(): [Ljava.lang.String;@3e25a5
Double Dimension toString(): [[Ljava.lang.Object;@19821f
Null Array toString(): null
///////////
Demonstrating Arrays.toString() to get arrays as String:
Single Dimension Arrays.toString: [Aaron, Bianca, Charles, Denise, Elmer]
Double Dimension Arrays.toString: [[Ljava.lang.Object;@addbf1, [Ljava.lang.Object;@42e816, [Ljava.lang.Object;@9304b1, [Ljava.lang.Object;@190d11, [Ljava.lang.Object;@a90653]
Null Array Arrays.toString: null


///////////
Demonstrating Arrays.deepToString() to get the String:
Single Dimension Arrays.deepToString: [Aaron, Bianca, Charles, Denise, Elmer]
Double Dimension Arrays.deepToString: [[Aaron, 10], [Bianca, 25], [Charles, 32], [Denise, 29], [Elmer, 67]]
Null Array Arrays.deepToString: null

The code above and its corresponding output lead to several observations:



  1. Simple Object.toString() on arrays is seldom what we want as it only prints the String representation of the array itself and not of its contents.
  2. Arrays.toString(Object[]) will print a String representation for multi-dimensional arrays, but this representation suffers the same drawbacks as Object.toString() after the first dimension. The first dimension (and only dimension for a single dimension array) gets put into an expected String, but deeper dimensions have the same Object.toString() treatment.
  3. Arrays.deepToString(Object[]), while intended for multi-dimensional arrays, produces the expected results for both single and multi-dimensional arrays.
  4. Both Arrays.toString(Object[]) and Arrays.deepToString(Object[]) handle null array gracefully, simply returning a String "null".

I tend to use Java Collections far more than I use Java arrays. However, when I do need to work with arrays, it is nice to have the many useful features of the java.util.Arrays class. As this post has demonstrated, Arrays.toString(Object[]) and Arrays.deepToString(Object[]) are particularly valuable in obtaining a useful String representation of an array's contents. The java.util.Arrays class provides similar "deep" methods for performing equals and hashCode functionality on multi-dimensional arrays: Arrays.deepEquals and Arrays.deepHashCode.

Wednesday, 22 June 2011

String utility function : Converting int[] to String

Method 1 - Writing our own function

public static String toString(int[] intArray) {

String separator =
",";

StringBuilder sb =
new StringBuilder("");

if (intArray != null && intArray.length > 0) {

for (int i = 0; i < intArray.length; i++) {

sb.append(intArray[i]);

if (i < (intArray.length - 1)) {

sb.append(separator);

}

}

}

return sb.toString();

}

Method2 - Using Arrays.asList()
Arrays.asList(intArray).toString()

Thursday, 28 April 2011

Easy Java Bean toString() using BeanUtils

Its always good to write toString() function for the bean, but doing it manually is really tedious for lazy developers, where one concatenates the fields in the class to create a toString() method.  This code snippet using Apache Commons (a.k.a. Jakarta Commons) is very helpful for just such occasions:
public String toString() {
try {
return BeanUtils.describe(this).toString();
} catch (Exception e) {
Logger.getLogger(this.getClass()).error("Error converting object to String", e);
}
return super.toString();
}

Wednesday, 27 October 2010

toString() for Enums

public enum Shape { RECTANGLE, CIRCLE, LINE }
Shape drawing = Shape.RECTANGLE;   // Only a Shape value can be assigned.
System.out.println(drawing);    // Prints RECTANGLE

Override toString method for All Enums
A semicolon after the last element is required to be able to compile it. More details on overriding enum toString method can be found.
public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE; //; is required here.

@Override public String toString() {
//only capitalize the first letter
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}


Override toString method element by element
The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toString() method. For example,
public enum MyType {
ONE {
public String toString() {
return "this is one";
}
},

TWO {
public String toString() {
return "this is two";
}
}
}
Running the following test code will produce this:
public class EnumTest {
public static void main(String[] args) {
System.out.println(MyType.ONE);
System.out.println(MyType.TWO);
}
}
-------------
this is one
this is two
Another interesting fact is, once you override toString() method, you in effect turn each element into an anonymous inner class. So after compiling the above enum class, you will see a long list of class files:
MyType.class
MyType$1.class
MyType$2.class