| | | java.lang.Boolean | | | java.lang.Character | | java.lang.Double |__________| java.lang.Math |_________| java.lang.Float | java.lang.Number | | java.lang.Integer | java.lang.String | | java.lang.Long | java.lang.StringBuffer |
Java handles primitive types by value and objects by reference.
Java provides object wrappers for primitive data types.
All numeric classes extend from the abstract Number class which
provides conversion methods.
java.lang.Number
Primitive data type wrappers
Object | Primitive |
Boolean | boolean |
Character | char |
Double | double |
Float | float |
Integer | int |
Long | long |
double x = 5.4;
Double X = new Double(x);
int y = X.intValue();
Integer Y = new Integer(X.intValue());
public class MyChar { public static void main(String args[]){ char c1 = 'A'; char c2 = 'z'; char c3 = '5'; Character C1 = new Character('c'); System.out.println("Is character "+ c1 + "uppercase? " + Character.isUppercase(c1)); System.out.println("Is character "+ c2 + "uppercase? " + Character.isUppercase(c2)); System.out.println("Is character "+ c3 + "a digit? " + Character.isDigit(c3)); System.out.println("Convert "+ c1 + "to lowercase: " + Character.toLowerCase(c1)); System.out.println("Character object C1 is: " + C1.charValue()); } }
The java.lang.Math class is a final class. Math defines constants for e and . There are also static methods for floating point trig and exponentiation. There are several other useful mathematics functions.
public void mathTest() { int x = 5; int y = 6; System.out.println("mathTest: x = " + x + " y = " + y + " Max = " + Math.max(x,y)); int radius = 10; System.out.println("mathTest: circle area = " + radius * radius * Math.PI); }
Whenever the java compiler encounters a set of double quotes, it creates a String object. The contents of a String object can not be changed. If you need to manipulate the contents of a String, create a StringBuffer instance with your String.
There are several class and instance methods in the String class. The StringBuffer class has several instance methods that can manipulate the object.
public void stringTest() { String string1 = new String("Fred was here"); System.out.println(string1.startsWith("Barney") ); // false System.out.println(string1.substring(0,3)); // Fred System.out.println(String.valueOf(56.1)); // "56.1" StringBuffer stringBuffer = new StringBuffer(string1); stringBuffer.insert("Barney and "); System.out.println(stringBuffer); // Barney and Fred was here }
System is a generic interface to system functions. Some System functions do not work for applets because of security constraints. All methods and variables are static. Since System is a final class, it can not be extended.
System.out.println("Use arrow keys to navigate..."); // Help message out
System.err.println("Failure to process ..."); // Error Warning to "err" device
String name = System.in.read() ; // reading string from the keyboard
System.getenv("USER"); // environment reguest (valid for application only)
Arrays are manipulated by reference. It can be used to store primitive type data or objects. All objects or primitive data MUST be the same type. We can create an array using new and sets a size for the array to be. This type of creation calls no constructors so there is no argument list needed. Elements of an array created this way are initialized to the default value. We can also create an array with a static initializer. This method creates the array and initializes the elements to the specified values.
Arrays are automatically garbage collected.
The length of an array can be accessed at run time like a public instance variable.
int myArray[] = new int[50];
TextField myFields[] = new TextField[50];
int _numbers[] = new int { 5, 8, 10, 12, 99 };
int x = myFields.length;
Multidimensional arrays are implemented as arrays-of-arrays.
When creating a multidimensional array, it is not neccessary to specify the number of elements that are contained in each dimension. When using multidimensional arrays, we have to allocate the size of, at least, the first dimension.
Initializing Multidimensional Arrays
// Legal
String _bigSet[][][][] = new String[2][4][][];
// Not Legal
String _bigSet[][][][] = new String[2][][4][];
// Static initializer
String _classes[][] = { { "Advanced Java", "Java Intermediate", "Java for beginners" }, { "Oracle Web Server", "JDBC and Oracle", "Oracle and SQL"} };
/** * The Menu class uses Hashtables to represent (store/retrieve/display) * 2-level menues * Example of usage: * Menu menu = new Menu(); * Hashtable table = menu.packTable(Menu.menues); // pack arrays of menue items into a table * String[][] menuItems = menu.tableToString(table); * menu.printTable(menuItems); */ public class Menu { public static String[] menuTypes = { "Sports", "Events", "Movies" }; public static String menues[][] = { { "10K run", "Basketball"}, { "French Revolution", "Civil War", "Superball"}, { "Die Hard", "Matrix"} }; /** * The packTable() method packs the string nested structrure into Hashtable * @param menuItemNames * @return table */ public Hashtable packTable(String[][] menuItemNames) { if(menuItemNames == null || menuItemNames[0] == null || menuItemNames[0].length == 0) { return null; } Hashtable table = new Hashtable(); for(int i=0; i < menuTypes.length; i++) { Hashtable menuTable = new Hashtable(); // menuItemNames[0] includes types of menuItemNames for(int j=0; j < menuItemNames[i].length; j++) { menuTable.put("" + j, menuItemNames[i][j]); } table.put(menuTypes[i], menuTable); } return table; } /** * The tableToString() method unpacks the Hashtable into 2-level menuItemNames * @param table * @return menuItemNames 2-level (dim) array of menuItemNames */ public String[][] tableToString(Hashtable table) { if(table == null) { return null; } String[][] menuItemNames= new String[table.size()][]; int i = 0; // menu types counter for(Enumeration e = table.keys(); e.hasMoreElements(); i++) { String key = (String) e.nextElement(); Hashtable menuTable = (Hashtable) table.get(key); int j = 0; // menu item counter menuItemNames[i] = new String[menuTable.size()]; for(Enumeration menuItemEnumeration = menuTable .keys(); menuItemEnumeration.hasMoreElements(); j++) { String menuItemKey = (String) menuItemEnumeration.nextElement(); String menuItem = (String) menuTable.get(menuItemKey); menuItemNames[i][j] = menuItem; } } return menuItemNames; } /** * The printMenues() method displays 2-level menuItemNames * @param menues */ public void printMenues(String[][] menues) { if(menues == null) { return; } for(int i=0; i < menuItemNames.length; i++) { // menuItemNames System.out.println("\n\n" + menuTypes[i]); for(int j=0; j < menuItemNames[i].length; j++) { // menu items System.out.println(" " + menuItemNames[i][j]); } } } /** * The main{} method is to test the class */ public static void main(String[] args) { Menu menu = new Menu(); Hashtable table = menu.packTable(menues); // pack arrays of menue items into a table String[][] menuItems = menu.tableToString(table); menu.printTable(menuItems); } }