1/26
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
variable
has a data type, identifier and a value.
Data Type
how much storage needed for variable
what things it can do
methods that can be executed w the object
creating variable
must declare before use, declare ONCE
dataType variableName
primitive data types
not object, methods can’t work on them, arithmetic operations, contain actual value
int - whole number 4 bytes
double - real numbers, no modulus, 8 bytes
objects
entities in your program that you manipulate by calling methods of that class.
Objects belong to a class; any data type representing an object will start with a capital letter
primitive data types - lowercase
The amount of storage for the object varies so the Variable name points to an address, not the actual data.
The address it points to will be the starting location of the actual data. So it is said to store a reference to the data.
String
a special type of object. Some things are done for you, so you can treat this data type like a primitive data type in many cases.
String variables will point to an address of where the data is located.
String variables can be printed just like a primitive variable, it will print the actual value because of special handling already done for us by Java (even though it contains an address.)
The String data is immutable – meaning there are no methods of the String class that actually CHANGE the value stored.
If you assign a new value to the variable, it will store the address of the new data instead of the address of the original data.
The original data will go away if no variable name is pointing to it.
initializating
assignment goes right to left
age = 16, 16 is assigned to age
declaration and initialization
dataType VariableName = value; ex: int age = 16;
identifier naming rules
1) can only use characters, numbers, underscore “_” and dollar sign”$”
2) cannot start with a number
3) cannot use blanks
4) cannot use any other special symbols such as “,” “/” or “&”
5) cannot use reserved keywords OR identifiers already declared in the program
case sensitive
class - Uppercase
variable and method - lowerCase
identifier types
a. reserved – part of Java language. Ex: public, void, static
b. pre-defined in libraries – ex – method names like println
c. user defined – created by the programmer
class method object
class = blueprint, layout, workspace for an object - something you can manipulate with methods
data = variables
actions = methods
“new” operator from another class to create an object of the class
class won’t create an object, only controls and provides address
class defining object - no main method
class Method
sequence of instructions that accesses the data of an object.
defines behaviors of an object
calling a method - all method names followed by parentheses - distinguished method (holds verb) and variable identifier (holds noun)
what is Printstream
class
out is a pre-defined object of that class created in the System class.
print and println are methods of the PrintStream class.
what is String
String is a class for an object that contains a set of characters.
methods for String class
length – returns the number of characters including special symbols and blanks that are contained in the string object passed as the implicit parameter.
toUpperCase – returns a copy of the implicit parameter converted to have only upper case characters.
toLowerCase - returns a copy of the implicit parameter converted to have only lower case characters.
replace method carries out a search-and-replace operation. It constructs a new string that is obtained by replacing all occurrences of the first explicit parameter with the second explicit parameter.
NOOO mutator methods everything here is a new copy!!
when u want to create an object…
use the Constructor method
Constructor’s job is to instantiate the object – declare and initialize and return the reference (address) of the new object.
special methods w/ own set of rules
To call a constructor, you use the new operator followed by a class name and parameters. as follows:
new Rectangle(5, 10, 20, 30);
When you call a constructor (by using the new operator) you are “instantiating” or “constructing” the object. Unlike other methods, Since there is no object yet, you do not use the object name as an implicit operator.
The instantiation of an object returns the ADDRESS of the object it created. Bank tom = new Bank();
Bank = class, tom = reference name / identifier, Bank() = calls instructor
method name is overloaded when
class has more than one method with the same name (but different parameter types).
creating multiple methods with the exact same name, but different inputs (parameters).
method signature
method name(data type, data type)
parameter = input, what r the types?
Implicit parameter – the object on which the method is invoked
ex: greeting.length() greeting is the implicit parameter to the method length.
Explicit parameter – additional information needed for the method to work. The explicit parameter is passed in a set of parenthesis FOLLOWING the method name.
Example System.out.println(“This is my explicit parameter”); Some methods require multiple explicit parameters, others don't require any.
return values
just printing the answer displays it
returning the value is useful when you calculate a piece of data and give it back to the rest of the program so you can still use it
void - no value returned; compiler error if attempted to print
accessor
returns the state of the object – one specific value such as the color or location of a bug. Usually has no explicit parameters. The return type is listed in the method header and must be the data type of the data being returned.
mutator
modifies the state of the object. ex: A withdrawal from a bank account object.
Usually requires an explicit parameter to contain the new value or the amount to adjust the current state. Usually has a return type of void indicating no data is returned.
test programs
have a main method and call the methods of a class that contains objects.
Classes that control the behavior of an object will not have a main method.
java classes are grouped into…
packages
use import statement to use classes defined in other packages
formal parameter
inside the parentheses of a method - DATA TYPE, not actual value
Which operator is responsible for getting the space needed for an instance of an object?
new
two things every object has
attributes and methods
attributes and behaviors
data and methods
data and behaviors