Chapter 1 Flashcards
What are the key (identifying) benefits of Java
- Object Oriented
- Encapsulation
- Platform independent
- Robust
- Simple
- Secure
- Multithreaded
- Backward Compatibility
What key pieces does the Java Development Kit contain?
- The compiler (javac)
- The launcher (java)
- The Java Virtual Machine (JVM)
- javadoc
- jar
What is an object?
A runtime instance of a class in memory
What is a reference?
A variable that points to an object.
What are the two primary elements of a Java class?
Methods and fields.
What is the simplest valid Java class we can write?
public class Animal { }
The method signature consists of .. ?
The method name and parameters.
Does Java require a class to be public?
Java does not require a class to be public
Given a file with two or more classes, how many are allowed to be public? How many must be public?
Only one is allowed to be public (must match file name) & none are required.
Where does Java begin execution?
By calling the main() method.
What command do we use to check the version of java?
java -version
Using the terminal, how can we compile and then run the file Zoo.java?
- javac Zoo.java
2. java Zoo
Using the terminal with single-file source-code, how can we compile and then run the file Zoo.java?
- java Zoo.java
What is the constraint of single-file source-code?
It can only be used if your program contains one file.
What are the differences between using the commands javac + java and single-file source-code to compile and run a program?
Full command-javac Zoo.java java Zoo Bronx Zoo
- need to use two commands
- Produces a class file
- For any program
- Can import code in any available Java library
Single-file source-code - java Zoo.java Bronx Zoo
- one command
- Fully in memory (no class files)
- For programs with one file
- Can only import code that came with the JDK
Is using java.util.Random more efficient than using java.util.* to import the Random class?
Java automatically imports only the classes that are needed so in the end only Random will be importer in both cases.
You might think that including so many classes slows down your program execution, but
it doesn’t. The compiler figures out what’s actually needed.
There is one special package in Java that is implicitely imported by Java. Which one is it?
java.lang
What package do the Files and Paths class belong to?
java.nio.file
How many wildcards (*) are allowed in a package declaration?
One, and it must be at the end.
What does java do with “ties” for precedence in import declarations?
example:
import java.util.Date;
import java.sql.Date;
It throws an error:
- error: reference to Date is ambiquous
Compile and run the following two classes using the java terminal commands:
- javac packagea/ClassA.java packageb/ClassB.java
- java packageb.ClassB
Where does the javac command place the compiled classes by default?
In the same directory as the source code.
How can we use the javac command to compile ‘packagea/ClassA’ in the directory ‘classes’
javac -d classes packagea/ClassA.java
Compile and run the two classes below using the java terminal commands. Compile them into a directory called ‘classes’.
- packagea/ClassA
- packageb/ClassB (contains the main method)
- javac -d classes packagea/ClassA.java packageb/ClassB.java
- java -cp classes packageb.ClassB
What options are there to specify the classpath to the java or javac command?
- ‘-cp’
- ‘-classpath’
- ’–class-path’
Create a JAR file: ‘myNewFile.jar’ based on the current directory.
jar -cvf myNewFile.jar .
– or –
jar –create –verbose –file myNewFile.jar .
Create a JAR file: ‘myNewFile.jar’ based on the directory ‘dir’
jar -cvf myNewFile.jar -C dir .
Is the following entry point method allowed?
- static public void main(String [ ]args) { }
yes