Home » Archive

Articles Archive for February 2009

Java Codes »

[28 Feb 2009 | No Comment | 42 views]

To get the range of a byte value, use the wrapper class Byte and get the value of its static variables MIN_VALUE and MAX_VALUE.The output of the code below is:-128127
/** * * @author javabout.com */public class Main {        /**     * Example method for getting the range of a byte     */    public void getByteRange() {                System.out.println(Byte.MIN_VALUE);        System.out.println(Byte.MAX_VALUE);    }        /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().getByteRange();    }}

Java Codes »

[28 Feb 2009 | No Comment | 52 views]

This little java code example shows the principle of the StringTokenizer class. If you want to devide a String that has substrings divided with a separator you create a new instance of the class and send the string as the first argument and the devider as the second argument.You loop through it by calling the hasMoreTokens() method which returns true until there are no more tokens left.In this example we just print out each token on a new line.The output of the example is:value1value2value3value4
import java.util.StringTokenizer;/** * * @author javabout.com */public class Main {        /**     * …

Java Codes »

[28 Feb 2009 | No Comment | 104 views]

Often you need to split a String up in elements, for example if you’re reading from a comma separated file.This example shows a method that does the job in two different ways. If your delimiter is not an advanced one it’s easy to make use of the split() method in the String class.It does take a regular expression as argument, but if you have as in this example a comma as delimiter, it’s no different from the argument type being a String.The split method returns an array of Strings with …

Java Codes »

[28 Feb 2009 | No Comment | 42 views]

The wrapper class for the datatype short has two static variables for getting the minimum and maximum values that a variable of type short can have, MIN_VALUE and MAX_VALUE.The code example below prints them out and below the code snippet you can see the min and max values.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the short datatype.     */        public void displayMinAndMaxValuesOfShort() {                System.out.println(Short.MIN_VALUE);        System.out.println(Short.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfShort();    }}

When executed, the output of the code is:-3276832767

Java Codes »

[28 Feb 2009 | No Comment | 39 views]

The class ‘Long’ is a wrapper class for the datatype long, and it has two static variables that contains the minimum and maximum values of the long datatype: MIN_VALUE and MAX_VALUE.To find out the min and max values of the long datatype, you can print out the values like below:
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype long.     */        public void displayMinAndMaxValuesOfLong() {                System.out.println(Long.MIN_VALUE);        System.out.println(Long.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfLong();    }}

The output from the executed code will be:-92233720368547758089223372036854775807

Java Codes »

[28 Feb 2009 | No Comment | 45 views]

This example shows how to validate if a String is a number.The first example checks if it’s and Integer by trying to parse the String to an int.The second example can be used if you’re not sure if the possible number value contained in the String will exceed the maximum value for an int.Here we instead use the Long class to parse the String to the datatype long.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Validates if input String is a number     */    public boolean checkIfNumber(String in) {                try {            Integer.parseInt(in);                } catch (NumberFormatException ex) {            return …

Java Codes »

[28 Feb 2009 | No Comment | 53 views]

To redirect standard error (or System.err) there’s a method in the System class called setErr() which takes a PrintStream instance as argument.If we for example want to redirect all messages on System.err to a file, we can create a FileOutputStream instance and send it to the constructor of the PrintStream.This Java code example creates the FileOutputStream with the filename “standard_err.txt” and pass it to the PrintStream, which in turn is passed to the setErr() method.A NullPointerException is forced to test that the stacktrace is printed to the file.
import java.io.FileNotFoundException;import java.io.FileOutputStream;import …

Java Codes »

[28 Feb 2009 | No Comment | 45 views]

This example shows how to clone objects by implementing the Cloneable interface. Some classes implements the Cloneable interface by default but when creating custom classes the interface needs to be implemented explicitly.The class that implements the Cloneable interface (the Person class in the example) needs to have a method named clone() that returns an object.In the example we have chosen to create a new instance of Person and populate its properties with the same values as the object that the clone() methodis called on.
/** * Main.java * * @author www.javabout.com */public class Main …

Java Codes »

[28 Feb 2009 | No Comment | 26 views]

To find the superclass of an object we first need to call the getClass() method of the object to return the Class-object.The Class object then has a method called getSuperClass() which returns another Class object on which the method getName() can be called.In the example below we call the getSuperClass() method for four different object types: Vector, ArrayList, String and Integer.
import java.util.ArrayList;import java.util.Vector;/** * Main.java * * *#64;a*#117;t*#104;*#111;*#114; www*#46;*#106;a*#118;*#97;d*#98;.c*#111;m */public class Main {        /**     * Constructor     */    public Main() {                checkObjectSuperClass(new Vector());        checkObjectSuperClass(new ArrayList());                checkObjectSuperClass(“Test String”);        checkObjectSuperClass(new Integer(1));            }        /**     * Checks which superclass the object has.     *     * @param testObject The object     */    public void checkObjectSuperClass(Object testObject) {                System.out.println(“Object …

Java Codes »

[28 Feb 2009 | No Comment | 60 views]

To check if a string contains another string the method indexOf() is used. It returns the index in the haystack string where the needle string begins.If the needle string is not present in the haystack string, the value -1 is returned.In the code example below we check two needle strings against a haystack string, one exists and one does not.
/** * Main.java * * *#64;a*#117;t*#104;*#111;r *#119;ww.*#106;a*#118;*#97;*#100;*#98;*#46;*#99;o*#109; */public class Main {        /**     * Checks if string contains another string.     */    public void checkStringContains() {                String haystack = “Programming in Java”;        String needle1 = “Java”;        String needle2 = “Pascal”;                int index1 = haystack.indexOf(needle1);        int index2 = …