Java Programmer face Question collection (2)

Source: Internet
Author: User
Tags comparable string back

1 String = different from new

Assigning a value with "=" does not necessarily create a new string each time, but instead finds the string from the string instance pool . When you assign a value using "new", a new string is created each time.

2 string and StringBuffer

The string class is immutable and cannot be changed once it has been initialized. Whereas the StringBuffer class is a mutable class, the string value can be changed. Often in practical applications to see the exhaustion of such code: String s=new ("Hello"), s+= "World", the two lines of code first create a character Hello, and then append the world to the Hello end and re-assigned to the variable s. Then, this is actually the process: first create a StringBuffer object, then append the string with the Append method of the StringBuffer class, and finally call the ToString method on the StringBuffer object to synthesize the string back. As you can see, using the + sign to connect strings is essentially a variable StringBuffer class that is affected by the change in performance efficiency, so it is recommended that you consider using the StringBuffer class directly when you need to append strings. Reference answer:The String class is an immutable class, where the string value cannot be changed once it is initialized. StringBuffer is a mutable string class, string-like buffer that can modify the value of a string .

3 The difference between StringBuffer and Stringbiulder

StringBuffer is a thread-safe string buffer, and StringBuilder is not thread-safe .

Back to top 4 type of wrapper?

The wrapper class includes byte, short, Integer, long, float, Double, Character, Boolean and other classes, mainly used for byte, short , Integer, Long, float , double, character, and Boolean are eight basic data types that are packaged to be referred to as reference types.

5 java parameter passing

Parsing: When a value is passed, an unavoidable action is written when the application is applied. For example, a method declaration form is public void f (int x) {}, and the write method must be passed an actual argument of type int, such as f (10). Also such as public void G (Employee). When you use this method, you must pass an actual argument for the Employee type for it, such as G (new Employee ()). Therefore, it is important for the novice programmer to understand the value passing rules of the Java language. The Java language is a value passed by this type, such as f (10), which simply copies 10 to form parameter x, which is a copy of the value. Referring to a class's pass-through reference, that is, a virtual address, such as G (new Employee ()) is the actual parameter of the virtual address to the formal parameter e, that is, the actual parameter and formal parameters of the same virtual address, physically the same object. Reference answer: The base data type passes the value, and the reference type passes the reference , that is, the virtual address.

6 static modifier properties or methods

When static modifies a property or method, the properties and methods do not belong to a particular object, but are all shared, or static members are not dependent on an object and are initialized when the class is loaded . Static decorated properties or methods that can be called directly using the class name without first instantiating the object and then calling it.

7 = = function of the operator

Parsing: In practical programming, = = is a very common operator. Many novice programmers will use such a code if (s== "Save" 0{}, the result will be found that even when the value of the string s is save, the IF condition is still not executed. is = = When comparing reference types, the virtual address of two objects is compared, not the content. To compare the contents of two objects, you often need to use the equal method, such as if (S.equals ("Save")) {}. = = Whether the binary of the ratio is equal when comparing the base type, for example (x==0.5) {}. It is worth noting that a null value is compared with a null pointer, often with = =, such as if (s==null| | S.equals ("")), indicating if the string is a null pointer or an empty string. Reference Answer: = = can be used to compare basic types or reference types. When comparing a base type, = = is used to compare binary values and to compare the virtual addresses of objects when comparing reference types.

8 && different from &

Parsing: The logic of "and" or "or" often needs to be used in practical programming. where && and & have a certain difference && possible road problems, such as if (S!=null&&s.length () ==6), if the value is null, then the value of the first expression is false, The return value is definitely false, and the value of the second expression is not evaluated, which is a short-circuit. However, using &, such as if (S!=null&s.length () ==6), assuming that the value of S is null, the return value is definitely false, but the value of the second expression is judged, a null pointer exception will occur. In practice, && operators are often used.
Answer:&& A short-circuit problem occurs, and when the value of the first expression is false, the result is returned directly to false without judging the value of the second expression. & does not have a short-circuit problem, even if the value of the first expression is false, the value of the second expression is judged .

9 dependencies and associated relationships

Parsing: Association relationships and dependencies are two very common relationships, and the difference between them is obvious, and programmers understand the difference between them to understand the object-oriented thinking more deeply.
Reference answer: A dependency is a transient relationship, a dependent B, refers to a behavior of a parameter is the type of B, that is, a to achieve this behavior, must rely on the example of B, a association B, is a long-term relationship . b as an attribute of a, as long as an object of a is instantiated, the object of the A is instantiated a B object, as its properties, can be used in a where B is required to use B.

the inheritance of Java

Parsing: Inheritance is a major feature of object-oriented languages, and the primary role is to reuse, and subclasses can reuse the properties and methods of the parent class by inheriting the parent class. Note that there are two strategies that can make the implementation reusable, one is an association, one is inheritance, and in actual programming, the association uses more. Because the child class inherits the parent class, the details in the parent class are exposed to the child class. Novice programmers must not arbitrarily use inheritance, avoid misuse of inheritance. Reference answer: inheritance is primarily to be able to reuse members in the parent class. Inheritance in the Java language is single-inheritance, meaning that a class can inherit at most one parent class .

the role of one super

Parsing: When using some ides to generate code, you will often see the Super keyword in the construction method. As the name implies, super is a key word related to the parent class, and familiarity with super usage is necessary for mastering inheritance.

Reference answer: such as super (): Represents the constructor method that invokes each reference in the parent class : Another example is super (10), which represents a constructor method that invokes an integer parameter in the parent class. The second usage is to call the members of the parent class in the subclass, such as SUPER.F (), which means to call the F () method in the parent class.

the role of final

The final modifier is a very versatile modifier that can be used to modify classes, methods, and properties.

Reference answer: The final class is a class that cannot be inherited, called the ultimate class, such as the String class is the final class and cannot have subclasses. The final modification method is the ultimate method, which cannot be overridden by the quilt class, but can be used by the quilt class. the final class cannot be inherited, the final method cannot be overwritten, and the final property cannot be changed .

the role of the object class, there are any methods.

Parsing: The object class is a very important class, which is the parent class of all classes, including arrays, that is, to a Java class, whether defined in the API or a custom class, directly or indirectly inheriting the object class. So, if there is a method F (Object o), then it can be passed to the object of any class of this method, including%%%%. Because all objects can be said to be of type object.
Reference answer: Theobject class is a direct or indirect parent class that has many classes .

Common methods:toString () equals Finalize ()

equals and Hashcode methods of the object class

Parsing: In the object class, the Equals method compares the reference value of an object, that is, only two references that are physically the same object, and returns true using the Equals method. The Hashcode method returns the hexadecimal value of an object's intrinsic address. Because the object class is the parent class for all classes, both methods are available in any class and can be overridden. In particular, the Equals and Hashcode methods of the collection element class when manipulating the Set,map object are necessary because the set and map determine whether the element is heavy%%% depends on both methods. The general principle is that if there are X and Y two objects, the call to X.equals (y) is the same, then the return value of the Hashcode method should be the same, and if X.equals (y) returns false, then the return value of the call Hashcode method may or may not be the same. It is important to note that as long as you override the Equals method, a certain%%% overrides the Hashcode method. In practical work, many IDE environments support the overriding Equals and Hashcode methods.

Reference answer: The Equals method in the Oject class is used to compare two reference values, and hashcode is used to return the hexadecimal value of the inside address of a reference . In set and map collections, it is often necessary to use these two methods when judging whether or not two elements are duplicated. These two methods are often covered by the quilt class, overriding the rule is: if there are X and Y two objects, call X.equals (y) return true, then call the Hashcode method return value is the same, if X.equals (y) return false, The value returned by the call to the Hashcode method may or may not be the same.

15 interfaces and abstract classes

Analysis: The concept of proximity to Europe is a design-level concept, and novice programmers tend not to define interfaces themselves, but interfaces are used and it is necessary to understand the concepts of interfaces. The interface defines the specification of the implementation class, that is, the part of what does, and all implementation classes must be implemented in accordance with this specification.
Reference answer: The interface is characterized by the inability to define variables (static constants can be defined statically final decoration, Bo friends said JDK8 has been fixed, this did not try, expecting reference), and all methods are abstract methods . abstract classes can have variables and do not enforce the need for abstract methods .

inheritance of Class 16 and implementation of interfaces

Parsing: Class Inheriting the parent class implements the interface with the class, which is essentially the same. is to either extend or rewrite the parent class or interface as a template on this template. In practical programming, programmers often need to inherit the parent class or implement an interface.
Reference to the answer: the class inherits the parent class is only a single inheritance, that is, a subclass has at most one parent class , and the class implementation interface can be implemented more, that is, a subclass can implement multiple interfaces at the same time , and overwrite all the abstract methods in all interfaces.

the role of the comparable interface

Parsing: The comparable interface is an interface that is often used in practical programming, which defines the Comareto (object o) method, which defines the object's comparison logic. This interface is often enforced in other APIs, such as the sort (object[]) method of the arrays class, forcing the array element to implement the comparable interface, and another interface similar to this interface is comparator.
Reference answer: The comparable interface defines the CompareTo (object o) method, which can be used to implement object comparison logic , which is often enforced in other APIs to standardize the comparison logic of objects

Java Programmer face Question collection (2)

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.