1 Class structure Flashcards
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