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