Niu Ke's Diary (October 29, 2021)

Niu Ke's Diary (October 29, 2021)

Title:

The following about Object The correct statement of class is ()
A.Java All classes in inherit directly or indirectly from Object,Whether explicitly specified or not, whether it is an abstract class or not.
B.Java Interface in(interface)Also inherited Object class
C.Use“=="When comparing two objects, Java Call inherited from Object of equals Method to determine whether they are equal.
D.If there is no redefinition in the definition of the class toString()Method, the object created by this class cannot be used toStrig()method.

Resolution:

Object is the base class, and all classes in Java inherit directly or indirectly; therefore, A pair
Derived from one class must be another class. Object is a class. If the interface inherits from object, the interface must be a class, so B is wrong
When using the equals() method for comparison, you will call = = to see the source code of the equals() method. It can be said that = = is more powerful than equal, so C is wrong
The toString() method can be used in the Object class even if it is not overridden, so D is wrong

Title:

We often use it in our programs“ System.out.println()"To output information, in the statement System Is the package name, out Is the class name, println Is the method name.

A.yes
B.wrong

Resolution:

System is a class in java.lang. out is a member variable in the system. This variable is an object of java.io.PrintStream class. println is a method.

Title:

In the following description, the error is ()
A.File Class can store file properties
B.File Class can read and write files
C.File Class can create files
D.File Class can get file directory information

Resolution:

Correct answer: B

The File class can manipulate the File itself, but cannot modify the File content

Title:

After jdk1.5, the output of the following java program is __.

int i=0;
Integer j = new Integer(0);
System.out.println(i==j);
System.out.println(j.equals(i));

A.true,false
B.true,true
C.false,true
D.false,false
E.The results are different for different environments
F.The program cannot be executed


Resolution:

Correct answer: B

The overall is mainly divided into two aspects
① Compare values
1, When comparing a basic data type with a reference data type, the reference data type will be unpacked, and then the values will be compared with the basic data type
give an example:
int i = 12;
Integer j = new Integer(12);
i == j returns true
2, Compare the reference data type with the basic data type (equals method) , the basic data type is automatically boxed and compared with the reference data type. The equals method in the Object compares addresses, but the Integer class has overridden the equals method. As long as the values of the two objects are the same, they can be regarded as the same Object. See the API documents for details, so this is also a value comparison in the final analysis!
give an example:
int i = 12;
Integer j = new Integer(12);
j.equals(i) returns true
② The comparison is the address
1, If the reference data type is like this, Integer i = 12; get the object directly from the constant pool. If the value is between - 128 and 127, it will be regarded as the same object, otherwise it will be regarded as different objects
give an example:
Integer i = 12; Integer j = 12; i == j returns true
Integer i = 128; Integer j = 128; i == j returns false
2, If the reference data type is direct new, no matter whether the value is the same or not, the two objects are different, because they will open up a space in the heap memory
give an example:
Integer i =new Integer(12);
Integer j = new Integer(12);
i == j returns false
3, Objects from the constant pool are also different from those from new
give an example:
Integer i = 12;
Integer j = new Integer(12)
i == j returns false at this time, because the second statement actually has two new objects!!!

Title:

class A {}
class B extends A {}
class C extends A {}
class D extends B {}
Which four statements below are correct?

A.The type List<A>is assignable to List.
B.The type List<B>is assignable to List<A>.
C.The type List<Object>is assignable to List<?>.
D.The type List<D>is assignable to List<?extends B>.
E.The type List<?extends A>is assignable to List<A>.
F.The type List<Object>is assignable to any List reference.
G.The type List<?extends B>is assignable to List<?extends A>.

Resolution:

Correct answer: A C D G

After reading it patiently, I'm sure I can understand this problem!

  1. Just look inside the angle brackets!! clarify the two concepts of point and range
  2. If the angle bracket contains a class, the angle bracket contains a point, such as List,List,List
  3. If there is A question mark in angle brackets, it represents A range, <? Extends A > represents A range less than or equal to A, <? Super A > represents A range greater than or equal to A, and <? > represents all ranges
  4. It is wrong for all points in angle brackets to assign values to each other, except for two identical points
  5. Angle brackets assign a small range to a large range. Yes, a large range assigns a small range. Wrong. If a point is included in a range, it can be assigned. Otherwise, it cannot be assigned
  6. List <? > and list are equal and both represent the maximum range

7. Supplement: List is both a point and a range. When it represents a range, it represents the maximum range
Paste the following code:

public static void main(String[] args) {
		List<A> a;
		List list;
		list = a;   //A yes, because List is List <? >, which represents the maximum range. A is only one of the points and must be included
		List<B> b;
		a = b;      //B error, points cannot be assigned to each other
		List<?> qm;
		List<Object> o;
		qm = o;     //C yes, list <? > represents the maximum range, and list < Object > is just a point, which must be included
		List<D> d;
		List<? extends B> downB;
		downB = d;  //D pair, list <? Extends b > represents the range less than or equal to B, and list < d > is a point in which
		List<?extends A> downA;
		a = downA;  //E error, range cannot be assigned to point
		a = o;      //F error, list < Object > is just a point
		downA = downB;  //G pair, the range less than or equal to a includes the range less than or equal to B, because B is smaller than A. B is a subclass of A
	}

Title:

The following are about Java in queue Which statements are correct ()?

A.LinkedBlockingQueue Is an optional bounded queue and is not allowed null value
B.PriorityQueue,LinkedBlockingQueue Are thread unsafe
C.PriorityQueue Is an unbounded queue and is not allowed null Value, the time complexity of entering and leaving the team is O(log(n))
D.PriorityQueue,ConcurrentLinkedQueue All follow FIFO principle

Resolution:

right key
AC
Answer analysis
A. LinkedBlockingQueue is an optional bounded blocking queue based on node links. null values are not allowed.
B. LinkedBlockingQueue is a thread safe blocking queue that implements features such as first in first out.
C. PriorityQueue is a * * * queue. null value is not allowed. The time complexity of queue entry and queue exit is O (log(n)).
D. PriorityQueue is another queue different from the first in first out queue. The elements with the highest priority are taken out of the queue every time. ConcurrentLinkedQueue is a * * * thread safe queue based on linked nodes, and the elements of the queue follow the FIFO principle.

Keywords: Java Back-end

Added by bossman on Fri, 29 Oct 2021 03:13:06 +0300