Chapter 3 - Core Java APIs Flashcards
Is java.util.List a class or an interface?
The java.util.List is an interface.
What does the set method does in a list?
Set: replaces an existing value.
Format:
list.set(index,value)
What is the sintax to convert a list to an array?
Convert a list to an array:
Object[ ] objectArray = list.toArray( );
What is the sintax to convert an array to a list?
Convert an array to a list:
String[ ] array = {“Hawk”,”robin”};
List list = Arrays.asList(array);
Can a list size be changed?
List sizes cannot be changed, for example using remove. An UnsupportedOperation Exception is thrown.
Can the date and time classes be instantiated directly?
No. The date and time classes have private constructors to force you to use static methods.
Below code won’t compile:
LocalDate d = new LocalDate( ); // DOES NOT COMPILE
What is the result if invalid numbers are passed to the of( ) method?
Passing invalid numbers to of( ) throws DateTimeException.
Example:
LocalDate.of(2015, Month.January, 32); //throws an exception.
Are the date and time classes mutable or immutable?
The date and time classes are immutable just like the String class.
Do arrays have a lenght method or property?
Arrays have a lenght property NOT a method.
Example:
char[ ] c = new char [2];
int lenght = c.lenght;
Instead of:
int lenght = c.lenght( );
Which method is used in an ArrayList to show the lenght?
ArrayLists have a method that is call size. It shows the lenght of an array. In the arraysList ‘size’ is a method not a property as lenght is in arrays.
Example:
ArrayList l = new ArrayList( ); int lenght = l.size( );
What are characteristics of an array?
- An array has a fixed size.
- An array allows multiple dimensions.
- An array is ordered.
- An array is NOT immutable.
What arrays use for comparisons?
Arrays use object equality while doing comparison. i.e: equals.
What result is generated if a binary search is done for an unsorted array?
To do a binarySearch an array must be previously sorted to get a meaningful result, otherwise an undefined result is thrown.
What are characteristics of an ArrayList?
- An arraylist can change size.
- An arraylist is ordered.
- An arraylist is not immutable.
- An arraylist can have multiple dimmensions.
- ArrayLists implement equality that means same elements in the same order.
== refers to same objects.
Can primitive values be assigned to an array list?
Primitive values cannon be assigned to a list. For example:
List weights = new ArrayList;
You can see if there is an object because of the capital letter –> Double
Does period allow chaining?
Period does not allow chaining. If chaining is present, just the last value in the chain is taken.
Example:
LocalDateTime.of(2015, 5, 10, 11, 22, 33);
Period p = Period.ofDays(1).ofYears(2);
System.out.println(f.format(d));
What are the 2 main uses of wrapper classes?
There are mainly two uses with wrapper classes.
1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
2) To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
What the features of the Java Wrapper Classes?
Features of the Java wrapper Classes.
1) Wrapper classes convert numeric strings into numeric values.
2) The way to store primitive data in an object.
3) The valueOf() method is available in all wrapper classes except Character
4) All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.
What are the method access modifiers?
public - method can be called from any class.
private - method can be called from within the same class.
protected - method can be called from classes in the same package or subclasses.
default (Package Private) Access - The method can only be called from classes in the same package. There is no keyword for default access.