Java Interview | Selected basic Questions

Source: Internet
Author: User
Tags instance method stringbuffer

1. Reflection
1.1 Definitions
The Java reflection mechanism is a running state in which all the properties and methods of the class are known to any class, and any one of its methods can be called for any object.

1.2 Effects
① determine the class to which any object belongs at run time
② to construct an object of any class at run time
③ determines the member variables and methods that any class has (through the setaccessible () method to access or modify private members) at run time
④ method to invoke any object at run time

1.3 Usage
First you have to get the class bytecode object, and then you can get the various properties and methods in the class through the class object.
3 Ways to get the class object:
1. Through the GetClass method of the object class

Class clazz = Foo.getclass ();
2. Getting an object from an object instance method

Class clazz = Foo.class;
3. By Class.forName Way

Class Clazz = Class.forName ("Xx.xx.foo");//complete class name, including package
Example:

public class User {
private String name;
private int age;

private void speak(String name){    System.out.println("我的名字是:"+name);}public User(String name,int age)) {    this.name = name;    this.age = age;}

}
User user = New User ("Zhang San", 25);
Get all methods in the user class
Method[] methods = User.class.getDeclaredMethods ();
Get all properties in the user class
field[] fields = User.class.getDeclaredFields ();
Traverse all properties of the user class
for (int i = 0; i < fields.length; i++) {
Fields[i].setaccessible (TRUE);
System.out.println (Fields[i].getname () + ":" +fields[i].get (user));
}
Results:

Name: Zhang San
Age:25
(Reflection can basically get all the information in the class, please Google yourself)

1.4 Advantages and Disadvantages
Advantages:
1. The ability to dynamically obtain instances of classes at runtime greatly improves the flexibility and extensibility of the system;
2. Combined with Java Dynamic compilation, you can achieve extremely powerful functions.
Disadvantages:
1. The performance of using reflection is low;
2. It is relatively unsafe to use reflection;
3. The encapsulation of classes is broken, and the properties and private methods of this class can be obtained by reflection.

The difference between 2.String, StringBuilder and StringBuffer
Store
string constant, which cannot be changed once the object is created
StringBuilder and StringBuffer: String variables, objects can be changed

Execution speed
string<stringbuffer<stringbuilder;
Thread Safety
String,stringbuilder is thread insecure, StringBuffer is thread-safe.
Scope of application
String: Applies to a small number of string operations
StringBuilder: For a single thread, large number of string operations
StringBuffer: For multi-threading, a large number of string operations
3. Single-Case mode
Role
Ensure that only one instance of a class exists in a Java program.
Singleton mode in spring
The spring build object is singleton by default, and you can set the scope property to prototype to multiple instances
<bean id= "HI" class= "com.test.Hi" init-method= "Init" scope= "prototype" >
Applicable scenarios
1. Objects that need to be created and destroyed frequently;
2. Objects that are too time consuming or resource intensive, but often used when creating objects;
3. Tool class objects;
4. Objects that frequently Access databases or files.
is the 4.Java + + operator thread-safe?
is not a thread-safe operation. It involves multiple instructions, such as reading the value of a variable, increasing it, and then storing it back into memory, and this process may occur when multiple threads are involved.

The difference between 5.== and equals

1. Compare basic data types (int,float,double ...) , the comparison is whether their values are equal
2. Compare reference types (such as the string class, custom user class, etc.), comparing whether the object pointed to by the reference is equal, that is, whether the object memory address is the same
equals
The Equals method is provided by the object class and can be overridden by a subclass
The default implementation of the object class is as follows:

public boolean equals (Object obj) {
return (this = = obj);
}
The default implementation returns true only if the object is compared to itself, and the "= =" is equivalent
Of
Many classes in Java (The String class date class file Class) have overridden the Equals method, which
A common string class in the

public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
if (anobject instanceof String) {
String anotherstring = (string) anobject;
int n = value.length;
if (n = = anotherString.value.length) {
Char v1[] = value;
Char v2[] = Anotherstring.value;
int i = 0;
while (n--! = 0) {
if (v1[i]! = V2[i])
return false;
i++;
}
return true;
}
}
return false;
}
Here, as can be seen from the overridden Equals method of string, the comparison is whether the contents of a string are equal
The Equals method is actually given to the developer to rewrite, in the custom class, you want to use it to compare anything, as long as you rewrite it, so we can not simply say it is used to compare what

6. How arrays are allocated in memory
Notice before reading:
Stacks: Storing Object references
Heap: Storing all new objects and arrays

Arrays can hold basic data types and can hold reference types. The reference to the array is stored in the stack, and the objects actually reside in the heap.
You can see the following code and analysis:

Store basic data types
int[] arr = new INT[3];
Arr[0] = 1;
ARR[1] = 2;
ARR[2] = 3;

Store Custom type User
User[] Userarr =new user[3];
User User1 = new User ("Zhang San");
User User2 = new User ("John Doe");
User[0] = user1;
User[1] = user1;
USER[2] = User2;
Run Step Analysis:

Store basic data types
1. Create an arr reference in the stack
2. Create an int array of length 3 in the heap and initialize it, assigning the default value of 0
3. Point an ARR reference to an int array
4. Re-assign values to each int value in an int array

Store reference type User
1. Create a Userarr reference in the stack
2. Create a user array of length 3 in the heap and initialize it, assigning a default value of NULL
3. Point the Userarr reference to the user array
4. Create a user1,user2 reference in the stack
5. Create user ("Zhang San") in the heap, user ("John Doe") object
6. Point User1,user2 to User ("Zhang San"), User ("John Doe")
7. Re-assign each user in the user array, at which point User[0] points to User ("Zhang San"), User[2] to user ("Zhang San"), User[2] to user ("John Doe")
PS: The point referred to here is that the reference variable points to the object, that is, the reference variable holds the storage address of the object in the heap memory
Can see, better understand:
Picture description
PS: The base data type that an array holds may be stored in the heap or stored in a constant pool in the method area, only as an example of the base data type in the heap.

Java Interview | Selected basic Questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.