rulururu

post Java Scanner Class

February 29th, 2008

Filed under: Java — Kai @ 12:56 pm

Today I’d like to introduce to you the Scanner Class which can be used in Java 1.5 and later.

It is a very useful new class that can parse text for primitive types and substrings using regular expressions. Before arguing round and round the subject I’m gonna give you an example:

import java.util.Scanner;
import java.io.*;
 
public class Test {
 
 public static void main(String[] args) {
 
   int n;
   double d;
   String foo;
 
   System.out.print("Enter an integer,  a floating-point number, and a word: ");
   String line = Console.In.ReadLine(); 
 
   Scanner in = new Scanner(line);
 
   n = in.nextInt();
   d = in.nextDouble();
   foo = in.next();    
 
   System.out.println("integer: " + n + ", float: " + d + ", word: " + word); 
 }
 
}

For each of the primitive types there is a corresponding nextXxx() method that returns a value of that type. If the string cannot be interpreted as that type, then an InputMismatchException is thrown.

There are a number of other useful methods in the Scanner class such as skip() to jump over input, useDelimiter() defines a delimiter in place of the default white space, and findInLine() to search for substrings.

You can also give an input direcly to the scanner:

//from the keyboard:
Scanner keyboardScan = new Scanner(System.in);   
 
//from a file:
Scanner fileScan = new Scanner(new FileReader("file.txt");

A common way, from a stream, can be done very short:

int n = new Scanner( getResourceAsStream("file.txt") ).nextInt();

For detecting EOF (End of File) this is important (methods return boolean):

fileScan.hasNext();
fileScan.hasNextLine();

When with floading point numbers you’d note that for example in Germany floading point numbers are written with comma instead of dot.

Scanner scanner = new Scanner( "1,3" );
double d = scanner.nextDouble();

Won’t detect that “1,3″ is a double and return an java.util.InputMismatchException, the reason why is very logical:

The default Locale-Object for the Scanner is what Locale.getDefault() returns - on an English computer it will return Locale.English or something similar. If you like to set it to another language you can simply to that as follows:

Scanner scanner = new Scanner( "1,3" ).useLocale( Locale.GERMAN );

A detailed overview can be found in J2se 1.5 docs: Class Scanner

post String vs. StringBuilder / StringBuffer

February 26th, 2008

Filed under: .NET, Java — Kai @ 6:22 pm

Some time ago I learned when to use StringBuilder instead of String. Now I want to know the differences and advantages exactly.

As a C++ developer when working with .NET I soon notices the difference about the data types in .NET, especially the standard string object.
A standard .NET string object is immutable.

According to that this is pretty inefficient:

String s;
for (.;.;.) s += "something";

For each +=, the String’s content needs to be copied into a new instance that has a bit more space at the end for “something”.

A better solution is StringBuilder which is used to store character strings that will be changed (String objects cannot be changed). It automatically expands as needed.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.

Also in Java String objects are constant strings. Once they are initialized and populated, the value and memory allocation is set. If the value changes in any way, a new object is created for the new value.

But there’s a difference in Java:
StringBuilder was added in Java 5. Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.
It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster.

StringBuffer should be used for multi-threaded applications.
But you have to be careful the StringBuffer class is only available in Java not in .NET languages, here it’s just StringBuilder for single- and multi-threaded applications.

The usage of a .NET StringBuilder is as simple as expected:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0; i<1000; i++)
{
    sb.Append("something");
    sb.Append(12);
    sb.Append(DateTime.Today);
    sb.AppendFormat("{0}: {1} ", i, "times passed the loop");
}
 
String out = sb.ToString();

Append() is overriden for lots of types.

Using Java it’s about the same:

String sum = new StringBuffer().append(40.00).append(" Dollar").toString();

Every time you add two strings using + operator, a new string allocation is reserverd in memory and used for this new string. So let’s say, you are adding strings 100 times using + operator, there will be 100 new memory allocations. In case of StringBuilder, there will only be one memory allocation.

StringBuilder provies a lenght() and a capaitiy() method:

When creating a new StringBuilder

StringBuilder sb = new StringBuilder();

the length is is 0 but the capacity is 16.

When creating a StringBuilder with a String in constructor

StringBuilder sb = new StringBuilder("something");

now length is the length of “something” (8) and the capacity is 16 + 8

If you expect that your buffer will need a capatity of 1000 you can initalize the StringBuilder with a buffersize:

StringBuffer sb = new StringBuffer(1000);
      System.out.println(sb.length());         // prints 0
      System.out.println(sb.capacity());       // prints: 1000

The length() always returns the number of chars in the builder, the number returned by capacity() is the same or bigger than length and tells you how many chars are left in the buffer. If no buffer is left the Builder has to reallocated new memory space, which takes time.
The StringBuffer should be initalized with the expected buffersize to avoid time-consuming reallocation.

You’d always be aware of the fact that the first time creating a StringBuilder costs time - if you just add three String together it will definetely be slower to use a Builder than concatenating them the common way.

Finally I got a visualized comparison of both:

compare SB and String

The numbers at the x-axis represent the quantity of iterates.

The blue line is the performance of the pure String approach. The red line is StringBuilder at its basic settings.
The green line represents a StringBuilder initialized to the size of the final string.

StringBuilder performance can sometimes be a bit tricky because there’s kinda “break-even-point” you always keep in mind.

A simple rule of tumb is:
If you have to concatenate a string more than 10 times, it’s better to use StringBuilder.

post Basic design patterns and a book recommendation

February 23rd, 2008

Filed under: General Programming, Java — Kai @ 3:52 pm

About a year ago I got interested in the complexety of design patterns for better solving problems in software development you are circularly confronted with. When reading “Head First Design Pattern” by Eric Freeman, Elisabeth Freeman and Kathy Sierra I soon mentioned that I use the common ones of them without having recognized it before.

designpatternsI’m not as surprised about that as you might think ’cause there a several problems in software development you can just solve that way or you chose kinda “dirty” way. Even thougth the book is exhausting to read ’cause it’s not written the way most book for programmers are - it’s really abstract which means they’re almost never writing about problems that occur the development of a software product but all-time about compareable situations in real life. None the less I can without any doubt recommend it to you as a book every programmer should have read once or maybe twice.

To give you a short preview I’d like to tell you about real basic patterns you might already know very-well.

All code examples are written in Java, not because I do like Java that much, rather ’cause Java is qualified to be readed easily particularly concerning object orientated programming techniques. It doesn’t have much overhead in syntax that would distract you from the things I really want to you see. Of course patterns are used in every object orientated language they just differ form each other by the way there’re syntactical written and some programming languages have features that help to implement them easier.

  • The Factory Method
  • It’s its intention to separatly construct a complex object so that the same construction process can create different representations.

    It provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
    If there’s more than one way to created an object factory methods are right choice. Especially when the class that calls a factory method delivers the required information for creating an instance.

    The factory method pattern should be used in those cases:

    * The class that calls the factory method doesn’t know exactly what instance has to be created.
    * The calling class is not pretty sure howto created the needed instance.
    * The calling class gives information to the factory method that helps/is required to created the needed instance of an object.
    * The class that provides a factory method to gives permission of its creation to its subclasses.

    Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework.

    This example of a facotory method creates an instance of an ImageReader. Each time the program reads an image it needs to create a reader of the appropriate type based on some information in the file.

    public class ImageReaderFactory 
    {
        public static ImageReader getImageReader( InputStream is ) 
        {
            int imageType = figureOutImageType( is );
     
            switch( imageType ) 
            {
                case ImageReaderFactory.GIF:
                    return new GifReader( is );
                case ImageReaderFactory.JPEG:
                    return new JpegReader( is );
                // etc.
            }
        }
    }
  • The Abstract Factory Method
  • It provides an interface to create and return one of several families of related objects. It encapsulated a group of individual factories that have a common theme.

    Abstract factory methods work like classic factory methods but they’re a bit enhanced. Using them is recommented when:

    * A class should work totally independent from the creation of the instances of objects it works with.
    * A group of objects shall be created ’cause they’d be used together
    * A class library should offer a whole bunch of implementations.

    Typically you find it in GUI libraries that offer different themes for their controls.

    At first we design a abstract factory that looks very clear:

    public abstract class AbstractGUI 
    {
         public Button CreateButton()
         {
            return new Button();
         }
    }

    Afterwards two concret class get designed that extends the abstract class and its methods override the deliverd ones.

    public class XPStyle extends AbstractGUI 
    {
       @Override
       public Button CreateButton()
       {
          return new XP_Button();
       }
    }
     
    public class GnomeStyle extends AbstractGUI 
    {
      @Override
       public Button CreateButton()
       {
          return new Gnome_Button();
       }
    }

    The GUI class has a method for the name of a control:

    public class AbstractGUI 
    {
    private String name;
     
      public String getName() 
      {
        return this.name;
      }
     
      public void setName(String name) 
      {
        this.name = name;
      }
    }

    This is the most important part, that is used when a particular button is created:

    public class XP_Button extends Button
    {
      public XP_Button()
      {
        super();
        setName(”XP Button”);
      }
    }

    Eventually the great advantage of this is that you don’t have to worry about what kind of objects you’re building the factory does ease your workload.

  • The Builder Pattern
  • It can be used to clearify a complex building progress of an big object.

    This should explain how a abstract builder works:

    abstract class ButtonBuilder
    {
        protected Button button;
     
        public Button getButton()
        { 
            return button; 
        }
        public void BuildNewButton()
        { 
            button = new Button(); 
        }
     
        public abstract void buildStyle();
    }

    In this class the concret building is done:

    class MySuperButtonBuilder extends ButtonBuilder
    {
        public void buildStyle()
        { 
            button.RoundBorders(1);
            button.FancyLook(1);
        }
    }

    It separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.

  • The Prototype Pattern
  • The prototype means making a clone. This implies cloning of an object to avoid creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object. We use the interface Cloneable and call its method clone() to clone the object.

    public class Data implements Cloneable 
    {
        public Object clone()
        {
            try{
                return this.getClass().newInstance();
            }
            catch(InstantiationException e)
            {
               e.printStackTrace();
               return null;
            }
        }
    }
     
    public class DataCreator
    {
       private Data d;
     
         public DataCreator(Data d)
         { 
             this.d = d; 
         } 
         public Data GetData() 
         { 
           return (Data)data.clone(); 
         } 
         public Object clone()
         { } 
    }
     
    public class SecretData extends Data 
    {
    //concrete prototypes to clone
    }

    Working with that can be done in that way:

    Data dataprotoype = new SecretData(); 
    Data mydata = new SecretData(dataprotoype); 
     
    for(int i=0; i<100; i++) 
       tmpData = mydata.GetData();

    It starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.

  • The Singleton Pattern
  • Is the one that provides a class of which there can be no more than instance, and provides a single global point of access to that instance.
    Sometimes it’s appropriate to have exactly one instance of a class: e.g. for window managers, print spoolers etc.
    The most common usage is a logging class that does something with alle logs and exceptions that are thrown.

    If an object is needed in the whole application a singleton is better than a real global object. But you’d also be careful when implementing lots of singletons ’cause you surly don’t want
    give up object orientation and have everything globally just for easier usage.

    A singleton should give you those possibilies:

    * Create the only existing object of a class
    * Provide a global point of access to the object (getInstance(); ).

    The first thing you’ve to do when avoiding the creation of multible objects from one class is to set the constructor to private access.
    Then you’ve provide your own method for creation of an object that checks if another object exists before creating. Easy but effective!

    private static Logger log;
     
    private Logger() 
    {
    //private constructor
    }
     
    public static Logger getInstance() 
    {
     if (Logger.log == null) 
     {
       Logger.log = new Logger();
     }
        return Logger.log;
    }

    To improve this solution we’d make it thread-save. Java provides a synchronized keyword that ensures the synchronized access. Java makes it really simple, C# needs a variable that is volatile (indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.) which is check before creating a new object.

    public static synchronized Logger getInstance () 
    {
        if (Logger.log == null) {
          Logger.log = new Singleton ();
        }
        return Logger.log;
      }

Design patterns are an extensive topic you could write about for hours. My aim was just the give you a thorough insight into that issue without going to deep and making it more complex than necessary.

To have basic ability in using those patterns is also important ’cause when discussing with other programmers you don’t want to discuss the whole logic you’ll write you just want to discuss about a certail technical term and if it matches to solve the given problem.

Here you can buy Head First Design Patterns published by O’Reilly on Amazon.com

post Don’t compare objects mindless

February 18th, 2008

Filed under: .NET, Java — Kai @ 11:27 pm

When comparing objects in .Net I sometimes have to think about what way is the right one for my current case.

Imagine we got two objects called a and b that hold some data. There are several ways to compare them to each other - but it fact it’s not as confusing as I first thought.

Basically we can compare two different things of an object:

  • Identity (reference equality): Two objects are identical if they actually are the same object in memory. If the point to the same reference in memory they are equal.
  • Equivalence (value equality): Two objects are equivalent if the value or values they contain are the same.
int a = 3; 
int b = 3;

In most cases you’d regard them as equal ’cause their values are the same. Compared by identitiy they’re not equal.
This is the first you’d always keep in mind.

if(a.Equals(b))
{ 
Console.WriteLine("a equals b...");
}

Equals() is a virtual method on System.Object. It’s intended to test for identity or equivalence as appropriate. If you have an own type definition you’d like to be compareable you can override it in your class. Althougth it’s not necessary because there’s a default override of .Equals() in the base class. System.ValueType which will work for any structs you set up. Before overwriting it you’d consider that it uses reflection, which is slow, and involves a certain amount of boxing.

Value types get refleced over their internal fields to see if they are all equal.
For reference types, the situation is different. In general I’d expect Equals() for reference types to do an identity comparison but certain reference types aren’t lightweight enough to work as value types, but nevertheless have value semantics. That means most reference types are compared by reference but some like System.String are compared by value.

if(object.Equals(a, b))
{ 
Console.WriteLine("a equals b...");
}


object.Equals(object, object)
is a static method on the object class. This method was designed to reduce effort for programmers to check if a is null before calling a.Equals(b).
Generally it’s about the same as above mentioned a.Equals(b) but it also returns true if both objects are null ’cause they point to the same reference.

if(object.ReferenceEquals(a, b))
{ 
Console.WriteLine("a has same reference as b...");
}

object.ReferenceEquals(object, object) compares the addresses on the memory where the objects are.
Normally whether value types occupy the we don’t care about the memory addresses of them when comparing. It isn’t relevant for anything we’d want to normally use them for.

The difficulty comes from the fact that ReferenceEquals expects two System.Objects as parameters. This means that our value types will get boxed onto the heap as they are passed in to this routine. Normally, because of the way the boxing process works, they will get boxed separately to different memory addresses on the heap.

String a = "test", b="test";
if(a == b)
{ 
Console.WriteLine("a is equal to b...");
}

== is propably the most used operator for equality testing.
For value types within the .NET Framework, == is implemented as you would expect, and will test for equivalence (value equality).

If you use the == operator with reference types without thinking, bad things can happen. For example if you compare data structures which are not the same but contain the same value they’ll be compared as objects which means the comparison is done for reference and the result will be false even though the values are the same.

If you’d like to compare e.g. as System.String by reference you can do that as well by casting them to object.

String a = "test", b="test";
if ((object) a == (object) b)
{
Console.WriteLine("a and b point have same reference");
}

a is b checks the type of the the objects it is given. It’s not the comparison you might expect by the name of the operator.

int a = 3, b=4;
if(a is b)
{ 
Console.WriteLine("a and b are both integer");
}

In Java the meaning of those methods is about the same but when using the == operator you’d be careful because it does always a reference comparison - that’s why most people when programming Java for the first time don’t get the expected results. When comparing Strings in Java you’d always use equals().

static String s1 = "test";
static String s2 = new String ("test");
public static void main (String [] args)
{
  System.out.println ((s1 == s2) ? "same reference" : "not same reference");
  System.out.println ((s1.equals (s2)) ? "same value" : "not same value");
}

C# makes comparing strings easy even for a novice programmer and without sacrificing flexibility. The compiler is smart enough to realize that the == operator, when used with Strings, compares values rather than references. The option to compare references, of course, is still open to the programmer.

Always make sure that you know what you want to compare before using one of those methods/opertors without thinking.

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)