// AppendixADeclarations.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-07-14 File created. // 2005-07-14 Last modification. // Testing some of the declarations shown in Appendix A of the book. // The literals of Appendix A are tested in LiteralTests.java import java.util.* ; class AppendixADeclarations { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; char character_from_keyboard ; short given_small_integer ; int integer_from_keyboard ; long multiplication_result ; char user_selection = '?' ; byte mask_for_most_significant_bit = (byte) 0x80 ; int character_index = 0 ; int bit_mask = 0x80000000 ; long speed_of_light = 299793000L ; float kilometers_to_miles = 1.6093F ; double value_of_pi = 3.14159 ; boolean text_has_been_modified = false ; System.out.printf( "\n %X", mask_for_most_significant_bit ) ; final int LENGTH_OF_NORMAL_YEAR = 365 ; final int LENGTH_OF_LEAP_YEAR = 366 ; final double LENGTH_OF_YEAR_IN_SECONDS = 31558149.5 ; final float EXACT_LENGTH_OF_YEAR_IN_DAYS = 365.256F ; char[] array_of_characters ; array_of_characters = new char[ 50 ] ; int[] array_of_integers = new int[ 60 ] ; int[] integers_to_power_of_two = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121 } ; int[] two_to_power_of_integer = { 1, 2, 4, 8, 16, 0x20, 0x40, 0x80, 0x100 } ; char[] hexadecimal_digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' } ; double[] pi_times_integer = { 0, 3.1416, 6.2832, 9.4248, 12.5664 } ; float[] centimeters_to_inches = { 0, 0.3937F, 0.7874F, 1.1811F, 1.5748F, 1.9685F}; int[][] some_two_dimensional_array = new int[ 5 ][ 9 ] ; System.out.print( "\n " + some_two_dimensional_array[ 4 ][ 7 ] ) ; String some_string ; // declares a string reference String another_string = "" ; // an empty string String third_string = "text inside string object" ; char[] some_letters = { 'K', 'a', 'r', 'i' } ; String some_name = new String( some_letters ) ; String some_copied_string = new String( some_name ) ; System.out.print( "\n " + some_copied_string ) ; Date first_day_of_this_millennium = new Date( 1, 1, 2000 ) ; Date last_day_of_this_millennium = new Date("12/31/2999") ; Object anything ; // This can reference any object String[] any_array_of_strings ; String[] array_of_strings = new String[ 9 ] ; array_of_strings[ 0 ] = "some text line" ; array_of_strings[ 1 ] = "another text line" ; System.out.print( "\n array_of_strings[ 3 ] is " + array_of_strings[ 3 ] ) ; // The following is an initialized array of strings String[] largest_moons_of_jupiter = { "Io", "Ganymede", "Europa", "Callisto" } ; Date[] days_of_this_millennium = new Date[ 365243 ] ; days_of_this_millennium[ 0 ] = new Date( 1, 1, 2000 ) ; days_of_this_millennium[ 1 ] = new Date( 2, 1, 2000 ) ; days_of_this_millennium[ 2 ] = new Date( 3, 1, 2000 ) ; // I test here the working of the instanceof operator if ( some_name instanceof String ) { System.out.print( "\n true" ) ; // the object is a String object } if ( some_name instanceof Object ) { System.out.print( " true" ) ; // String is a subclass of Object } if ( some_name instanceof Comparable ) { System.out.print( " true" ) ; // String implements Comparable } } }