Learn Java In One Day Flashcards
What are the 8 primitive data types in Java?
Byte
Short
Integer
Long
Float
Double
Char
Boolean
What data range does a byte hold?
-128 to 127
How much storage space does a byte use?
1 byte
What range(Approx) does a short hold and how much space does it use?
-32k to 32k
2 bytes
What range(Approx) does an int hold and how much space does it use?
-2.14 bn to 2.14bn
4 bytes
What range(Approx) does a long hold and how much space does it use?
Less than 2.14bn to more than 2.14bn
8 bytes
What’s the precision of a float?
7 digits
How much memory does a float use?
4 bytes
What’s the precision of a double?
15 digits
How much memory does a double use?
8 bytes
Which primitive data types require a suffix in order to be used. What are the suffixes?
Long and Float.
L and F
What are two good practices to follow when naming variables?
The name should be:
Meaningful
Convey Intent
Camel Casing is used for variables in Java. What is that?
Capitalise the first letter of each word except the first e.g. thisIsGreat
What’s the difference between these statements:
System.out.println(counter++);
System.out.println(++counter);
In the first, Counter is printed then incremented.
In the second, Counter is incremented and then printed
In Java, 7/2 = ?
3
The result is an integer. The result is truncated after the decimal point. Change to 7.0/2 to get a floating point result
What is type casting?
It is where we convert from one data type to another
What is widening primitive conversion?
This is where we convert from a smaller data type to a larger one
Declare an integer array called userAge
int[] userAge;
Declare an integer array of 5 empty elements
int[] anArray = new int[5]
Declare an integer array and initialise it with three integers
int[] anArray = {1,2,3}
How can we quickly sort an array?
Use Arrays.sort(array name)
What’s the syntax for a divide and conquer search of an array in Java?
binarySearch()
E.g. int foundIndex2 = Arrays.binarySearch(myInt, 39);
If the Arrays.binarySearch method cannot find what you’re looking for what will the result be?
A negative and a number that tells you where the thing should have been i.e. it should be at that index number minus 1.
Which algorithm does the Arrays.sort method use?
Quicksort