Java Language Basics (interview related)

Source: Internet
Author: User
What are the characteristics of the 1.java language? Object-oriented platform independence, one compilation, running everywhere Java provides a lot of built-in class libraries Java has mentioned the support of Web application development has better security and robustness than C + + language more concise, rigorous 2. What is the byte order of Java?

Java byte order refers to the order in which multibyte types of data are stored in Java virtual machines, and the Java byte sequence is Big-endian (big-endian). the final keyword in 3.java. The final keyword can be used for member variables, local variables, methods, and classes. The final member variable must be initialized at the time it is declared or initialized in the constructor, or a compilation error will be reported. You are not able to assign a value to the final variable again. Local variables must be assigned at the time of declaration. All variables in an anonymous class must be final variables. The final method cannot be overridden. The final class cannot be inherited. The final keyword differs from the finally keyword, which is used for exception handling. The final keyword is easy to confuse with the Finalize () method, which is defined in the object class and is invoked by the JVM before garbage collection. All the variables declared in an interface are final in themselves. Final and abstract These two keywords are inverse related, the final class can not be abstract. The final method is bound in the compile phase, called a static binding (binding). The final variable called blank final that was not initialized at the time of Declaration (blank final
variable), they must be initialized in the constructor, or call this () initialization. If you do not, the compiler will complain that the final variable (variable name) needs to be initialized. Declaring classes, methods, and variables final can improve performance so that the JVM has an opportunity to estimate and then optimize. In the Java Code convention, the final variable is a constant, and the pass-by-volume name is capitalized

finally:

Interfaces can be implemented more than one, and inheritance can only inherit the difference between formal parameters and arguments.

Formal parameters:

Called "Formal parameter", is the parameter that is called when the method name and method hair body are defined. Wood is used to receive the method when the parameters passed, it is the scope of the whole method, the method is called a temporary variable, the actual parameters in the keynote first method, in the method call when the value of the miserable to the corresponding formal parameters, Only formal parameters can be used inside the called method, and formal parameters cannot be used.

The difference between an argument and a formal parameter: the scope of the formal parameter is within the method, and the life cycle of the formal parameter ends when the method call is finished, so you cannot use formal parameters outside of the method. It only allocates memory cells when called, and immediately frees up the memory space when the call ends, and the argument cannot be used inside the calling method. When a method is invoked, only the value of the argument can be passed to the formal parameter, but the parameter's value cannot be passed back to the argument, so a change to the value of the formal parameter does not affect the value of the argument during the invocation of the method. the difference between & and &&.

1.& is bitwise-and-operator, A&b is the conversion of A and b to binary numbers, and then bitwise-and-bitwise operations. && is the logical AND operator, A&&b is true when the value of two counts is true, otherwise the result is false;
2. In addition, && also has a short-circuit effect, in the participating operations in the two expressions, only if the result of the first expression is true, the second expression will continue to evaluate the value. variable Parameters

Example:

String ... A, where string is the type of the variable parameter, and a is its name
Characteristics:
1. Can only appear as the last parameter,
2. Can only be located between the variable's type and the variable name. String ... A;
3. The compiler implicitly creates an array of variable parameters that can be accessed by means of an array of three methods of accessing a class (bytecode object) object by calling the object's. GetClass () method through the Class.forName (" Package name. Class name "To obtain by using the class name,. class syntax. access Modifiers

In the Java language, the permission access modifiers for a class are as follows: private default protected public. Private:private can modify data members, construct methods, and method members, and you cannot modify classes, members that are decorated by private, and can only be used in classes that define them and cannot be invoked in other classes. (default permissions are available only in this class): classes, data members, constructor methods, and method members can use default permissions. The default permissions are the same as package permissions, and elements of the same package permission can only be invoked in the class in which they are defined and in the class of the same package (this class and the same package class). Protected permissions (Protected): You can modify the data member construction methods and method members, you can not modify the class, the class that is protected decorated can only be accessed in the classes and subclasses that define it (this class and subclasses). Public permissions (????????.). Class, data members, constructor methods, and method members can be decorated. Members that are decorated by public can be invoked in any class, regardless of the same package or a different package. Is the most privileged modifier (this package and different packages). The permissions for the access modifier are from large to small: public protected default and private. three main characteristics of object-oriented. inherit the wrapper polymorphic this and super.

Keyword this refers to the current object's reference key
Super refers to the application of the parent class within the current object.
The wildcard super can call the parent class's construction method, the parent class's method and the property instance method and the class method. instance method: No static modifier, also known as Non-static Method class method: Static modification, also known as the Statics method

Detailed

Instance method:
When a byte-code file for a class is loaded into memory, the instance method of the class is not assigned an entry address, and the instance method assigns the entry address only after the object of the class is created. Thus instance methods can be invoked by all objects created by the class, it is also important to note that when we create an object for the first class, the entry address of the instance method completes the assignment, and when the subsequent object is created, the new entry address is not assigned, or all objects of the class share the entry address of the instance method. When all objects of the class are destroyed, the entry address disappears.
Class method:
When the byte code file of a class is loaded into memory, the entry address of the class method is assigned to completion, so the class method can be invoked not only by the object of the class, but also by the class name directly. The entry address of the class method disappears only when the program exits.

Rules to follow:

1. Cannot reference an instance variable in a class method
Instance variables are defined like instance methods, there are no static-decorated variables, instance variables are created the same as instance methods, and are completed when object creation of the class, so the instance variable cannot be referenced in the class method because the instance variable has not yet allocated the memory address.

2. The super and this keyword cannot be used in class methods
This is because super and this all point to the parent class and the objects of this class, and when invoked in the class method, the objects of those references may not have been created yet.
3. Instance methods cannot be invoked in class methods

Attention:

Instance methods can refer to class variables and instance variables

Instance methods can use the super and this keyword

the difference between a class method String StringBuffer and StringBuilder can be invoked in an instance method.

1. Variable and immutable
A string is provided in the string class that cannot be changed numerically.
The strings of StringBuffer and StringBuilder can be changed.
2. There is no way to rewrite the Equals and hashcode
String overrides the Equals and Hashcode methods

3 Execution speed of three:

Stringbuilder>stringbuffer>string

Reason:

String is a literal constant
StringBuilder and StringBuffer are string variables

As you can see from the name above, string is a "character Fu Yi constant", which is an immutable object. You may have such a question about the understanding of this sentence, such as this code:

1 String s = "ABCD";
2 s = s+1;
3 System.out.print (s);//RESULT:ABCD1

We obviously changed the variant s of the string, so why is that not changed? It's a deception, actually. The JVM parses this code by first creating the object s, giving an ABCD, and then creating a new object s to execute the second line of code, which means that we didn't change the object s before, so we said that the string type is an immutable object, because of this mechanism, When you manipulate strings with string, you are actually constantly creating new objects, and the original objects will become garbage collected by GC, so it is conceivable that the execution efficiency will be more than the end.
And StringBuffer and StringBuilder are not the same, they are string variables, are variable objects, and whenever we use them to manipulate strings, we actually operate on an object, so that we don't manipulate objects like string. Of course the speed is fast.
3. A special example:

1 String str = "This are only a" + "simple" + "test";
2 StringBuffer builder = new StringBuilder ("This is only 
a"). Append ("simple"). Append ("test");

You would be surprised to find that the Str object was generated at a rate that was simply too fast, and at this point the speed of the StringBuffer was not at all dominant. This is actually a trick of the JVM, actually:

String str = "This are only a" + "simple" + "test";

is actually:
   

String str = "This are simple test";

So it doesn't take too much time. But what you should be aware of here is that if your string is from another string object, the speed is not that fast, for example:

       String str2 = "This are only a";

String STR3 = "simple";

String STR4 = "Test";

String str1 = str2 +str3 + str4;

This time the JVM will behave in the same way as it did.

4.StringBuilder and StringBuffer

StringBuilder: Thread is not safe
StringBuffer: Thread-safe

When we are using a string buffer to be used by multiple threads, the JVM does not guarantee that the StringBuilder operation is safe, although he is the fastest, but can ensure that stringbuffer can be operated correctly. Of course, most of the time we are operating in a single thread, so in most cases it is recommended to use StringBuilder rather than stringbuffer, is the reason for speed.

Summary of the three-person use

1. If you want to manipulate a small amount of data with = String
2. Operation of a large amount of data under a single thread operation string buffer = StringBuilder
3. Manipulating large amounts of data under a multithreaded operation string buffer = The difference between StringBuffer overloads and overrides.

Overload:

Multiple methods in the same class have the same name, but these methods have different argument lists, the number and type of the level parameters cannot be exactly the same, and the return value type can be compatible or not.

Rule: You must have different argument lists that can have different return types, or you can have different access modifiers to throw different exceptions

Rewrite:

The polymorphism between the parent class and the subclass, redefining the function of the parent class. If you define a method in a subclass with the same name and parameters as its parent class, we say that the method is overridden.

Rules: Argument lists must be exactly the same as the parent class The returned type must be the same as the return value of the method of the parent class the limit of access modifiers must be greater than the exception that the parent class's modifier throws cannot be larger than the parent class's

The difference between overloading and overriding. Whether the overloaded method can be distinguished by the return type.

The overloads and overrides of a method are all ways to implement polymorphism, the difference being that the former realizes the compile-time polymorphism, while the latter realizes the Run-time polymorphism. Overloads occur in a class with a method with the same name if there are different argument lists (parameter types are different, the number of parameters is different or both are considered overloaded; overrides occur between the subclass and the parent class, overriding requires that the subclass be overridden by a method that has the same argument list as the parent's overridden method, and has a compatible return type. is better accessed than the parent-class overridden method, and cannot declare more exceptions than the parent's overridden method (the Richter substitution principle). Overloading has no special requirements for return types and cannot be differentiated based on the return type. the difference between forwarding and redirection. The difference between throw and throws.

User program custom exceptions and application-specific exceptions must be thrown with the help of throws and throw statements.
When you need to display an exception (typically a user-defined exception), you need to use the keyword throw to throw the exception, and the keyword throws is used to list the types of exceptions that a method might throw.
Example:

Class MyException extends exception{} public
class test{
//Tell the caller of the method this method may throw myexception exception public
void F ( int i) throws myexception{
if (i==1)
throws a custom exception when the condition is met
throw new MyException ();
    }

The other difference: throws usually appears in the function header, and throw usually appears in the function body throws represents a possibility of an exception that does not necessarily occur, and throw throws an exception, that is, if you execute throw, you must throw an exception Both are negative ways to handle the exception (this kind of negativity is not to say that a bad way), just throw or may throw an exception, but there will be no function to handle the exception, the real handling of the exception is the function of the upper call processing. What is the difference between final, finally, and Finalize in Java. final is used to declare properties, methods, and classes, representing properties that are not variable, methods cannot be overridden, and classes cannot be inherited
Final Modifier property: The final modified variable is immutable, because there are two kinds of meaning: one is the reference immutable, the other is the object immutable, final refers to what it is.
An example is provided:

public class  test{public
    static void Main (string[] args) {
        final stringbuffer s=new stringbuffer ("Hello");
        S.append ("World");
        System.out.println (s);
    }

Run the result:
Hello World
public class  test{public
    static void Main (string[] args) {
        final stringbuffer s=new stringbuffer ("Hello" );
       S=new stringbuffer ("Hello World");
    }

Compile-time error

As you can see from the example above, final refers to the immutability of references, that is, she can only point to the object at the beginning, but not the content of the object to which it is directed, so the variable being modified by final must be initialized.
Several ways to initialize:


Initializing a final member variable at the time of definition can be initialized in the initialization block, but cannot be initialized in a static initialization block

3. Initializes in the constructor of the class, but the static final member variable cannot be initialized in the constructor method.

Final method: When a method is declared final, the method does not allow any subclasses to override the method, but subclasses can still use this method.
Final parameter: Used to indicate that this parameter is not allowed to be modified inside the method
Fianl class: When a class is final by life, this class cannot be inherited, and all methods cannot be overridden, but this is not to say that the final class's member variables are immutable, and the final modification must be added to the member variables in order to be immutable.
Note: A class cannot be decorated as abstract and modified to final. tomact Optimization Experience to remove the monitoring of web.xml, the JSP in advance to edit the servlet in the case of surplus physical memory, increase tomact use of the JVM's memory utilization cache and compression using the cluster The difference between the Get method of the HTTP request and the Post method. the parameters of the get are passed through the URL, and the post parameter passes the length of the parameters passed in the URL by the request body, and the post is not more secure than the post because it is directly exposed to the URL. A GET request can only be URL-coded, and the post may be able to do multiple encodings of getting requests that allow the browser to actively cache, while the post no get request parameter will be fully retained in the browsing history, and the parameters in the post will not be retained. Get generates a TCP packet; Post generates two TCP packets. the difference between forward and redirect

Forward is the server request resources, the server directly access the URL of the destination address, the URL of the response content read over, and then send the content to the browser, the browser does not know where the server sent the content, so its address bar or the original address.
Redirect is the server based on logic, send a status code, tell the browser to retrieve the request that address, generally speaking, the browser will use the parameters just requested again, so the session and request parameters can be obtained.

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.