- Standard out / Standard Err
Ans: A Java program writes to Standard Out with System.out.println, which
is redirected to an output file when a Java program is run like this from a Command Prompt Window:
> java Program > out.txt
Standard Out output is colored black in IntelliJ.
On the other hand, a Java program writes to Standard Err with
System.err.println, which is not redirected when Standard Out is redirected.
Standard Err output is colored red in IntelliJ.
- primitive type / class type
Ans: A primitive type value is not a class, whereas an object is instantiated from a class type.
A wrapper class is a class that corresponds to a primitive type according to this table:
Primitive Type | Wrapper Class |
int | Integer |
char | Character |
boolean | Boolean |
double | Double |
A primitive type value must be converted to a wrapper class object before putting it into a collection
such as an ArrayList or a HashMap.
This process is called boxing.
- static method / instance method
Ans: A static method is called from a class; a non-static method is called
from an object. A static method is called a class method in some languatesRuby; a non-static
method is called an object method or an instance method. An example of a static
method: Integer.parseInt. An example of a non-static method the getAge getter
method from the Person class.
- void / other return type for method
Ans: void means that a method doesn't return any value, for example:
// Setter for _age instance variable:
public void setAge(int theAge) {
_age = theAge;
}
If a method does return a value, then the return type is specified
after the public keyword:
// Getter for _age instance variable:
public int getAge( ) {
return _age;
}
- getter / setter
Ans: A getter returns the value of an instance variable; a setter changes (sets)
the value of an instance variable. See the definitions of getAge and
setAge for
the instance variable _age in the answer to problem 4.
- private / public / protected / package What are the
UML symbols for these accessibilities?
Ans: private means that an instance member (variable or method) can only used
from within the class where it is defined.
public means that the instance member can be accesses anywhere.
protected means that the instance member can be accessed in any derived class (not necessarily
in the same package) or anywhere in the package.
package accessability means the instance member can be accessed anywhere in the package. Here
is how you declare a getter with package accessibility:
// No accessibility keyword is used for package accessability//
// This is why it is also called default accessability.
int getAge( ) {
return _age;
}
The UML symbols for private, public, protected, and package are +, -, #, and ~
(tilde), respectively.
- ( ) / [ ] / { } / < >
Ans: Parentheses, ( ), (a) contain arguments when they are passed in to a method, (b) contain parameters in a
function definition, (c) are used to cast one numeric type to another, (d) change the order of precedence of operations,
(e) enclose the condition for if statements and for while or for loops.
Brackets, [ ] , (a) are used for array lookups a[3], (b)
are used for array definitions, for example
int[ ] a = {43, 56, 86, 10};
or
int[ ] a = new int[4];
Braces, { }, are used to enclose the body of a method, bodies of if..else if..else statements, bodies of while or for loops.
Braces are also used to specify the values used to initialize an array.
Angle brackets are used to specify the datatype for an ArrayList or HashMap collection, for example:
ArrayList<Transaction> col =
new ArrayList<Transaction>( );
or
HashMap<String, Transaction> col =
new HashMap<String, Transaction>( );
Angle brackets are also used when implementing some interfaces, for example:
public class Person implements Comparable<Person>
- extends / implements
extends means to create a derived class from a base class, for example:
public class Employee extends Person {
implements means to implement an interface, for example:
public class Cat implements Speaker {
In addition, after implementing an interface, you must supply definitions for
all methods required by the interface.
- ArrayList / HashMap
Ans: In an ArrayList collection, items are inserted and accessed by index.
In a HashSet collection, items are inserted and accessed by key.
- HashMap size / HashMap capacity
Ans: The HashMap size is the number if key-value pairs that are stored;
the HashMap capacity is the total number of slots for storing key-value pairs.
The load factor is the size divided by the capacity.
- Observer / Observable
Ans: An Observer object is an object whose class implements the Observer interface,
which means that it supplies an update method. An Observable object is an object
whose class extends the Observable interface. The Observable objects are the objects
being observed; the Observer objects are the ones doing the observing.
- State / Strategy (software Patterns)
Ans: The State software pattern implements the states in a state diagram as State objects
that implement the State interface. The State objects manage the transitions between
states.
The Strategy software pattern is used when an operation can be implemented using a variety
of algorithms, for example sorting. The user of the pattern can change out the algorithm
at any time.
- Traditional Test / Unit Test
In a traditional test, output is printed to Standard Out. This output
is checked by hand to see if it is correct. In a unit test, the expected output
is included in the test methods. The expected output is compared to the
computed output using an assertEquals method like this:
assertEquals(11, p.getAge( ));
- What does @Override mean?
Ans: @Override is used to show that a method is overriding a
method in the base class or is implementing a method required by an interface.
- What is wrong with this statement?
import java.util;
Ans: The statement needs to either specify the class that will be used, for example:
import java.util.Random;
Alternatively, a star can be used to import all the classes in the package:
import java.util.*;
- How do you run a Java method in a separate thread?
Ans:
- Define a class A that implements the Runnable interface, which has the required method
run. Instantiate an object from A:
A a = new A( );
- Create a Thread object to run the class object A:
Thread t = new Thread(a);
- Start the thread t like this:
t.start( );
This calls the run method of the a object.
- Have the main thread wait for the thread t to terminate by joining
t back with the main thread:
t.join( );
- Describe these Java related technologies:
Swing Ant Maven Gradle
Kotlin Android JSP