Head First Java Flashcards
What should the first line of your program be in the main class?
public static void main (String[] args){
}
What are the 3 standard loop constructs in Java?
while, do while and for
How do you declare and give initial values to a three-element String array in Jave?
String[] pets = {“Fido”, “Zeus”, “Bin”};
What does random() return in Java? How do you use this?
It returns a number between 0 and just less than one. You use it by multiplying the max amount of your desired range by whatever random return to give you your random number.
In terms of adaptability, what is one of the biggest advantages of OOP?
You can accommodate new requirements without altering previously tested, delivered, and accepted code. There is no need to recompile the existing code.
What does the dot operator do in respect of a class?
It gives you access to the objects state and behavior i.e. the instance variables and the methods,
What’s the function of the main method? Why
It serves to test your classes and to start your program off. The real idea of OOP is to get classes talking to classes.
How do you write a global variable called totalValue in Java?
You don’t. There is no concept of global variables in Java.
What is a manfest text file?
It is a text file bundled with your application classes that defines which class in the zipped jat file holds the main method.
What are 2 types of variable?
Primitive and Reference
What are the four integer primitives in Java?
long, int, short, byte
What are the two floating-point primitives in Java?
float and double
What are the six types of integer and floating-point variables in Java?
float double
byte short int long
What values can be held in a byte, short, integer, and long?
Byte:c. -127 to 127
Short: c.-31k to 31k
Int: c. -2bn to 2bn
long - more than 2bn
How many primitive types are there in Java?
8
Name the primitive variable types in Java
char, bool, byte, short, int, long, float, double
What does an object variable contain?
The address of the object - a reference to the place in memory
Declare an empty integer array
Declare an integer array with 7 empty elements
int[] a
a = new int[7]
Declare an array of Dog objects called pets
Dog[] pets;
Declare an array of Dog objects called pets of length 7. What do we need to do to use the array?
Dog[] pets ;
pets = new Dog[7];
We have declared an array of 7 elements that will hold dog objects but, we have not declared any dogs to go into the array elements.
we could say e.g. pets[0] - new Dog();
If we have an array which holds Dog objects (array called myDogs), how do we access methods of elements in the aray
myDogs[0].bark();
Is it ok not to initialize local and instance variables?
Instance variables - yes, because they will always get a default value of 0, false or null. Local variables, no. You must initialize them before use.
What’s the code to convert a string to an integer?
Integer.parseInt(“3”);
Write a for each loop
for(int number : listOfNumbers){
}