90 likes | 220 Views
Dive into the foundational concepts of Java programming, focusing on objects, strings, and wrapper classes. Learn about object methods like `toString()` and `equals()`, and the significance of overriding these methods to define equality based on content rather than memory location. Explore immutable string objects, string manipulation methods, and the concept of autoboxing with wrapper classes like `Integer` and `Double`. This guide will also highlight essential Java API methods for enhanced functionality. Elevate your Java skills today!
E N D
Classes we will discuss • Object • String • Wrapper Classes • Double • Integer • Math
Object • Superclass to all other classes • Methods • toString() • equals() • Unless overridden, returns true if this object and other object are same object, false otherwise. By “same” here, we mean same memory location • What if we want two objects to be considered equal if their contents are the same? Let’s override! POLYMORPHISM – WOOT WOOT • Note: • default .equals() is same as == • <, >, etc are not overridden in java. Must use .equals() and/or .compareTo() (Comparable interface, we’ll discuss more soon) • BankAccount.java • OrderedPair.java • Classes to write: BankAccount.java, OrderedPair.java, Client.java
String • A sequence of characters • Immutable objects – there are no methods to change them after they’ve been constructed • you can however create a new String which is a mutated version of an existing String • You can also reassign a String variable • String word = “abracadabra”; word = “supercalifragilisticexpealidocious”;
String methods and operators • Constructors • String name = “Mike”; • String name = new String(“Mike”); • Concatenation • String + String • String + primitive type • compareTo() • Compared lexicographically (think dictionary, or how files are sorted alphabetically) • Return value • int length() • String substring(intstartIndex) • String substring(intstartIndex, intendIndex) • intindexOf(String str) • intindexOf(char ch) • char charAt(int index) • boolean equals(Object o) • Look at == • booleanequalsIgnoreCase(String s) • String[] split(String delim) • String toUpperCase() • String toLowerCase()
Wrapper Classes • Takes existing object or primitive type and “wraps” or “boxes” it in an object • Provides new set of methods • Autoboxing – automatic conversion between primitive and corresponding reference type • Allows: • Construction of an object from a single value (wrapping/boxing) • Retrieval of primitive value (unwrapping/unboxing)
Wrapper Classes Integer Double Double(double value) double doubleValue() intcompareTo(Object o) boolean equals(Object o) String toString() • Integer(int value) – boxing • intintValue() – unboxing • intcompareTo(Object o) • boolean equals(Object o) • Overrides Object’s equals method • Throws ClassCastException if o is not an Integer • String toString() • Returns String representation of the Integer
Math • static int abs(int x) • static double abs(double x) • static double pow(double base, double exp) • static double sqrt(double x) • static double random() • static double floor(double x) • static double ceil(double x) • static long round(double x)
Note • There are MANY other methods in the classes we discuss that may be of use to you. Feel free to explore these in the Java API!!!!