Home » Archive

Articles tagged with: bean

Java Codes »

[28 Feb 2009 | No Comment | 163 views]

Getting data from the system clipboard is implemented nicely in Java through the Toolkit class which has a getSystemClipboard method.Since there isn’t necessarily text contents in the clipboard the getSystemClipboard returns an instance of the class Transferable. With that instance we have to test if the content is text before we do any processing with it.To test the code, just copy some text from the browser or some other document and have the java program print it out.
package com.javadb.example;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.io.IOException;/** * * @author javabout.com */public class Main {    /**     * …

Java Codes »

[28 Feb 2009 | No Comment | 130 views]

It is very easy to place text on the clipboard with Java.We just get an instance of the default toolkit and from that we get hold of a reference to the clipboard object.To place some text on the clipboard we need to pass an object of type Transferable and another object which is the clipboard owner to the method setContents.To make it easy we may specify null as the owner, and as the first argument we pass an instance of the class StringSelection which implements the Transferable interface.To test the …

Java Codes »

[28 Feb 2009 | No Comment | 159 views]

Since strings are immutable, you may want to use the StringBuffer class if you’re going to alter the String in the code. The StringBuffer class can be seen as a mutable String object which allocates more memory (or deallocates if told so) when it’s content is altered.It should be said early that the StringBuffer class i thread-safe which means that it automatically handles synchronization of access to the object by different threads.If you’re using only one thread in your program you should use the StringBuilder class instead since it’s pretty …

Java Codes »

[28 Feb 2009 | No Comment | 110 views]

To find out the minimum and maximum values of a char we can use the static variables MIN_VALUE and MAX_VALUE of the Character class.If we would output the min and max characters themselves it wouldn’t be very readable. Therefore we cast the value to an int to display the decimal values instead.
/* * Main.java * * @author www.javabout.com */public class Main {        /*     * This method displays the minimum and maximum values of the datatype char.     */        public void displayMinAndMaxValuesOfChar() {                System.out.println((int)Character.MIN_VALUE);        System.out.println((int)Character.MAX_VALUE);    }            /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().displayMinAndMaxValuesOfChar();    }}

Java Codes »

[28 Feb 2009 | No Comment | 95 views]

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

The output from the executed code will be:-21474836482147483647

Java Codes »

[28 Feb 2009 | No Comment | 89 views]

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

The output from the executed code will be:4.9E-3241.7976931348623157E308

Java Codes »

[28 Feb 2009 | No Comment | 104 views]

Sometimes you want to compute the time a certain operation takes, which can be done by using the currentTimeMillis() method of the System class.The value returned is the current time in milliseconds and by calling the method before and after the operation you can compute the difference in time,which is the time of the operation.The example below performs and operation by looping from 0 to 9 and in each loop makes the thread sleep for 60 milliseconds and then displays the time elapsedbetween the start and the end of this …

Java Codes »

[28 Feb 2009 | No Comment | 141 views]

This code example shows how to display the total amount of memory the Java Virtual Machine uses, the maximum amount of memory the JVM will attempt to use andalso the total amount of free memory in the JVM.It is done through calling the methods totalMemory(), maxMemory() and freeMemory() of the Runtime class.To obtain an instance of the Runtime class we call the static method getRuntime().The amount of memory from the different methods are returned in bytes, so we round the numbers by creating an instance of the DecimalFormat class and …

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 …