Chapter 1 - Java building blocks Flashcards Preview

JAVA 8 OCA > Chapter 1 - Java building blocks > Flashcards

Flashcards in Chapter 1 - Java building blocks Deck (68)
Loading flashcards...
1

How many public classes a file can have?


1. Each file can contain only one public class
2. The file name must match the class name, including case, and have a .java extension.

2

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

3

What are imports needed for?

Imports are needed to be able to reference an external class.

4

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.

5

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.*

6

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.*;

7

What is the code needed to compile a Java program from the command prompt?

To Compile:
javac package/Class.java

8

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

9

What is the purpose of a constructor?

The purpose of the constructor is to initialize fields, although you can put any code in there.

10

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.

11

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.

12

Where are instance initializers located at?

instance initializers blocks are outside a method.

13

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.

14

What is a literal?

When a number is present in the code, it is called Literal.

15

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.

16

What is a reference type?

A reference type refers to an object (instance of a class).

17

What a primitive type holds?

Primitive types hold their values in memory where the variable is allocated.

18

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.

19

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.

20

Can a primitive type be assigned a null?

No, Primitive types will give you a compiler error if you attempt to assign them null.

21

Can reference types call methods?

Yes, reference types can be used to call methods when they do not point to null.

22

Do primitives have methods declared on them?

Primitives do not have methods declared on them.

23

What is the purpose of a constructor?

The purpose of the constructor is to initialize fields, although you can put any code in there.

24

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.

25

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.

26

Where are instance initializers located at?

instance initializers blocks are outside a method.

27

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.

28

What is a literal?

When a number is present in the code, it is called Literal.

29

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.

30

What is a reference type?

A reference type refers to an object (instance of a class).