Overview: 2.10.0

  • Learned to use objects that have been written already
    • Objects are variables of a class type
  • Learned to use constructors and methods with and without parameters
  • Learned to use built-in classes like String, Integer, Double and Math

Concept Summary: 2.10.1

  • class
    • Defines a new data type
    • A formal implementation of attributes and behaviors for objects of that class
  • object
    • A specific instance of a class
    • Declared as variables of a class type
  • constructor
    • Code used to create a new object and initialize attributes
  • new
    • keyword used to create an object and call one of the class’ constructors
  • instance variables
    • Attributes for an object
  • methods
    • Functions for objects
  • dot operator
    • Used to access an object’s methods
  • parameters
    • values passed to an object’s methods
    • this is the name for the formal declaration
  • arguments
    • The values passed as parameters when a method is actually called
  • return values
    • Values returned by a method after being called
  • Immutable
    • Not able to be changed
    • Strings are immutable
      • All methods which modify a string return a new string
        • they do not modify the existing string itself
  • wrapper classes
    • Classes which create objects from primitive types

Keyword Summary: 2.10.2

  • new
    • used to create a new object
  • null
    • used to indicate that an object reference doesn’t refer to anything

String methods and constructors

  • String(String str)
    • Constructs a new String object that represents the same sequence of characters as str.
  • int length()
    • returns the number of characters in a String object.
  • String substring(int from, int to)
    • returns the substring beginning at index from and ending at index (to -1)
    • The single element substring at position index can be created by calling substring(index, index + 1).
  • String substring(int from)
    • returns substring(from, length()).
  • int indexOf(String str)
    • returns the index of the first occurrence of str
      • returns -1 if not found.
  • boolean equals(String other)
    • returns true if this (the calling object) is equal to other
      • returns false otherwise.
  • int compareTo(String other)
    • returns a value < 0 if this is less than other
      • returns zero if this is equal to other
      • returns a value > 0 if this is greater than other.

Integer methods and constructors

  • Integer(value)
    • Constructs a new Integer object that represents the specified int value.
  • Integer.MIN_VALUE
    • The minimum value represented by an int or Integer.
  • Integer.MAX_VALUE
    • The maximum value represented by an int or Integer.
  • int intValue()
    • Returns the value of this Integer as an int.

java.lang.Math methods

  • int abs(int)
    • Returns the absolute value of an int value (which means no negatives).
  • double abs(double)
    • Returns the absolute value of a double value.
  • double pow(double, double)
    • Returns the value of the first parameter raised to the power of the second parameter.
  • double sqrt(double)
    • Returns the positive square root of a double value.
  • double random()
    • Returns a double value greater than or equal to 0.0 and less than 1.0 (not including 1.0!)

Common Mistakes: 2.10.4

  • Forgetting to declare an object to call a method from outside of the class
  • Forgetting parenthesis after method names when calling methods
  • Forgetting to give the correct parameters in the right order
  • Forgetting to use the value returned from a method
  • Using == to test the equality of strings
    • Instead you must use String.equals or String.compareTo
  • Treating upper and lower case letters as the same
  • Thinking substrings include the character at the end index
  • thinking strings can change
    • they are immutable
  • Trying to call a method on a reference variable which hasn’t been initialized yet
    • This will cause a null pointer exception