Professional Documents
Culture Documents
String Class
String jobtitle = new String(developer); Instantiates a String object having its own space in the heap memory Which will have the same string value as the String object in the literal pool. Any references involving jobTitle will access the object on the heap. String literals are implemented as String objects String jobTitle = "clerk";
Because the string literals are identical, the compiler creates one String object for "abc" in the literal pool. It is then assigned to both x and y.
The String class doesn't give any methods to change a character in an existing String
Method
Usage
charAt()
Returns the character at the specified index within the string where the first character has an index of 0 Performs a case-sensitive comparison of the value of this string to the value of another String object to determine which comes first alphabetically
Concatenates the specified string to the end of this string to create another String object Performs a case-sensitive comparison of the value of this string to the value of another String object to determine if they are equal
compareTo()
concat()
equals()
equalsIgnoreCase()
Compares the value of this string to the value of another String object with both strings viewed as having the same case
Returns the index of the first occurrence of a specified search character or substring within this string (overloaded)
indexOf()
lastIndexOf()
Returns the index of the last occurrence of a specified search character or substring within this string (overloaded)
Returns the character length of this string Replaces all occurrences of a specified character in this string with another character to create another String object
length()
replace()
StringBuffer Class
Strings are Immutable StringBuffer class can be thought of as a String that can be modified: StringBuffer objects are modifiable Strings. StringBuffer objects are generally slower. StringBuffer allows for easy appending, insertion and removal. Here are two constructors for the StringBuffer class StringBuffer()
The first creates a StringBuffer with no characters to begin with and
StringBuffer(String aString)
creates one with the characters equal to the ones in the given string
void setLength(int length) Decrease or increase the length of the string to the given length. If the length is less than the current length, the characters are truncated. If the size is greater, null characters are used to fill in the extra places. char charAt(int index) Returns the character in the string at the given index. The first character has index 0. void setCharAt(int index, char c) Sets the character at the given index of the string to c.
StringTokenizer class
Allows to break a string into individual substrings based on some separation criteria. Extract the words from a sentence one at a time separated by commas or other chars Implements the Enumeration Interface A tokenizer is created for a particular String which is specified in the constructor: new StringTokenizer(String aString); Tokens are separated by characters which are called delimiters. The default delimiter is the space character. new StringTokenizer(String aString, String delimiters); (e.g., delimiters may be ",:;|_()" etc...)
StringTokenizer Methods
The countTokens() returns the number of tokens in the String. nextToken() which returns a String representing the token: hasMoreTokens() returns whether or not there are any more tokens remaining: Commas or spaces or any combinations of consecutive commas and spaces count as a single token separator (or delimeter).
Lang Package
The Java Reflection API
What is Reflection?
Reflection is a mechanism for manipulating classes, interfaces, and objects at runtime
useful for developing tools such as debuggers, class browsers, dynamic analysis tools for Java programs, etc. Also useful for developing application architectures custom code can be encapsulated within concrete implementations of abstract base classes.
Examining Classes
Java Runtime Environment (JRE) maintains an object of the class java.lang.Class that contains information about a class
fields, methods, constructors and interfaces
If the name of the class is known at compile time, retrieve its Class object by appending .class to its name:
Class c = myPackage.Helloworld.class;
If the class name is unknown at compile time, but available at runtime, the forName method can be used:
Class c = Class.forName(myPackage.HelloWorld);
Class Modifiers
A class declaration includes the following modifiers: public, abstract, or final. The method getModifiers of the class Class returns a flag (as an integer) that corresponds to the class modifier To interpret these flags, we need to use the static methods of the Modifier class The following example displays the modifier of the class
Finding Superclasses
The method getSuperclass() returns an instance of Class that represents the superclass of the Class object that uses it To identify all ancestors of a class, we need to call getSuperclass() iteratively until it returns null. The program that follows finds the names of the Button class's hierarchy of superclasses
Class[] theInterfaces = c.getInterfaces(); for (int i = 0; i < theInterfaces.length; i++) { String interfaceName = theInterfaces[i].getName(); System.out.println(interfaceName); } } }
Class Constructors
The method getConstructors() returns an array of Constructor objects that represents all public constructors of the Class object that uses it The method getDeclaredConstructors() can be used to retrieve all other constructors of the class The methods of the Constructor class can be used to determine the constructor's name, set of modifiers, parameter types The class Constructor can also be used to create a new instance dynamically using the static method: Constructor.newInstance The program that follows displays the parameter types for each constructor in the Rectangle class
Class Methods
The method getMethods() returns an array that contains objects of the Method class that represent the accessible public methods of the Class object that uses it this includes the inherited methods To retrieve private, protected and package-scope methods getDeclaredMethods() can be used. This method does not include the inherited methods The methods of the Method class can be used to return a method's name, return type, parameter types, set of modifiers, etc The static method Method.invoke can be used to call the method itself The following program displays the name, return type, and parameter types of every public method in the Polygon class
More examples
The following examples will show you how to use the reflection model to: Create objects Set/get field values Invoke methods using Method.invoke
heightField = c.getField("height"); //get the field object heightValue = (Integer)heightField.get(r); // get value System.out.println("Height: " + heightValue.toString()); } catch (. . .) { . . . } } }
static void modifyWidth(Rectangle r, Integer widthParam ) { Field widthField; //declare a field Integer widthValue; Class c = r.getClass(); //get the Class object try { widthField = c.getField("width"); //get the field object widthField.set(r, widthParam); //set the field to widthParam =300 } catch (. . . ) { . . . } } }