Home » Archive

Articles Archive for February 2009

Java Codes »

[28 Feb 2009 | No Comment | 52 views]

This code example shows how to determine the Class of an Object instance.There is a method called checkObjectClass which takes an Object as input parameter and first uses instanceof to check whether object is of type Vector or ArrayList.If none of those classes match, the name of the class is determined by calling the getClass() method which returns the name of the class as a String.The checkObjectClass() method is called four times with different arguments from the constructor, with an instance of Vector, ArrayList, String and Integer.
import java.util.ArrayList;import java.util.Vector;/** * Main.java * * …

Java Codes »

[28 Feb 2009 | No Comment | 51 views]

This example shows how to determine the package of an object. To get the package name we need to call the getClass() method of the object to get a reference to the object class.Then we can call the getPackage() method that will return an object of type Package. Finally we call the getName() method on the Package object which returns a String containing the package name.
import java.util.ArrayList;import java.util.Vector;/** * Main.java * * @a*#117;*#116;h*#111;r w*#119;*#119;*#46;ja*#118;*#97;*#100;*#98;*#46;c*#111;m */public class Main {        /**     * Constructor     */    public Main() {                findPackage(new Vector());        findPackage(new ArrayList());                findPackage(“Test String”);        findPackage(new Integer(1));            }        /**     * Checks which package the object has.     *     * @param testObject The …

Java Codes »

[28 Feb 2009 | No Comment | 48 views]

This code example shows how to get a substring from a string. To do that you need to know atleast the starting index of the substring and a good way to find that out is the indexOf() method of the String class.The substring() method takes either start index (inclusive) and end index as parameters, or just the start index which will return the remainder of the string.The example below shows both ways:
/** * Main.java * * @*#97;*#117;th*#111;*#114; *#119;*#119;w*#46;*#106;*#97;*#118;*#97;*#100;b*#46;c*#111;m */public class Main {        /**     * Gets a substring from a string.     */    public void getSubstring() {                String str = “Getting …

Java Codes »

[28 Feb 2009 | No Comment | 366 views]

To convert all upper case characters in a string to lower case, use the method toLowerCase() of the String class.The example below shows how to use it. Note that the method returns a String so you’ll need to assign the return value of the method to a variable (in this case the same variable).
/** * Main.java * * *#64;au*#116;h*#111;r *#119;*#119;*#119;.*#106;*#97;*#118;*#97;*#100;*#98;.*#99;om */public class Main {        /**     * Converts all upper case characters to lower case     */    public void stringToLowerCase() {                String str = “All Upper Case Characters In This String Will Be Converted To Lower Case”;                str = str.toLowerCase();                System.out.println(str);    }        /**     * Starts the …

Java Codes »

[28 Feb 2009 | No Comment | 172 views]

Since Java 6 there is a new way to check if a String is empty. The most common way to determine if a String is empty is to call the length() method of the String class and check if it returns 0. There is now a method added to the class called isEmpty() which returns a boolean value.The output from the code below is:truetruefalsefalse
/** * * @author javabout.com */public class Main {        /**     * Example method for checking if a String is empty     */    public void checkEmptyString() {                String a1 = “”;        String b1 = “not an empty string”;                System.out.println(“Checking …

Java Codes »

[28 Feb 2009 | No Comment | 179 views]

When a Java program has gone through all code it exits automatically by returning zero if all has gone well.In some situations you might want to exit the program with a line of code, and this is done by calling the static exit method of the System class.The exit method takes an int as argument so if you want to exit the program normally you write:
System.exit(0);

Should you want to exit because of for example some error has occured you normally specify a negative number as the argument.You could of course …

Java Codes »

[28 Feb 2009 | No Comment | 160 views]

By listing the properties of the system you run the program on you can find out a lot about it. Your program maybe should act in a certain way depending on what operating system it’s currently on.This code example shows how to retrieve the properties and how to enumerate through and print them out to the console.
import java.util.Enumeration;import java.util.Properties;/** * * @author javabout.com */public class Main {        /**     * Lists the system properties     */    public void listSystemProperties() {                Properties prop = System.getProperties();                Enumeration enumeration = prop.keys();                while (enumeration.hasMoreElements()) {            String key = (String) enumeration.nextElement();            System.out.println(key ” = “ …

Java Codes »

[28 Feb 2009 | No Comment | 161 views]

There are severals ways to accomplish this. The example below shows two of them.The second example is only possible if you run Java version 1.5 or later.
import java.text.DecimalFormat;public class NumberUtil {  public void roundNumber(double numberToRound) {    DecimalFormat df = new DecimalFormat(“0.000″) ;    System.out.println( “Rounded number = “ df.format( numberToRound ) ) ;        //This only works with Java runtime 1.5 or later    System.out.println(String.format(“Rounded number = %.3f”, numberToRound));  }  public static void main(String[] args) {    NumberUtil util = new NumberUtil();    util.roundNumber(1.23856);  }}

The output of either one of the above rounding methods is:“Rounded number = 1,239″

Java Codes »

[28 Feb 2009 | No Comment | 154 views]

Since strings are immutable, you may want to use the StringBuilder class if you’re going to alter the String in the code. The StringBuilder class can be seen as a mutable String object which allocates more memory (or deallocates if told so by using the trimToSize() method) when it’s content is altered.It should be said early that the StringBuilder class is not thread-safe which means that it doesn’t automatically handle synchronization of access to the object by different threads as opposed to the StringBuffer class.If this object needs to be …

Java Codes »

[28 Feb 2009 | No Comment | 152 views]

This code snippet shows how to find out the minimum and maximum values of the byte datatype.To get the minimum value you simply call the static variable MIN_VALUE of the Byte class, and MAX_VALUE for the maximum value.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the byte datatype.     */        public void displayMinAndMaxValuesOfByte() {                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().displayMinAndMaxValuesOfByte();    }}

The min and max values of a byte are:-128127