Java Basics Flashcards
Reference types
Reference types refers to objects that they refer to.
A reference points to an object by storing the memory address where teh object is located
Can I assign a String refence variable to Integer later?
No, you cannot assign reference variable to different type.
A reference can be assigned to another object of same type.
What is important about local variables?
You cannot use local variables without initializing.
It cannot be assigned to default value. It contains garbage value if not assigned any value.
If you try to use it before assigning, compile time error occurs
public void findAnswer(boolean check) { int answer; int onlyOneBranch; if (check) { onlyOneBranch = 1; answer = 1; } else { answer = 2; } System.out.println(answer); System.out.println(onlyOneBranch); }
Will this compile?
No, it will not compile.
The compiler throws error.
It complains saying that the the field- onlyOneBranch might have not been initialized
Which method when overriden, called by Garbage collector?
finalize()
How many times a finally could be called by GC?
Zero or one
What are the rules for automatic numeric promotion?
If two values have different data types, Java will automatically promote one of the values
to the larger of the two data types.
2. If one of the values is integral and the other is floating-point, Java will automatically
promote the integral value to the floating-point value’s data type.
- Smaller data types, namely byte, short, and char, are first promoted to int any time
they’re used with a Java binary arithmetic operator, even if neither of the operands is
int. - After all promotion has occurred and the operands have the same data type, the resulting
value will have the same data type as its promoted operands
int x = 3;
int y = ++x * 5 / x– + –x;
System.out.println(“x is “ + x);
System.out.println(“y is “ + y);
X=2
y=7
short x = 10;
short y = 3;
short z = x * y;
What is the actual result?
Compilation error.
We have to downcast it like
short z=(short) (x*y)
What is special about compound operator?
Downcasting will be automatically taken care by compound operator
5 == 5.00 -> result?
TRUE
int a=10;
int b = (a=2);
Syso(a );
syso(b); output?
a=2, b=2
int y = 1;
int z = 1;
final int x = y<10 ? y++ : z++;
System.out.println(y+”,”+z); // Output?
2, 1
What should we keep in mind regarding Switch statements?
The value in each case statement must be a compile time contsant. Means it should be final modifier with constant value assigned to it
Output?
int x = 0;
for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + “ “);
}
Compilation error- cannot redeclare the variable in for loop initialix=zation block
String x=”Hello”;
String y =”Hello”.trim()
x==y?
false.
Because, one is computed at runtime
three ways of creating an array
- int[] abc=new int[3];
- int[] abc=new int[] {1,2,3};
- int[] abc={1,2,3}
what is the method that prints the elements of the array nicely?
java.util.Arrays.toString()
String[] strings = { “stringValue” };
4: Object[] objects = strings;
5: String[] againStrings = (String[]) objects;
6: againStrings[0] = new StringBuilder();
7: objects[0] = new StringBuilder();
line 6 throws compile time error, because StringBuilder cannot fit into String
line 7 throws runime error (ArrayStrore Exception)
Method to sort elements in an array?
Arrays.sort()
What is the requirement of using BinarySearch in array and what if it doesnt met?
The Array should have been sorted before using BinarySearch.. Otherwise, the result will be unpredictable
How to convert to array from List?
list.toArray()
what does list.toArray() defaults to?
Array of object
Object[] array = list.toArray();
What do we need to do if we want String array from list ?
String[] array = list.toArray(new String[0])
What is special about converting from Array to list?
The original array and backed array list are linked
Creating List from Array?
Arrays.asList(array)
private final ArrayList list=new ArrayList<>();
list.add(“String”);
Will it compile? If no, why? if Yes, why?
Yes, becasue we can change the state of the object here. Only problem is when we try to assign list to some other object like list=new ArrayList<>(); Now it throws compile time error
private static int one;
15: private static final int two;
16: private static final int three = 3;
17: private static final int four;
18: static {
19: one = 1;
20: two = 2;
21: three = 3;
22: two = 4;
23: }
Which of these wont compile?
line 17- > because it is not initialized at 17 or in Static Initialization block
line 21 -> final vaiables cannot be reassignes
line 22 -> final variables cannot be reassigned
static imports are meant only for what?
Static imports are meant only for importing static members.
You cant say
import static java.util.Arrays;
instead you can say
import static.java.util.Arrays.*;
What are not a valid overloading?
- Just changing the return type of the method and leaving others unchanged is not the valid overloading
- Instance and static methods with the same method signatures
public void fly(int[] lengths) { }
public void fly(int… lengths) { }
which method is called if we pass an int[]?
Compiler error.
Even though overloading is valid here.
what is the difference between calling a method with VarArg parameters and array parameters?
for Eg..,
- public void fly(int[] lengths) { }
- public void fly(int… lengths) { }
what are the options to call these two methods?
Both can be called with array.
eg. .
1. fly(new int[]{1,2,3})
But, only varargs can eb called like below.
fly(1,2,3)
which will be called when using fly(3);
public void fly(int numMiles) { }
public void fly(Integer numMiles) { }
method with primitive argument.
Java will call the method with most specific value type in the argument
public class Glider2 { public static String glide(String s) { return "1"; } public static String glide(String... s) { return "2"; } public static String glide(Object o) { return "3"; } public static String glide(String s, String t) { return "4"; } public static void main(String[] args) { System.out.print(glide("a")); System.out.print(glide("a", "b")); System.out.print(glide("a", "b", "c")); } }
142
public class TooManyConversions { public static void play(Long l) { } public static void play(Long... l) { } public static void main(String[] args) { play(4); //1 play(4L); //2 } }
what happens on 1 and 2?
- If an int is there, java is happy to convert it to long. means
int -> long is fine in calling methods.
but int -> long -> Long => Java will not do, so compilere error as we dont have methods that take int/long/object arguments - It calls the Long version of method
what is the keyword to call constructer from another constructor in the same class?
And what is more important in that?
this
eg… this (a,b)
And this call must be the first statement inside the costructor from where we call
When should be a final variable must have been assigned?
By the time constructor completes, all final variables must have been assigned
what is functonal programming?
Functional progrmming is a way of writing the code more declaratively
what are the variables that lambda can access?
- Static
- Instance
- Method and local variables - provided there values are not changed
(a, b) -> { int a = 0; return 5;} // will this compile?
No, lambda cannot change the parameters value passed
What is the functional interface that hs a method that returns boolean?
Predicate.
public interface Predicate{
public boolean test(T t);
}
what is the arrayList method that takes the Predicate?
removeIf()