Friday, 10 May 2013

Top 3 Examples of variable arguments or varargs feature in java

Variable Arguments feature in java


Variable arguments feature is one of java language feature introduced in java 5. Java 5 introduced lot of new features like Enum feature[] etc.

method have multiple arguments, this arguments count are fixed, with Variable arguments feature in java5, method can have multiple arguments(zero to many) to be passed without defining number of arguments by specifying variable argument syntax. variable arguments are spcified in method defination by ellipse (...).

Before Varargs feature or prior to java 1.5:-
prior to java 1.5 version, developers has no chocie of variable arguments supplied but we

Java has a feature of method overloading in which same method can have different versions with changing arguments.


Advantages of Varargs feature:-


variable declarion not required at compile time or run time.
Clients have free control over passing variable arguments to Service API methods
backward compatability support for prior java versions




Variable argument syntax


We have to delcarate DataType ... arguments in method declaration


public void method(DataType ... arguments)

arguments are followed by ellipse ... meaning method accepts atleast zero arugments to mutiple arguments

Rules for Variable arguments


1. Varargs can be combined with other normal arguments.
2. Only one varargs in any method declaration
3. if there is combination of varargs and normal arguments, varargs declaration should be defined at last

JVM Execution for varargs:-


when variable argument is declared in method, java compiler of JVM loads that method and create a array of arguments of Data type


Varargs Simple example


package com.cloudhadoop.varargs;

public class VarargsExample {

static void print(String... words) {
String sentence = null;
for (String word : words) {
sentence = sentence + word;
}
System.out.println(" " + sentence);
}

public static void main(String[] args) {
print("Hi", "How ", "are", "You");
}

}



and output is :- Hi How are you

Iteration of Varable arguments in java example


Variable arguments variable name is an reference to array of argument type. so we can use for each loop to iterate variable arguments


static void print(String... words) {
String sentence = null;
for (String word : words) {
sentence = sentence + word;
}

Friday, 3 May 2013

Top 3 Java Text Formatting examples : MessageFormat


Why MessageFormat is introduced:-

Before MessageFormat is introduced, we used to process the messages using String concatenation,
the disadvantages with String class processing is Strings are immutable objects, more objects are created in heap memory
and these messages order is not same for every language, because of this problems,
Sun has introduced text format classes like MessageFormat
String stringText="Hi"+ name+ "How are u";

MessageFormat is class in java.text package, introduced in java 5 language, and this text format class in java used to provide capabilities for Internalization.

java.util.MessageFormat class provides capabilities to display localization specific message and format the messages as per language specific.In Any application, messages are displayed to users when validation fails or request is submitted successfully.In real world programs, messages are stored in resource bundles or property files, prorgrams read the properties files based on language.

Basic example or usage of MessageFormat

Object userInformation={"John","success"};
String messageText=" user {username} data is submitted with {status} message";

MessageFormat messageFormatExample=new MessageFormat(messageText);
System.out.println(messageFormatExample.format(userInformation));

and output is user John data is submitted with success message


In above code, format() method formats strings by receiving {0},{1} arguments and MessageFormat is easy to learn and implement


How to format text message contains Date fields


MessageFormat also process which contains Date and currency fields, For this we have to specify the date format placeholders as below
Date currentDate = new Date();
Locale.setDefault(Locale.US);
System.out.println(MessageFormat.format("Current Date is {0,date yyyy-MM-dd}", date));

and output is Current Date is 2013-05-03

How to format text message contains number fields:-

Numbers in messages are formatted using {0,number,000.000} which display the numbers with 3 decimals

System.out.println(MessageFormat.format("Number is  {0,number,000.000}", 123456));

and output is Number is 123.456

How to Internationalization java applications

How do we Internationalization in java


Usually applications are developed in english, but when we want our applications to target users or customers of different countires, Sun provides Internationalization concept in java.
Internalization or I18n is set of java classes or interfaces provided by java to support the global application in java. It means java applications works with multiple languages and multiple countries.

What are items to do in Internationalization
1.DateFormat
2.Time Zone
3.NumberFormat
4. Message Format
6.CurrentSymbol

when we target above things, application is named as internationalized applications



In simple terms, we develop applications in one langugae and can change to different local or localize the applications by doing the above things.

In java, java.util.Locale class holds localized information

For example, have coded for creating button in swing

JButton buttonEx=new JButton("Upload");

here, button label message is hard coded, It is very difficult if we want to develop this application to target different regions.

To Localize the application, we have to separate the labels and place them in property or resource file.

label message in resource file for US country is

button.upload.label =Upload– label_en_US.property

In the same way we need to create one resource property file for each country or language. This can be added to application after application development is done.

button.upload.label = starten– app_de_DE.property

In java, Java.util.ResourceBundle is used to read resource bundles or property files.

In order to read resource files we can use java.util.ResourceBundle.

Creating resourceBundle

ResourceBundle rb = ResourceBundle.getBundle(basename, locale);

The above code will read the locale specific resource file , if it is not found, default locale resource is considered.

In Java applications, java.text.DateFormat,
java.text.NumberFormat, java.text.MessageFormat are used to achieve Internationalization



If you like this post, please share by clicking google +1 button