ch1 Flashcards
What is an object?
It is an instance of a class (in memory)
What are the two primary elements of a class?
fields/variables
methods
What are variables?
hold the state of the program
what are methods?
Block of code that performs it’s duty when called
What are the three types of comments?
// single
/*
* multi
*/
/**
*javadoc
*/
Can you have two classes in the same file?
yes
Given the following,
public class Animal{
}
class Animal2{
}
What should the filename be named? Why?
Animal because the public class need to match the filename.
Hence the name of the file needs to be Animal.java and not Animal2.java
What the main method in java
public static void main(String[] args)
{
log.info(“Hello”);
}
How do you compile a hello.java?
javac hello.java
java hello (execute it)
What happens when the hello.java is compiled?
It converts to bytecode, i.e hello.class which is something that the JVM care read and exectue
Name the rules of a file name?
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. As you might imagine, a
reserved word is a keyword that Java has reserved so that you are not allowed to use it.
Remember that Java is case sensitive, so you can use versions of the keywords that only
differ in case. Please don’t, though
What package is automatically imported?
java.lang
contains System.
How do you create an instance of a class?
simply write new
Random r = new Random();
Make a constructor for the following class :
public class Hello{
}
public Hello(){
System.out.println(“Constructor”);
}
What is a constructor?
It is a method that matches the class name and does not have a return type.
Is this a contructor?
public void Hello(){}
no because it has a return type = void
What is the purpose of a constructor?
Constructors create Java objects.
Make an instance variable
public class hello{
int number = 0; //instance variable
}
What is a code block
The code between braces is called a code block
How to count an instance initializer?
code that are outside the method
What are the rules of order or 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 intializers blocks have run
List the 8 primities data types
int
double
float
char
boolean
long
short
byte
how do you initialize a variable?
int q = 1;
Are the following declaration legal?
boolean b1, b3;
int i, int i=1;
double d1, double d2;
int i; int w;
int num; number;
int i, double number;
1 fine
2. No
NO for double. you need to do double d1, d2;
int i and int w fine
int num; number; no
last one no because it needs to be of the same data type
Why do local variable need to be initialized?
Cause they do not have default values.
What is a local variable?
A variable defined within a method. They need to be initialized. They do not have defaut values.
What is the default value of initialization of the following:
boolean
byte,short, int, long
double, float
All object references
false
0
0.0
null
State the scope of the local variable
from the declaration to the end of the block
State the instance variable
in scope from declaration until object garbage collected
public class Hell{
int hello = 0; //instance variable
}
Class variables
In scope from declaration until the program ends
static int MAX_LENGHT = 0;
Order the element in a class
P - package
I - import
C - class declaration
Methods and fields go anywhere
Where are all the objects stored in java
My program’s memory called the heap