1 Class structure Flashcards
(31 cards)
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)
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.
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)
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.
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.
What are the rules for wildcards?
has to replace a class name can only have one wildcard in the statement
What is a code block that is outside a method?
instance initializers
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”;
What is the order of initialization?
Fields and blocks are run first in order, setting
Then the constructor runs.
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'
How is a long value written?
the integers followed by “l” or “L” Uppercase L is preferred.
Can a primitive be assigned “null”?
No, only object references can be assigned null
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
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
What is a local variable?
A local variable is a variable defi ned within a method
Do local variables have a default value?
No, local variables have to be initialized.
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.
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.
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
Can a local variable be used outside the method?
No.
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.
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.
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
What is the order of initialization?
- 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.) - Static variable declarations and static initializers in the order they appear in the file.
- Instance variable declarations and instance initializers in the order they appear in the file.
- The constructor.