1 Class structure Flashcards Preview

Java OCA flash cards > 1 Class structure > Flashcards

Flashcards in 1 Class structure Deck (31):
1

What is the sequence to compile and run a java program from command line?

-open the command prompt
-navigate to the directory where the .java file with main method is located
-type & enter: javac filename.java --> the file will get compiled and a .class file will be generated
-type & enter: java filename --> program should start running

Example:
$ javac Zoo.java
$ java Zoo (* NOT Zoo.java)

2

How can the arguments be written in the main method?

In practice, you can write String[] args, String args[] or String...
args; the compiler accepts any of these.

3

What is typed at the command line when arguments need to be passed into main?

compile as normal
add the parameters at the end of the run line
$ javac Zoo.java
$ java Zoo Bronx Zoo (Bronx and Zoo are parameters)

4

What java elements are necessary to compile? to run code?

you need to have a JDK to compile because it includes a compiler.

You do not
need to have a JDK to run the code—a JRE is enough

Java class files run on the JVM and
therefore run on any machine with Java rather than just the machine or operating system
they happened to have been compiled on.

5

Which package is automatically imported to all java classes and doesn't show at the top?

java.lang.
It is automatically imported. You can still type this package in an import statement, but you don’t have to.

6

What are the rules for wildcards?

has to replace a class name
can only have one wildcard in the statement

7

What is a code block that is outside a method?

instance initializers

8

What must happen first before an object reference (non primitive variable) can be referred to?

You can’t refer to a object's reference before it has
been initialized:
{ System.out.println(name); } // DOES NOT COMPILE
private String name = "Fluffy";

9

What is the order of initialization?

Fields and blocks are run first in order, setting
Then the constructor runs.

10

What are the Java primitive types and their sizes?

boolean true or false true
byte 8-bit integral value 123
short 16-bit integral value 123
int 32-bit integral value 123
long 64-bit integral value 123L
float 32-bit floating-point value 123.45f
double 64-bit floating-point value 123.456
char 16-bit Unicode value 'a'

11

How is a long value written?

the integers followed by "l" or "L" Uppercase L is preferred.

12

Can a primitive be assigned "null"?

No, only object references can be assigned null

13

Is this a legal declaration?
int num, String value;

int num, String value; // DOES NOT COMPILE
You can't declare two different types on same line

14

What are the rules for identifiers?

The name must begin with a letter or the symbol $ or _
Subsequent characters may also be numbers
You cannot use the same name as a Java reserved word.
Remember that Java is case sensitive, so you can use versions of the keywords that only
differ in case. Please don’t, though

15

What is a local variable?

A local variable is a variable defi ned within a method

16

Do local variables have a default value?

No, local variables have to be initialized.

17

What happens if you try to run code that has an uninitialized local variable?

It depends if you try to use it. It won't compile if you try to use a local variable that hasn't been initialized. If you declare it but don't use it, then it will just be ignored.

18

Does this compile?
4: public int notValid() {
5: int y = 10;
6: int x;
7: int reply = x + y;
8: return reply;
9: }

No, the local variable X is not initialized at the time it is used. The code won't compile.

19

What does finalize() do?

finalize() may or may not run. It is only called when the object is eligible for garbage collection so don't add it while there is still a variable referring to the object (like a static variable that stays in scope until the program ends.

The finalize() method will run once for each object if/when it is first garbage collected. finalize() call could run zero or one time. It will NEVER run a second time

20

Can a local variable be used outside the method?

No.

21

How are local variables affected by code blocks (Braces { } )?

Remember that blocks can contain other blocks. These smaller contained blocks can reference
variables defined in the larger scoped blocks, but not vice versa.

22

What is the order of variable initialization?

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.

23

What are the default initialization values by type?

boolean false
byte, short, int, long 0 (in the type’s bit-length)
float, double 0.0 (in the type’s bit-length)
char '\u0000' (NUL)

All object references (everything else) null

24

What is the order of initialization?

1. If there is a superclass, initialize it first (we’ll cover this rule in the next chapter. For
now, just say “no superclass” and go on to the next rule.)
2. Static variable declarations and static initializers in the order they appear in the file.
3. Instance variable declarations and instance initializers in the order they appear in the file.
4. The constructor.

25

What are the JavaBeans naming conventions (getters & setters)?

Properties are private. private int numEggs;
Getter methods begin with is if the
property is a boolean.
public boolean isHappy() {
return happy;
}
Getter methods begin with get if the
property is not a boolean.
public int getNumEggs() {
return numEggs;
}
Setter methods begin with set. public void setHappy(boolean happy) {
this.happy = happy;
}
The method name must have a prefix
of set/get/is, followed by the first
letter of the property in uppercase, followed
by the rest of the property name.
public void setNumEggs(int num) {
numEggs = num;
}

26

What does a main method look like that takes parameters?

? ask Terril

27

How can you write a main method that takes parameters?

public class Zoo {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
} }

28

When the main method takes parameters, how can the info be supplied?

In the command line, or ? other ways

29

What does the wildcard import?

It only imports classes in the indicated package. It doesn't import child packages, fields or methods.

30

What package is automatically imported (you don't see the import)?

java.lang

31

import java.nio.file.Files;
import java.nio.file.Paths;

Explain why these imports don't work
1) import java.nio.*;

2) import java.nio.*.*;

3) import java.nio.files.Paths.*;

import java.nio.*; // NO GOOD – a wildcard only matches
//class names, not "file.*Files"
import java.nio.*.*; // NO GOOD – you can only have one wildcard
//and it must be at the end
import java.nio.files.Paths.*; // NO GOOD – you cannot import methods
//only class names