



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!




3.1 – Garbage Collection
3.2 – String Methods
3.2 – StringMutation.java //******************************************************************** // StringMutation.java Java Foundations // // Demonstrates the use of the String class and its methods. //******************************************************************** public class StringMutation { //----------------------------------------------------------------- // Prints a string and various mutations of it. //----------------------------------------------------------------- public static void main (String[] args) { String phrase = "Change is inevitable, except from vending machines."; String mutation1, mutation2, mutation3, mutation4; mutation1 = phrase.concat (".. "); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30); System.out.println ("Mutation #4: " + mutation4); System.out.println ("Mutated length: " + mutation4.length()); } } 8 3.3 – Class Libraries
3.5 – Very useful: The Math Class
n absolute value n square root n exponentiation n trigonometric functions
discriminant = Math.pow(b, 2) - (4 * a * c); root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a); root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a); 14 3.7 – Enumerated Types
Season time;
time = Season.fall;
3.7 – Ordinal Values and Names
3.7 – IceCream.java //******************************************************************** // IceCream.java Java Foundations // // Demonstrates the use of enumerated types. //******************************************************************** public class IceCream { enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough} //----------------------------------------------------------------- // Creates and uses variables of the Flavor type. //----------------------------------------------------------------- public static void main (String[] args) { Flavor cone1, cone2; cone1 = Flavor.rockyRoad; System.out.println ("cone1 value: " + cone1); System.out.println ("cone1 ordinal: " + cone1.ordinal()); System.out.println ("cone1 name: " + cone1.name()); for (Flavor cone: Flavor.values()) { System.out.println ("cone value: " + cone); } }
3.8 – Wrapper Classes
Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void 18 3.8 – Wrapper Classes
Integer age = new Integer(40);
For example, some objects serve as containers of other objects
num = Integer.parseInt(str);
3.8 – Autoboxing and Unboxing