211 - 240 Flashcards
output:
int a = 30 - 9/5 +1; System.out.println("a = " + a); double b = 30 - 9.0/5 + 1; System.out.println("b = " + b); int c = 30 - 9/5; System.out.println("c = " + c); double d = 30 - 9.0/5; System.out.println("d = " + d); int e = 1 + 9/5; System.out.println("e = " + e); double f = 1 + 9.0/5; System.out.println("f = " + f); int g = 9/5 - 1; System.out.println("g = " + g); double h = 9.0/5 - 1; System.out.println("h = " + h);
a = 30 b = 29.2 c = 29 d = 28.2 e = 2 f = 2.8 g = 0 h = 0.8
Lưu ý:
- Phép trừ trước phân số
- double –> số phải có .
- what is reachable statement in Java?
- ## which one is unreachable?if(false) System.out.println(“a”); //1
while(false) System.out.println(“b”); //2
int n = 5;
while(n > 7) System.out.println(“c”); //3
- reachable: there must be some possible execution path from the beginning of the constructor, method, or static initializer that contains the statement to the statement it self
- -> if unreachable –> compile-time error - while(false) System.out.println(“b”); //2 –> unreachable
ArrayList
- final class?
- what is required to use ArrayList?
- resizable?
- no. It is not a final class –> can be extended
- import java.util.ArrayList;
- yes. Resizable
array = reference variables?
yes
arrays themselves are reference variables, which refer to a collection of objects of similar type
Array - ArrayList
- size
- specify size
- insert primitive type
- generics
- object-oriented
- size
- Array = fixed size
- ArrayList = can grow or shrink its size dynamically. ArrayList is backed by an array - specify size
- creating Array –> need to specify its size or insert elements into it to make sure size is specified
- ArrayList –> specifying size = optional. Initial size is taken care by ArrayList constructor. Initial size is 10 - insert primitive type
- Arrays allow to insert primitive type of data like int, float, double, byte, short, long.
- ArrayList does not allow this –> autobox to wrapper type i.e Integer - Generics
- Array –> cannot
- ArrayList –> can - ArrayList = object-oriented –> can extend from it and add functionality if needed
output --- int a[][] = new int[2][2]; a[0][0] = 0; a[0][1] = 1; a[1][0] = 2; a[1][1] = 3; System.out.println("a = " + a[0][0] + " " + a[0][1] + " " + a[1][0] + " " + a[1][1]); /////// int b[][] = {{0, 1},{2, 3}}; System.out.println("b = " + b[0][0] + " " + b[0][1] + " " + b[1][0] + " " + b[1][1]); /////// int c[][] = new int[][]{{0, 1},{2, 3}}; System.out.println("c = " + c[0][0] + " " + c[0][1] + " " + c[1][0] + " " + c[1][1]); /////// int d[][] = new int[2][]; d[0] = new int[]{0, 1}; d[1] = new int[]{2, 3}; System.out.println("d = " + d[0][0] + " " + d[0][1] + " " + d[1][0] + " " + d[1][1]); int e[][] = new int[2][]; int f[] = {0, 1}; int g[] = {2, 3}; e[0] = f; e[1] = g; System.out.println("e = " + e[0][0] + " " + e[0][1] + " " + e[1][0] + " " + e[1][1]);
a = 0 1 2 3 b = 0 1 2 3 c = 0 1 2 3 d = 0 1 2 3 e = 0 1 2 3
which one is correct declaration?
int i[][] = new int[][]{{0, 1},{2, 3}};
int j[][] = new int{{0, 1},{2, 3}};
int k[][] = new [][]{{0, 1},{2, 3}};
int h[][] = {{0, 1},{2, 3}};
int l[][] = new {{0, 1},{2, 3}};
int m[][] = new int[2][]{{0, 1},{2, 3}};
2 correct declaration:
int i[][] = new int[][]{{0, 1},{2, 3}};
int h[][] = {{0, 1},{2, 3}};
IOException
- checked or unchecked exception?
- what is?
- FileNotFoundException –> baseclass of IOException?
- checked exception
- throw the IOException whenever an input or output operation is failed or interpreted
- no. FileNotFoundException is a subclass of IOException
which one is correct? ----- class A{} class B extends A{} B a = (B) new B(); B b = (A) new B(); B c = (A) new A(); B e = new B(); B f = (B) new A();
- correct: o B a = (B) new B(); o B e = new B(); o B f = (B) new A();
- wrong: o B b = (A) new B(); o B c = (A) new A();
ArrayList –> method:
- to add & add all?
- remove all the ArrayList elements –> = set null?
- remove an element
- add(pos, Collection) - addAll(pos, Collection)
- clear() + # set null
- remove(pos) or remove(Object)
remove(Object) –> removes the first occurrence of the specified element if it’s present
method insert() –> ArrayList or StringBuilder?
StringBuilder
method delete() –> ArrayList or StringBuilder?
StringBuilder
method remove() –> ArrayList or StringBuilder?
ArrayList
arraycopy method signature
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
o src – This is the source array.
o srcPos – This is the starting position in the source array.
o dest – This is the destination array.
o destPos – This is the starting position in the destination data.
o length – This is the number of array elements to be copied.
passing primitive values to method
caller’s copy of primitive type arguments (int, char, etc.) DO NOT change when the corresponding parameter is changed
When you pass primitives to methods it is a straightforward pass by value. A method gets its own copy to play with and any modifications are not reflected outside the method
passing objects reference to method
the fields of the caller’s object DO change when the called method changes the corresponding fields of the object (reference) passed as a parameter
overridden method does not have any throw clause –> what cannot the overriding method do?
if overridden method does not have any throw clause, the overriding method cannot declare any CHECKED exceptions
exception in overriding method (2)
- can choose NOT to throw any exception
- can only throw
o exceptions that are declared in the throws clause of the overridden method
o exceptions that are SUBclasses of the declared exceptions
if a method uses another method that may throw a checked exception –> what should be done?
if a method uses another method that may throw a CHECKED exception, one of the two following things should be true
- the method should be enclosed within a try-catch block
- the method should specify this exception to be thrown in its method signature
9 examples of unchecked exception
unchecked exception = runtime exception
- IndexOutOfBoundsException o ArrayIndexOutOfBoundsException o StringIndexOutOfBoundsException - IndexOutOfBoundsException - ClassCastException - IllegalArgumentException - IllegalStateException - NullPointerException - NumberFormatException
String a = “aaa”;
System.out.println(a.replace(‘a’, ‘b’)); //1
System.out.println(a.replace(‘k’, ‘b’)); //2
//1 --> bbb //2 --> aaa --> nothing changes
when does replace() method create a new String object?
- replace() method creates a new String object.
- replace() method returns the same String object if both the parameters are same, i.e. if there is no change.
- -> “String”.replace(‘g’,’g’)==”String”
when does concat() method create a new String object?
If the length of the argument string is 0, then this String object is returned. Otherwise, a NEW String object is created
String a = “aaa”;
System.out.println(a.concat(“m”)); //1
System.out.println(a.concat(‘m’)); //2
//1 --> aaam //2 --> char is not accepted
byte range?
-128 –> 127
List students = new ArrayList();
- reference type?
- object type?
- reference type is List
- object type is ArrayList
for (initialization; termination; increment) {
statement(s)
}
=======
termination = blank –> meaning?
true
switch(5){
case 6: System.out.println(5); continue;
}
==========
what is the problem?
continue cannot be used outside of a loop –> cannot be used with switch
while (expression) { }
- how to make an infinite loop?
- expression = blank –> can be?
- while (true)
2. no
what do ArrayList and StringBuilder extend?
- StringBuilder extends Object
- ArrayList extends AbstractList
final or not final class?
- String, StringBuilder, and StringBuffer
- wrapper classes (java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.Short)
- Array
- ArrayList
- String, StringBuilder, and StringBuffer –> final
- wrapper classes (java.lang.Boolean, java.lang.Integer, java.lang.Long, java.lang.Short) –> final
- Array –> final
- ArrayList –> not final
use ‘this’ within non-static method?
keyword ‘this’ can only be used within non-static methods
reason: static methods cannot access non static fields or methods
SubClass().methodX(a)
or
SubClass.methodX(a)
SubClass().methodX(a)
lưu ý ()
class A{ A() { print(); } void print() { System.out.println("A"); } } class TestClass extends A{ int i = 4; public static void main(String[] args){ A a = new TestClass(); a.print(); } void print() { System.out.println(i); } }
0 4 ----- 1-Q40 ----- A() { print(); } --> constructor khởi chạy khi tạo instance ở dòng A a = new TestClass();
–> nếu xoá A() { print(); } thì kết quả chỉ còn là 4