Chapter 1 - Java building blocks Flashcards
How many public classes a file can have?
- Each file can contain only one public class
2. The file name must match the class name, including case, and have a .java extension.
What is the main method structure?
public static void main (String[ ] args) {
}
Where:
public is the access modifier. It declares this method’s level of exposure to potencial callers in the program.
static binds a method to its class, so it can be called by just the class name. An object does not need to be created to call the method.
void represents the return type. A method that returns no data.
main method: represented as an array of java.lang.String objects.
Valid sintax:
String[ ] args
String args [ ]
String… args
What are imports needed for?
Imports are needed to be able to reference an external class.
What are wildcards used for?
Wildcards (*) are use to import all the classes from the same package. Importing all the classes in a package vs just the class that is going to be used does not slow down the program. Listing each of the classes makes the code easy to read. Using the wildcard can shorten the import list.
Is java.lang package imported automatically?
Yes, Java.lang package is imported automatically, so no import statements need to be used for classes in the lang package. I.e. java.lang.System.
Below are examples of redundants imports:
Import java.lang.System
Import java.lang.*
Import java.util.Random
Import java.util.*
What are the classes import rules?
- A wild card only matches class names.
- Just one wildcard must be present and it should be at the end.
- Just classes can be imported. Methods cannot be imported.
Examples of bad importers:
import java.nio.*;
import java.nio..;
import java.nio.files.Paths.*;
What is the code needed to compile a Java program from the command prompt?
To Compile:
javac package/Class.java
What is the code needed to run a Java program from command prompt?
java package.class
In Java you don’t need to pass the extension when running a program. Example:
java package.ClassB
Instead of java package.ClassB.class
What is the purpose of a constructor?
The purpose of the constructor is to initialize fields, although you can put any code in there.
How a constructor should be defined?
- The name of the constructor should match the name of the class.
- There is no return type in the constructors.
What is a default constructor?
default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.
Where are instance initializers located at?
instance initializers blocks are outside a method.
How is the initialization order?
- Fields and instance initializer blocks are run in the order in which they appear in the file.
- The constructor runs after all fields and instance initializer blocks have run.
What is a literal?
When a number is present in the code, it is called Literal.
Can underscores be included in numbers? Where they can go?
Yes, since Java 7, underscores can be included in numbers to make them easier to read.
- Underscores can be added anywhere EXCEPT at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.
What is a reference type?
A reference type refers to an object (instance of a class).
What a primitive type holds?
Primitive types hold their values in memory where the variable is allocated.
What a pointer is?
references do not hold the value of the object they refer to. Instead they point to an object by storing the memory address where the object is located at. This concept is referred to as a pointer.
Can a reference type be asigned a null?
Yes, Reference types can be assigned null, which means they do not currently refer to an object.
Can a primitive type be assigned a null?
No, Primitive types will give you a compiler error if you attempt to assign them null.
Can reference types call methods?
Yes, reference types can be used to call methods when they do not point to null.
Do primitives have methods declared on them?
Primitives do not have methods declared on them.
What is the purpose of a constructor?
The purpose of the constructor is to initialize fields, although you can put any code in there.
How a constructor should be defined?
- The name of the constructor should match the name of the class.
- There is no return type in the constructors.
What is a default constructor?
default constructor is the one that is not provided by the user but by the jvm. This may be an exam question.
Where are instance initializers located at?
instance initializers blocks are outside a method.
How is the initialization order?
- Fields and instance initializer blocks are run in the order in which they appear in the file.
- The constructor runs after all fields and instance initializer blocks have run.