Java Building Blocks Flashcards
Is Java the coolest?
Yes. Yes it is.
What is a method?
A method is a block of code with a unique signature that defines what an object does.
What does void mean in a method signature?
It means that the method returns no value to whatever called it.
What is a parameter in the context of a method?
A parameter is information supplied to the method so that it can perform necessary actions.
What are the minimum components of a method signature that are required for code to compile (ignoring rules about constructors)?
A method must have a return type, a valid identifier, parenthesis for parameters, and a code block. Example:
void a() { }
How many public classes can there be in a single Java file?
1, though there can be other classes within a file so long as only one is declared public.
What method is required to begin execution of a Java program?
The main method.
When you compile a *.java file using the javac command, what is the result?
A file of bytecode with the same name as the *.java file with a file extension of .class.
What is the correct method signature of a main method?
It must be declared public and static. Its return type must be void. The method name must be “main” without caps, etc. It must accept an array or varargs of Strings. Example:
public static void main(String[] args) { }
public static void main(String… args) { }
When passing arguments via command line to a Java main method, what is the delimiter between arguments?
The space character.
If you want an argument to contain a space, how do you pass it to a Java main method?
You enclose the argument in double quotes. Example:
java ProgToRun arg “arg with space”
When passing arguments into java via the command line, what type of object or primitive is initially created from the argument?
A String. This is true regardless of what is typed in as an argument. Thus 2 is parsed as a string when input as an argument to java.
When writing a Java class, what imports do you get by default?
All the items in java.lang. Effectively this:
java.lang.*;
What are the minimum requirements for an import statement?
The import keyword, a valid package and class OR valid package and wildcard, and a semicolon. Example:
import java.lang.String;
import java.lang.*;
What does the wildcard (*) do in an import statement?
It tells the compiler that it may use ALL classes in a particular package.
Does the import wildcard (*) allow the compiler access to classes in sub-packages?
No.
When using the import wildcard (*) does the compiler add all the classes from the package when compiling the code?
No. The compiler may have access to all the classes in the package, but it only uses the ones it actually needs. Thus using the wildcard (*) does not negatively impact the compiler.
Can you use import wildcards (*) when packages contain classes of the same name?
No. This is compile error. You must explicitly import classes where class names collide.
Are import statements required?
No. A class might only use classes in java.lang.*. Additionally, you can explicitly declare all variables with a fully qualified package, like this:
public class A { public com.cerner.OurClass ourClass = new com.cerner.OurClass(); public static void main(String... args) { java.lang.System.out.println("This compiles!"); } }