Java written interview questions (ii)

Source: Internet
Author: User

a daily sentence: Life is full of expectations, the dream of connecting the future!

Java fresh or self-taught beginner's written interview topic summary, I hope that everyone has help, the topic from their own learning summary and teacher explanation, network resources, books and materials. Knock out everyone to see, every time to update the topic, I hope you can find a good job.

Part I: The basic part

The underlying sections include: basic syntax, similar syntax, internal classes, inheritance, exceptions, threads, collections, IO, virtual machines, and others.

11. What is the difference between "= =" and Equals method?
The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.

If the data that a variable points to is object type, then this time involves two memory, the object itself occupies a block of memory (heap memory), the variable also occupies a piece of memory, such as Object obj = new object (); variable obj is a piece of memory, new object () is another piece of memory, at which point the value stored in the memory of the variable obj is the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, whether the values in memory for the two variables are equal, then you need to compare them with the = = operator.

The Equals method is used to compare whether the contents of two independent objects are the same, as compared to two people whose looks are the same, and the two objects he compares are independent. For example, for the following code:

String a = new String("tumbler");String b = new String("tumbler");

Two new statements create two objects, and then with a, B, the two variables point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and a are not the same, so the expression a==b will return false, and the contents of the two objects are the same, so , the expression a.equals (b) returns True.

In real-world development, we often want to compare whether the string content passed in is equal, for example, string input = ...; Input.equals ("Tumbler"); Many people do not pay attention to write = = To compare, this is wrong, the string comparison is basically the Equals method.

If a class does not define its own Equals method, it inherits the Equals method of the object class, and the Equals method of the object class is implemented as follows:

boolean equals(Object o){    return this == o;}

This means that if a class does not have its own definition of the Equals method, its default Equals method is to use the = = operator, which is to compare whether the object pointed to by two variables is the same object, when using equals and using = = will get the same result, if you compare two separate objects, will always return false. If the class you are writing wants to be able to compare the contents of the two instance objects created by the class, then you must override the Equals method.

# # 12. The difference between a static variable and an instance variable
Syntax definition differences: Static variables are decorated with the static keyword, while instance variables are not.

Program Runtime differences: Instance variables belong to an object's properties, and you must create an instance object where the instance variable is allocated space before the instance variable can be used. Static variables do not belong to an object, but belong to a class, so also known as a class variable, as long as the program loads the class's bytecode, without wearing any instance object, the static variable will be allocated space, it can be used. In summary, instance variables must be created before they can be used by this object, while static variables can be used directly using the class name.

For example, for the following code, no matter how many instance objects are created, a staticval variable is always assigned, and each instance object is created, the staticval will be +1, but each instance object created will be assigned a instanceval. That is, multiple instanceval may be assigned, and each instanceval value is only added once.

public class VariantTest{    public static int staticVal = 0;    public int instanceVal = 0;    public VariantTest(){        staticVal++;        instanceVal++;        System.out.println("staticVal="+staticVal+",instanceVal="+instanceVal);    }}

13. Is it possible to make a call to a non-static method from within a static method?
No. Because the non-static method is to be used in conjunction with a class object, you must create an object before you can make a call to the method on that object, and the static method call does not need to create the object, which can be called directly. That is, when a static method is called, which object does the non-static method relate to? Logic cannot be established, so the answer is no.

The difference between an integer and an int
int is one of the 8 raw data types provided by Java. Java provides a wrapper class for each primitive type, and integer is the encapsulated class of int. The default value of int is 0, and the default value of integer is null, that is, the integer can distinguish between unassigned and a value of 0, and int cannot express an unassigned condition, for example, to express the difference between not taking an exam and a test score of 0, you can only use integer. In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the int default value is 0, so when the El expression is displayed in the text box, the result is 0, so that int is not suitable as the type of the form data for the Web tier.

In Hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined as an int type, It is also necessary to set its Unsaved-value property value to 0 in the HBM mapping file.

In addition, the integer provides multiple integer-related operations, such as converting a string to an integer, which also defines the maximum and minimum operations that represent integers.

Math.Round (11.5) equals? Math.Round (-11.5) equals?
Math provides three methods for rounding: Ceil, floor, and round, which are similar in meaning to their English name, such as ceil (ceiling), which means rounding up, so that the Math.ceil (11.3) result is 12, Math.ceil (-11.3) results for -11;floor (floor), Math.floor (11.6) =11,math.floor ( -11.6) =-12;round for rounding, algorithm for Math.floor (x+0.5), The original number is added 0.5 and then rounded down, math.round (11.5) =12,math.round (-11.5) =-11.

16. What is wrong with the following code?

1.if(username.equals("tumbler"){}2.int x = 1;  return x = 1?true:false

The first one lacks a closing parenthesis; the second one is correct;

17. Tell the difference between scope public, private, protected, and no write.
Scope Current class same package descendant class other package

Package Package
Scope Current Class Samedescendant class Other
Public
Protected X
Friendly X X
Private X X X

The difference between overload and override, can the overload method change the type of the return value?
Overload is overloaded, override is the meaning of overriding overrides.

Overloaded overload means that there can be more than one method with the same name in the same class, but the parameter lists of these methods vary (the number of arguments or the type of the parameter).

overriding override means that a method in a subclass can be exactly the same as the method name and argument in the parent class, and when called by the instance object created by the subclass, the method defined in the subclass is called, which is equivalent to overwriting the exact same method defined in the parent class. This is also a manifestation of the polymorphism of object-oriented programming. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception from the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The subclass method can only be more accessible than the parent class and cannot be smaller. If the method of the parent class is private, then the subclass cannot be overwritten, which is equivalent to adding a new method to the subclass.

19. A house has a chair, a chair with legs and back, what is the relationship between the House and the Chair, and what is the relationship between the Chair and the leg and back?
If a house has multiple chairs, which is an aggregation relationship, it is an association, and of course, aggregation is a special kind of association. A chair is a combination of a leg and a back.

20. Tell the difference between is a and has a.
is a represents a relationship that belongs to. For example, cats belong to an animal (inheritance relationship).

Has a represents a combination, including a relationship. For example, a cat contains legs, first-class components.

Java written interview questions (ii)

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.