This post shows you how to convert String to Short in Java. Let's write a generic code in this post.
Create Generic Converter interface
/**
* Converts objects of S type to T type.
*/
public interface Converter<S, T> {
/**
* Converts the source object from S type to T type.
*
* @param source the object to convert
*
* @return the converted object
*/
T convert(S source);
}
Converts String to Integer - StringToShortConverter.java
Let's implement the above Converter interface to convert String to Short in Java
/**
* Converts String to Short.
*/
public class StringToShortConverter implements Converter<String, Short> {
@Override
public Short convert(String source) {
return Short.valueOf(source);
}
}
Comments
Post a Comment