java interview questions (continually updated)

1 "==" and equals method is what is the difference?

== operator designed to compare two values of the variables (memory value) are equal, i.e. for comparing the variables corresponding to the values stored in the memory are the same, to compare two basic types of data, or two references variables are equal, with only == operator.

If the variable points to a data object type, then this time involves two memory occupied by the object itself a memory (heap memory), a memory occupancy variables, e.g. Objet obj = new Object (); obj is a variable memory, new Object () is a second memory, at this time, the value stored in memory corresponding to the variable obj is the object occupies the first address of that memory. For variable type pointing object, if you want to compare two variables refer to the same object, i.e., the value depends on two variables corresponding to the memory are equal, this time need to be compared with == operator.

 

equals method is used to compare two independent content object are the same, like is the same ratio to compare the appearance of two people, it compares two objects are independent.

We usually use the equals () method which was all Object rewrite equals () method comes, and inside the Object class equals () itself is to use the "==" refers to the address comparison:

 

For example, the following code:

String a=new String("foo");

String b=new String("foo");

Two statements create two new objects, then a, b are two variables point to one object, which is two different objects, they are different from the first address, i.e., values of a and b stored is not the same, therefore, the expression a == b will return false, and the content of these two objects are the same, therefore, the expression a.equals (b) returns true. But the biggest reason is because of the String equals method has been rewritten.

Let's look at the following code:

 

2, the difference between static variables and instance variables?

Differences in syntax definition: former static variable to add static keyword, but not before adding instance variables.

The program runs difference:

Instance variables belonging to the properties of an object , you must create an instance of an object, in which instance variables are allocated space in order to use this instance variable.

Static variables do not belong to an instance of an object, but belong to the class, it is also known as class variables , as long as the program is loaded bytecode class, without creating any instances of objects, static variables will be assigned space , static variables can be used.

In short, after the instance variables must create an object can be used by this object, you can use static variables direct the class name to reference .

3, the difference between overload and override the

Overloads (overload), represents the same method name Method plurality of the same class, these methods parameter list are different (the number of arguments, the type or sequentially).
Rewriting (override), a subclass rewrite a parent class method, including the same method name, parameters and return types.
Subclass access modifier range greater than the range equal to the parent class, subclass only exception thrown less than the parent.
Declared final method can not be overridden. Declared as static methods can not be rewritten, but can be declared again.
Sub-classes and superclasses in the same package, in addition to private subclass can override all final method. Subclass and parent not in the same package, the only way to subclass the public and protected override the parent class declaration.

 



Guess you like

Origin www.cnblogs.com/reaper-kd/p/11236504.html