Home » Archive

Articles tagged with: javabout.com

Java Codes »

[28 Feb 2009 | No Comment | 127 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 | 108 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 | 100 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 | 121 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 | 157 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 | 64 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 | 57 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 | 54 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 | 387 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 »

[21 Feb 2009 | No Comment | 65 views]

This code example shows how to compare and sort objects by implementing the Comparable interface. The code consists of a class called Main and another class called Car.The Main class creates a few instances of the Car class (which is the one that implements the Comparable interface) and them compare them and also sort them.The Car class has three attributes, make, year and mileage. Only the mileage attribute is really of importance here since it’s the attribute used for comparing.When a class implements the Comparable interface it has to implement …