61 - 90 Flashcards
Passed by?
- primitive
- object
o primitive –> value
o object –> object
Java passes all primitive data types by value. This means that a copy is made, so that it cannot be modified. When passing Java objects, you’re passing an object reference, which makes it possible to modify the object’s member variables. If you want to pass a primitive data type by reference, you need to wrap it in an object
String is immutable –> what does this mean?
- created –> content not modified but can call method
- methods that return a modified String value
- return a new String object
- original String value remains the same
access modifiers for local variables
only final can be used
ArrayList - 2:
- indexOf(Object o)
- lastIndexOf(Object o)
- .clone()
- contains(Object o)
- get(int index)
o indexOf(Object o) –> returns the index of the first occurrence of the specified element in this list + return –1 if this list does not contain the element
o lastIndexOf(Object o) –> returns the index of the last occurrence of the specified element in this list + return –1 if this list does not contain the element
o .clone() –> returns a value of the type Object
o contains(Object o) –> returns true if this list contains the specified element
o get(int index) –> returns the element at the specified position in this list
ArrayList - 1:
- remove(int index) & remove(Object o)
- addAll(Collection extends E> c)
- addAll(int index, Collection extends E> c)
- clear()
o remove(int index) –> removes the element at the specified position
o remove(Object o) –> removes the first occurrence of the specified element if it’s present
o addAll(Collection extends E> c) –> appends all of the elements in the specified collection to the end of this list in the order in which they are returned by the specified collection’s Iterator
o addAll(int index, Collection extends E> c) –> inserts all of the elements in the specified collection into this list, starting at the specified position
o clear() –> remove all the ArrayList elements –> after removing all elements –> print out [ ] not null –> # set to null
try-catch-finally –> what does the calling method receive when?
- both catch & finally define return statements
- finally modifies value returned from catch
- catch return primitive
- catch return object
- finally
- finally cannot modify
- finally can modify
what does getClass() return?
always returns the Class object for the actual object on which the method is called irrespective of the type of the reference
setLength()
o method of String or StringBuilder?
o newLength argument = current length
o StringBuilder
o newLength argument length is changed to the specified length
o newLength argument >= current length –> sufficient null characters (‘\u0000’) are appended so that length becomes the newLength argument
A || B khác gì A | B
A && B khác gì A & B
false && false = ?
false || false = ?
true || false = ?
‘&’ performs both tests, while ‘&&’ only performs the 2nd test if the first is also true
A || B khác gì A | B
A && B khác gì A & B
false && false = false
false || false = false
true || false = true
access reference variable - object –> when depends on:
- reference variable
- object
how must variables, methods be declared?
static + khác –> reference variable
non-static method –> object
variables, methods must be declared in both superclass and derived class
what happens when derived classes extend abstract class?
o implement all abstract method from base class
o derived classes have to be defined as abstract class
3 categories of exception. Which ones do not have to be thrown?
only checked is required to be thrown. The two others should not (but can) be thrown
variables defined in the body of interface are implicitly?
Method in interface? (3)
variables - abstract and public, static, final
method
+ must be public
+ no final + no static
interface implement interface and extend class?
no. Interface cannot implement class but can extends many other interfaces with keyword extends
constructors:
o name? o static, final, abstract? o how many constructors per class? o return type? o what is default constructor? Is it important?
o same name as the name of the class in which they’re defined o can many but should not be o no return type, even void --> return type = method o cannot be static, final, abstract o in the absence of a user-defined constructor --> Java inserts automatically + no argument. Without a constructor, a class won't run