java Flashcards
what is void?
Used at method declaration and definition to specify that the method does not return any type, the method returns void
what is the ‘main’ function?
it is the entry point of the class
what is an access modifier
keywords for other classes and methods that shows they can use these methods
example:
public, private
what is a class
a container for related methods
what is byte code when talking about java programs
the platform independent code that gets turned into native code for the platform (Mac, windows, etc) that you are running by the java virtual machine (software component of java runtime environment, which you need to download on your system)
written as Name.class
what is the equivalent of comma in numbers in java. how would you write 123,456,789?
123_456_789
How do you write a type long?
add an L at the end.
example: 3_123_456_789L
How do you write a type float?
ad an F at the end
10.99F
does a char use single or double quotes
single
are strings mutable in java
no they are immutable
are arrays fixed length in java
yes
How does Java code run?
COMPILE TIME
JavaFile.java (java code)
- this is the file that contains our java code
compiler
- this converts the java code to byte code
JavaFile.class (byte code)
- byte code is able to be read by the java virtual machine
---------- RUN TIME EXECUTION class loader - loads the byte code into memory - part of JVM that loads the the byte code
byte code verifier
- checks if everything is written correctly in the byte code
- checks the loaded byte code is valid and does not violate any rules of security
interpreter
- reads the byte code and executes the program
What does capitalized data type represent in java
Capitalized data types are non-primitive
Lowercase data types are primitive types
What value represents a variable that is not initialized yet
null
Why won’t this compile?
int n = null;
primitive types cannot be initialized to null
a variable of a reference type can refer to a special null value that represents the fact that it is not initialized yet or doesn’t have a value.
What is does the keyword final do?
Such variables are known as constants. Java provides a special keyword called final to declare them. The only difference between a regular variable and a final variable is that we cannot modify the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of the program.
How doo final variables work similar to const in javascript?
Yes
Important, that it is always possible to change the internal state of an object point by a final reference object, i.e. the constant is only the variable itself (the reference), not the object to which it refers.
final StringBuilder builder = new StringBuilder(); // "" builder.append("Hello!"); // it works System.out.println(builder.toString()); // Hello!
You can you initialize a final variable without a value. But what must you do to prevent the compiler from throwing an error?
You have to set a value to the final variable before you use it.
final boolean FALSE;
System.out.println(FALSE); // error line
final boolean FALSE; // not initialized
FALSE = false; // initialized
System.out.println(FALSE); // no errors here
Why use getters and setters?
To prevent unwanted actions on our classes
example: dog.height = 0; // we never want dog height to be 0 // so we create a setter that prevents this from happening
public void setHeight(int h) { if(h > 0) { height = h; } }
What is encapsulation?
refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object’s components.[1] Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them. Publicly accessible methods are generally provided in the class (so-called “getters” and “setters”) to access the values, and other client classes call these methods to retrieve and modify the values within the object.
What are the default values of Number primitives, booleans, and objects
0, false, null
What is the difference between .equals and ==
Use == to compare two primitives, or to see if two references refer to the same object.
Use the equals() method to see if two different objects are equal.
True or false?
int a = 3;
byte b = 3;
a == b
true
How do you declare an array?Instantiate and initalize? Most general way to Instantiate and initalize an array?
To declare an array we must use two special characters [] after the name of the type of elements in the array:
int[] array; // declaration’s form 1
or after the name of an array variable:
int array[]; // declaration’s form 2: less used in practice
The most general way to create an array is to use the special keyword new and specify the necessary number of elements:
int n = …; // n is a length of an array
int[] numbers = new int[n];
int[] numbers = { 1, 2, 3, 4 }; // instantiating and initializing an array of 1, 2, 3, 4
It’s possible to separate declaration and instantiation in two lines:
int[] numbers; // declaration
numbers = new int[n]; // instantiation and initialization with default values
Also, we can write the keyword new and enumerate all elements of an array:
float[] floatNumbers; // declaration
floatNumbers = new float[] { 1.02f, 0.03f, 4f }; // instantiation and initialization
What happens if you try to access a non-existing element by an index in an array?
If we try to access a non-existing element by an index then a runtime exception happens.
How do you convert an array to a string?
Arrays.toString(array)
How do you fill an array with values?
java.util.Arrays.fill() method is in java.util.Arrays class. This method assigns the specified data type value to each element of the specified range of the specified array.
What does StringBuilder do?
used to create mutable string objects
An object of this class is similar to a regular string, except that it can be modified.
What is the difference between length and capacity on the StringBuilder class?
The length returns the actual number of characters when capacity returns the amount of storage available for newly inserted characters, beyond which an allocation will occur. The capacity is a part of the internal representation of StringBuilder and its value will dynamically change.
When we need a lot of concatenations what will work faster StringBuilder or a String?
StringBuilder
Use 2 different methods to create a two dimensional array of integers
// two-dim array - the array of arrays int[][] twoDimArray = { {1, 2, 3, 1}, // first array of int {3, 4, 1, 2}, // second array of int {4, 4, 1, 0} // third array of int };
// another way int[][][] twoDimArray = new int[3][4];
How do you look over a 2 dimensional array in a 3 dimensional array called cubic using a for each loop?
// For each loop for (int[][] dim2Array : cubic) { for (int[] vector : dim2Array) { System.out.println(Arrays.toString(vector)); } System.out.println(); }
How would you write a isAnagram method?
static boolean isAnagram(String a, String b) {
if(a.length() != b.length()) return false;
int c[] = new int[26], d[] = new int[26] ;
a = a.toUpperCase();
b = b.toUpperCase();
for(int i=0; i
What is an exception?
errors detected during the program execution (at runtime)
Implement depth first search
bfs(root) {
let result = [];
let queue = [];
queue.push(root);
while(queue.length) { let curr = queue.shift(); result.push(curr.value) if (curr.left) { queue.push(curr.left) } if (curr.right) { queue.push(curr.right) } }
return result;
}
What is an interface?
A special kind of a class that can’t be instantiated. Often (but not always) it contains only abstract methods that you can implement in the subclasses.
An interface can’t contain fields (only constants), constructors and non-public abstract methods.
An interface can contain:
- public constants;
- abstract methods without an implementation (the keyword abstract is not required here);
- default methods with implementation since Java 8 (the keyword default is required);
- static methods with implementation (the keyword static is required).
Declare an interface with a constant, an instance method, a static method, and a default method
Let’s take a closer look at this interface. The variable INT_CONSTANT is not a field here, it’s a static final constant. Two methods instanceMethod1() and instanceMethod2() are abstract methods. The staticMethod() is just a regular static method. The default method defaultMethod() has an implementation but it can be overridden in subclasses.
interface Interface {
int INT_CONSTANT = 0; // it's a constant, the same as public static final INT_FIELD = 0 void instanceMethod1(); void instanceMethod2(); static void staticMethod() { System.out.println("Interface: static method"); } default void defaultMethod() { System.out.println("Interface: default method. It can be overridden"); } }
What are marker or tagged interfaces?
An interface can have no members at all.
public interface Serializable{
}
How would you implement an interface with 2 instance methods? Each instance method should print out it’s method name.
Interface to implement:
interface Interface {
int INT_CONSTANT = 0; // it's a constant, the same as public static final INT_FIELD = 0 void instanceMethod1(); void instanceMethod2(); static void staticMethod() { System.out.println("Interface: static method"); } default void defaultMethod() { System.out.println("Interface: default method. It can be overridden"); } }
class Class implements Interface {
@Override public void instanceMethod1() { System.out.println("Class: instance method1"); }
@Override public void instanceMethod2() { System.out.println("Class: instance method2"); } }
Interface instance = new Class();
instance. instanceMethod1(); // it prints “Class: instance method1”
instance. instanceMethod2(); // it prints “Class: instance method2”
instance. defaultMethod(); // it prints “Interface: default method. It can be overridden”