This article is the first in a series on good interviews. Last year, I interviewed a number of candidates as an interviewer, and buried a number of candidates at the same time. Today, I mainly want to share with you the pit I dug in the Java foundation when I was an interviewer. I hope you can avoid these pits in the New Year and don’t be buried again.

What have I buried in the base data types

So, how many basic data types does Java have? How old?

Java has eight basic data types, which are

  • Byte: contains 1 byte and 8 bits
  • Char, takes up 2 bytes of 16 bits
  • Short, occupying 2 bytes and 16 bits
  • Int, occupying 4 bytes of 32 bits
  • Float, occupying 4 bytes of 32 bits
  • Long, occupying 8 bytes 64 bits
  • Double, 8 bytes 64-bit
  • Boolean, occupy a byte 8 bits

Wrong. Default Boolean to one byte is pretty common for all beginners.

Note: The size of the Boolean is unknown, although we see that Boolean can only be used in the case of true and false and can be stored in 1bit, we don’t actually specify 1bit because there is no such thing as a Boolean type for virtual machines. In the Java Virtual Machine Specification, there are two definitions: 4 bytes and 1 byte for Boolean arrays, but depending on whether the virtual machine implementation complies with the specification, either 1 byte or 4 bytes is possible. It’s really a game between efficiency and storage, and both are very important.

So what are these integers?

These are wrapper types, each of which has a corresponding wrapper type, and assignment between them is done using automatic packing and unpacking. Such as:

Integer number1 = 2;     // Integer. ValueOf (2) is called
int number2 = number1;         Number1.intvalue () number1.intValue()
Copy the code

What’s the difference between new Integer(1024) and integer.valueof (1024)?

First, new Integer(1024) creates an object each time, while integer.valueof (1024) uses objects from the buffer pool, and multiple calls get a reference to the same object.

Here’s an example:

image-20210109100030722

No, that’s where I buried it. I’ve used it on multiple candidates.

image-20210109174541713

Note: Integer.valueof (1024) and integer.valueof (1024) are not true, but false. ValueOf The value taken from the buffer pool is size limited, not any number

We can look at the source code for valueOf(), which is simple enough to determine whether the value is in the buffer pool, and if so, return the contents of the buffer pool directly.

image-20210109100401281

Currently, my JDK version is 8. In JDk8, the default size of the Integer buffer pool is -128 to 127.

image-20210109100503563

As an interviewer, I love to dig up the details that people reveal when they answer questions. You just talked about automatic packing and unpacking. What do you understand?

The compiler calls the valueOf() method during autoboxing, so multiple Integer instances with the same value and values in the cache pool that are created using autoboxing will refer to the same object, so true will be returned for comparison.

image-20210109101748474

Dig deeper to see how deep the candidate’s knowledge is. So what do you know about buffer pools?

Currently, the buffer pools corresponding to basic types are as follows:

  • Boolean Buffer pool, true and false
  • Byte buffer pool
  • Short buffer pool
  • Int buffer pool
  • Char buffer pool

So when we use the wrapper type corresponding to these basic types, if the value is within the buffer pool range, then we can directly use the object in the buffer pool.

Keep digging, see if he’s seen the buffer pool source code. Are the upper and lower limits of these buffer pools the same? Or is it configurable?

They’re basically immutable, but in JDK 1.8, IntegerCache, the buffer pool for Integer, has a lower bound of -128 and an upper bound of 127 by default, but the upper bound is tunable. We can look at the source code

image-20210109102339939

When the JVM starts, we can through – XX: AutoBoxCacheMax = < size > to specify the size of the buffer pool, at the time of the JVM initialization, this setting will set a called Java. Lang. IntegerCache. High system properties, The IntegerCache initialization then reads the system properties to determine the upper bound.

Conclusion: the pit above have the size of the Boolean respectively, the size of the buffer pool, automatic unpacking and packing, the size of the buffer pool is variable, basically these a few pits can pit down sixty percent of the candidates, the second is as a interviewer, I like to dig the details of the candidate answer questions, dig, after all, can see you really have a material!!!!!!

See what I’m burying in the String

You’ve just said the basic types, so tell me what you know about strings

A String is declared final, so it cannot be inherited. In Java 8, strings store data internally using a char array and are declared final, which means that the value array cannot be initialized to reference another array, and there is no internal method to change the value array. So String is guaranteed to be immutable.

Dig deeper and talk about the benefits of immutability?

The answer to this question is more general, can be said to be more points, can be roughly divided into:

  • First, immutable nature means safe. When String is referenced as a parameter, immutable guarantees that the parameter is immutable.

  • Secondly, the hash value can be cached. In fact, we often use it as a map key in development. The immutable feature makes the hash value immutable, so only one calculation is needed.

  • Finally, a String Pool is required. If a String object has already been created, a reference will be taken from the String Pool. Naturally, a String Pool can only be used if String is immutable. If it is mutable, then the String Pool cannot be designed.

Have you ever used StringBuffer and StringBuilder? What’s the difference between String, StringBuffer and StringBuilder?

First of all, they’re all final, so they’re not inheritable, but in terms of variability, String, as we just said, is immutable, and StringBuffer and StringBuilder are mutable, because of the internal structure, StringBuffer and StringBuilder internal arrays of data are not final decorated.

Secondly, in terms of thread safety

  • String is immutable and thread-safe

  • StringBuilder is not thread safe because it does not use any internal security handling

  • Stringbuffers are thread-safe and synchronized internally

Let’s dig into the details. You just said String Pool. Tell me what you understand

The String Pool is often referred to as the String constant Pool. It holds all String literals and is determined at compile time.

The String Pool is determined at compile time. Is it immutable?

Yes.

A mistake that all beginners make is to ignore the String. Intern, which I often use to distinguish between beginner and intermediate candidates!!

image-20210109174601615

We can use the String’s intern() method to add strings to the String Pool at run time.

We can see that

image-20210109111528457

This is a local method, so we can’t see the source code, but we can see the comments

When a String calls the intern() method, a reference to the String in the String Pool is returned if there is already a String in the String Pool that is equal to the String (determined using the equals() method). Otherwise, a new String is added to the String Pool and a reference to the new String is returned.

Here’s a demo to illustrate the process

image-20210109111744460

My s1 and S2 above create two different strings using new String(), while S3 and S4 get the same String reference through the s1.intern() and s2.intern() methods. The first intern() first puts “talk programming” into the String Pool and returns the String reference, while the second intern() reads directly from the String Pool, so s3 and S4 refer to the same String.

Continue to dig the hole, ready to bury the candidate just said new String(” talk about programming “)! = new String(” talk about programming “), is “talk about programming” the same as “talk about programming”? What’s the process?

It’s the same thing, as we can see

image-20210109112317572

The process is this: by creating a String in this literal form, the JVM automatically places the String in the String Pool, so the two are equal.

Going into the details, this is a tricky question: New String(” programming “) What does the JVM do?

First of all, there are two String objects created using this method, provided that the String Pool does not already have a String object called “Geek program”, so at compile time a String object will be created in the String Pool that points to the geek Program literal.

Then we create a String object in the heap using the new method, which we can see in conjunction with the String constructor

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}
Copy the code

As you can see, when you take a String object as a constructor argument to another String object, the JVM takes that String object from the String Pool and passes it as an argument to the String constructor, giving the new object an array of values and a hash value.

Bottom line: String is often used in everyday development, but a qualified candidate should understand the difference between String, StringBuilder, and StringBuffer, and should know as much about the String Pool as possible to avoid being buried in the hole I dug above.

See what kind of hole I’ve buried in the computation

Do you pass method parameters by reference in Java? Or is it value passing?

If the parameter is of a basic type, it is passed by value; if the parameter is of a reference type, it is passed by reference.

No, this is where a lot of beginners get it wrong, and this is where I dig and bury people every day

Java parameters are all passed to methods as values, not references. If the argument is a primitive type, a copy of the literal value of the primitive type is passed. If the parameter is of a reference type, the value passed is a copy of the address value of the object referenced by the parameter in the heap.

Let’s see, float f = 2.2. Is that okay?

It looks like it’s ok, but it’s actually a problem, and this is something that I don’t normally use in an interview, but in a written test.

2.2 this literal is of type double, so you can’t directly assign 2.2 to float because it’s a downward transition, and remember Java can’t implicitly perform downward transitions because that would reduce accuracy.

The normal way to write it is

float f = 2.2 f;
Copy the code

Keep digging, so float f = 2.2f; F + = 2.2; You can?

This, again, is a question I would put in for postgraduate candidates, but it works because the JVM performs implicit type conversions using the += or ++ operators.

S1 + 1 = s1 + 1 = s1 + 1

f = (float) (f + 2.2);
Copy the code

Conclusion: in the aspect of operation of buried pit is the foundation, is usually placed in the interview questions, and in fact use idea development can, in fact, in the development stage will be an error, but this does not mean that the idea can be detected, you can’t understand, want to come to our company interview, in particular, this means that your professional ability to pass.

See what I’ve buried in the modifiers

Talk about understanding the modifier final

The first is the use of final on variables. This means that the data is declared to be constant, either at compile time or at run time, and cannot be changed once initialized. This can be divided into:

  • For basic types, final leaves the value unchanged;
  • In the case of reference types, final renders the reference immutable, so that no other object can be referenced.

If final is used on a method, the declared method cannot be overridden by a subclass.

If final is used on a class, the declared method is not allowed to be inherited.

Final int b = 1; final int b = 1; B. So if this is an example like this, can I change the x of object A

image-20210109123855200

Fianl applies to the reference to object A, not to the data member X of object A, so it can be changed.

Just now you said that declared methods cannot be overridden by subclasses, so the question is, why does that work

image-20210109124438515

Normally a candidate would flounder here.

image-20210109174614608

In fact, he answered the right theory, but he didn’t actually try to write it the way I did. In fact, when a private method is implicitly specified as final, if a method defined ina subclass has the same signature as a private method in the base class, the subclass method does not override the base class method, but instead defines a new method in the subclass.

Let’s see what you know about static

First is to use on the variable, the variable we usually call it a static variable, can also be called class change, that is to say, the variable belongs to the class, the class all instance Shared static variables, generally we are directly by the name of the class to access it, need to pay attention to the thing, there is only a static variables in memory.

If it is used on a method, it is called a static method. This static method exists when the class is loaded, and it does not depend on any instance. Therefore, a static method must have an implementation, which means it cannot be abstract.

Can I use the this or super keyword in a static method to start digging

No, you can only access static fields and static methods of the class you belong to. You can’t have this or super keyword in a method.

There was an intern who came and made this mistake while writing code. Look at the results below

image-20210109125553717

The correct answer is

image-20210109125704464

Just remember that static blocks take precedence over normal blocks, and normal blocks take precedence over constructors.

So what if there was inheritance? For example, in this case, talk about the order in which they execute

image-20210109130202218

Most junior candidates stumble over this question. The correct answer should be:

image-20210109130258183

That is, in the case of inheritance, the initialization order is:

  • Parent class (static variable, static statement block)
  • Subclasses (static variables, static blocks)
  • Parent class (instance variable, normal statement block)
  • Superclass (constructor)
  • Subclasses (instance variables, plain statement blocks)
  • Subclasses (constructors)

Conclusion: although looks modifier is a small thing, but if the actual development of the mining pit, it will cause bigger risks, such as the execution order made a mistake, but also can understand the situation through the candidates of the modifier, you can see the actual programming level, this also is we, as the interviewer wants to know urgently.

The last

The following series of articles:

  • Talk about the hole I dug in Object
  • Tell me about the hole I’m digging in the assembly
  • Talk about the hole I dug in the Netty series…

Hello, I’m a Java interviewer talking about programming, and I’m going to tell you from an interviewer’s point of view about the holes I dug during the interview.

Good interview series will be divided into many articles, basically read the series of articles, Java foundation this can meet god kill god, after all come and go on these, the follow-up wonderful please wait!

The public number: talk about programming The original link: https://mp.weixin.qq.com/s/F73r_f5YcBOayPdsin4gBg

Thank you for your support 👍👍👍!