Home » Archive

Articles tagged with: java language

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

Java Codes »

[28 Feb 2009 | No Comment | 322 views]

The class ‘Float’ is a wrapper class for the datatype float, and it has two static variables that contains the minimum and maximum values of the float datatype: MIN_VALUE and MAX_VALUE.You can print out the min and max values of the float datatype by specifying these variables as arguments to System.out.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype float.     */        public void displayMinAndMaxValuesOfFloat() {                System.out.println(Float.MIN_VALUE);        System.out.println(Float.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfFloat();    }}

The output from the executed code will …

Java Codes »

[28 Feb 2009 | No Comment | 158 views]

Often it is necessary to validate input data. One such validation could be to check if a user has entered a sequence of numbers.This example shows how you can check a String to see if it only contains integers. This validation could be done with less code if we were using regular expressions, but this example aims to keep it simple and more understandable so we use the Character wrapper class instead.The Character class has a static method called isDigit() which takes a char value as argument and returns true …

Java Codes »

[28 Feb 2009 | No Comment | 154 views]

If you want to change the location to where the standard output messages go there’s a method in the System class called setOut() which takes a PrintStream instance as argument.In this code example we show how to redirect all standard output messages to a file by creating a FileOutputStream instance and pass it to the PrintStream instance that is sent to the setOut method, like so:
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Redirects System.out and sends all data to a file.     *     */    public void redirectSystemOut() {                try {                        System.setOut(new PrintStream(new FileOutputStream(“system_out.txt”)));                    } …

Java Codes »

[28 Feb 2009 | No Comment | 162 views]

To display the number of processors available to a certain Java Virtual Machine there is a method of the Runtime class called availableProcessors().To get an instance of the Runtime class we have to call the static method getRuntime(). The example below illustrates how to go about displaying the number of processors.
/** * Main.java * * @author www.javabout.com */public class Main {        /**     * Displays the number of processors available in the Java Virtual Machine     */    public void displayAvailableProcessors() {                Runtime runtime = Runtime.getRuntime();                int nrOfProcessors = runtime.availableProcessors();                System.out.println(“Number of processors available to the Java Virtual Machine: “ nrOfProcessors);            }        /**     * Starts …