





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






StringBuffer: A handy class for dealing with strings whose contents can vary dynamically. Java’s Stack Data Structure: One of the simplest data structures in Java’s class library. Java’s Method Call Stack: How does Java keep track of local variables and parameter when methods are called?
StringBuffer( ) – Constructs an empty string buffer. append( … ) - Converts the argument into a string, and appends it to the end of the string buffer. (Possible arguments include boolean, char, double, float, int, long, Object, and char[ ].) charAt( int index ) - Returns the character at the specified index. length( ) – Returns the number of characters in the buffer. toString( ) - Returns a String representation of the buffer.
StringBuffer b = new StringBuffer( ); b.append( 99.5 ); b.append( '%' ); b.append( " pure" ); System.out.println( b );
String split: Splits a string about a given regular expression pattern. [abc] matches characters ‘a’, ‘b’, ‘c’. [abc]* matches 0 or more repetitions of these characters. [abc]+ matches 1 or more repetitions of these characters. To remove punctuation we split about 1 or more occurrences of space, comma, period, question mark: split( "[ ,.?]+" ) String toLowerCase: Returns an equivalent lower-case string. String valueOf: Produces a String representation of an object.
Local storage: This is where your local variables (including parameters) are stored. Heap: This is where all objects (created by new) are stored.
public class CallStack { public static int numberLowerCase( String theStr ) { int count = 0; for ( int i = 0; i < theStr.length( ); i++ ) if ( Character.isLowerCase( theStr.charAt( i ) ) ) count++; return count; } public static void stats( String str ) { int total = str.length( ); int lower = numberLowerCase( str ); System.out.println( "String: " + str ); System.out.println( "Total count: " + total ); System.out.println( "Lower case count: " + lower ); } public static void main( String[ ] args ) { CallStack.stats( "Que Bueno!" ); } }
public static int numberLowerCase( String theStr ) { int count = … int i = … } public static void stats( String str ) { int total = … int lower = numberLowerCase( str ); … } public static void main( String[ ] args ) { CallStack.stats( "Que Bueno!" ); } (Figure omitted)
Bitwise operators: Operate on values as binary numbers. Conditional operator: An “if-then-else” operator.
int x = 1037; // binary: …0010000001101 filled out to 32 bits char c = ‘y’; // binary: …0000001111001 filled out to 16 bits boolean b = true; // binary: 1
~a: complement of a a & b: and (1 if both a and b are 1) a | b: or (1 if either a and b are 1) a ^ b: exclusive or (1 if either a or b is 1, but not both)
int a = … // a = 1100101101 int b = 3; int c = a << b; // c = 0101101000 int d = a >> b; // d = 1111100101 int e = a >>> b; // e = 0001100101 int f = … // f = 0100101101 int g = f >> b; // g = 0000100101
boolean-condition? true-value : false-value
public static int absValue1( int x ) { if ( x < 0 ) return -x; else return x; }
public static int absValue2( int x ) { return ( x < 0? -x : x ); }
double max = (x > y)? x : y ;
String s = … double z = … double x = s.equals(“zero”)? 0.0 : 3*(z + 13.2);
Resizing: Arrays are not suitable for situations where the size of the array changes frequently. Appending to an Array: if we reach the maximum capacity of an array and we need to add an element, we have to create a new array, copy over elements, and add the desire element.