Tags & Description
int length() method
returns the number of characters in the string, including spaces and special characters like punctuation.
String substring(int from, int to) method
returns a new string with the characters in the current string starting with the character at the from index and ending at the character before the to index (if the to index is specified, and if not specified it will contain the rest of the string).
int indexOf(String str) method
searches for str in the current string and returns the index of the first occurrence of str; returns -1 if not found
class
define a new data type of attributes and behaviors or a blueprint for objects
object or instance
variable from class or instance of a class, can create as many as you need
attributes or instance variable
object's properties, what it know about itself, written as instance variable
behavior or method
what an object does, written as methods
dot operator (.)
used to run methods, asking object to do something ex: object.method();
arguments
constructors
used to initialize the attributes in a newly created object, same name as class, doesnt take any parameter
overloading
when there is more than one constructor, when there are multiple methods with the same name but a different method signature, where it requires a different number or type of parameters
constructor signature
constructor name followed by the parameter which is a list of the types of the parameters and the variable names used to refer to them in the constructor
no argument constructor
a constructor that doesn't take any passed in values (arguments)
parameter list
the header of a constructor, is a list of the type of the value being passed and a variable name. These variables are called the formal parameters. ex: (int width, int height)
call by value
pass a value to a constructor or method it passes a copy of the value,
nonstatic method
one that must be called on an object of a class, works with object's attributes
static method
one that doesn't need to be called on an object of a class, can call by using ClassName.methodName() without creating an object, class name + dot operator + method name
procedural abstraction
allows a programmer to use a method and not worry about the details of how it exactly works
method signature or method header
method name followed by a parameter list
how do you call method from main or outside the class?
create an object and call object.method();
how do you call method from inside the class?
method();
NullPointerException
forgetting to create a new object followed by the class name and attributes, Trying to call a method like indexOf on a string reference that is null. ex: String dog = null; or String dog;
formal parameter
the variables in a method's definition that holds the arguments
arguments or actual parameters
the value of data passed to an object's method, plugs in actual values in the formal parameter
void
not returning anything
toString()
converts an object to a string
string
objects of the string class that holds sequences of characters, can declare a variable to be type string, is a class type
string literal
String word = "hello";
backslash escape sequence
put in a backslash in front of the quote to signal that we want just the character. ex: "Here is a backslash quote " " + " and a backslashed backslash (\) " + "Backslash n \n prints out a new line."
\n
creates a new line
\ "
"
null
object reference doesn't referred to any object yet
The first character in a string is at index - and the last characters is at -
0 and length -1
remember
substring(from,to) does not include the character at the to index! To return a single character at index i, use str.substring(index, index + 1)
compareTo
compares two strings character by character. If they are equal, it returns 0. If the first string is alphabetically ordered before the second string (which is the argument of compareTo), it returns a negative number. And if the first string is alphabetically ordered after the second string, it returns a positive number. (The actual number that it returns does not matter, but it is the distance in the first letter that is different, e.g. A is 7 letters away from H.)
Application Programming Interface (API)
a library of prewritten classes that simplify complex programming tasks for us
Strings are ---
immutable, meaning that they can't change. Anything that you do to modify a string (like creating a substring or appending strings) returns a new string
IndexOutOfBoundsException
trying to access part of a string that is not between index 0 and length -1
string == string
tests to see if they refer to the same object not to see if they are the same. you would use equals or compareTo to see if strings are equal
String(String str)
Constructs a new String object that represents the same sequence of characters as str.
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.
boolean equals(String other)
returns true if this (the calling object) is equal to other; returns false otherwise.
underflow
returning the maximum integer value if you try to subtract one from the minimum value
overflow
returns the minimum integer value if you try to add one to the maximum
autoboxing
the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes
unboxing
the automatic conversion that the Java compiler makes from the wrapper class to the primitive type
Double(double value)
constructs a new Double object that represents the specified double value.
double doubleValue()
returns the value of this Double as a double
The Java compiler applies unboxing when a wrapper class object is:
passed as a parameter to a method that expects a value of the corresponding primitive type.
assigned to a variable of the corresponding primitive type.
The Java compiler applies autoboxing when a primitive value is:
Passed as a parameter to a method that expects an object of the corresponding wrapper class.
Assigned to a variable of the corresponding wrapper class
int intValue()
returns the value of this Integer as an int
integer(value)
constructs a new Integer object that represents the specified int value
wrapper classes
integer class and double class that create objects from primitive types
Math.random()
returns a random number between 0.0-0.99
moves the random number into a range starting from a minimum number
(int)(Math.random()*range) + min
the range
(max number - min number + 1)
Math.pow(number,exponent)
ex: Math.pow(10,2) which means 10 to the power of 2. If you start listing all the permutations possible, you can tell that there are 102 or 100 possible permutations for a 2 dial lock from 0-9.
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)