1 / 9

Standard Classes

Standard Classes. Classes we will discuss. Object String Wrapper Classes Double Integer Math. Object. Superclass to all other classes Methods toString () equals()

garret
Download Presentation

Standard Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Standard Classes

  2. Classes we will discuss • Object • String • Wrapper Classes • Double • Integer • Math

  3. 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

  4. 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”;

  5. 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()

  6. 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)

  7. 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

  8. 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)

  9. 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!!!!

More Related