No water today, dry goods directly! I hope the following will resonate with you.

1. Lengths vary

Have you ever been asked in an interview whether an array has a length() method? Does a string have a length() method? Does the set have a length() method?

In the face of this problem, we have to ridicule the way to obtain length in Java, the design is really a bit chaotic, for just the beginning of the program ape, it is absolutely a face of meng force.

String[] array = {“abc”, “def”};

String str = “abcedf”;

List<String> list = new ArrayList<String>();

list.add(“abc”);

list.add(“def”);

System.out.println(” array length: “+ array.length);

System.out.println(” string length: “+ str.length());

System.out.println(” set length: “+ list.size());

Let’s make it official. I hope it sticks in your mind. Array length with the length attribute; Length () is used to calculate the length of a string. The length of the set is determined by the size() method.

2. String interception has meaning

For programmers, programming specifications can form good programming habits, improve code quality and reduce communication costs. According to the programming specification of Ali Java Development manual, [mandatory] method names, parameter names, member variables, and local variables all use the lowerCamelCase style and must follow the camel form.

I have to mention the substring method in String, do you often write “substring” as “substring”? This is not the point of the joke. I would like to share the following code snippet.

public class StringInterview {

   public static void main(String[] args) {

String str = “…… abcdefgh…….” ;

String subStr = STR. The substring (1, 3);

       str = null;

       System.out.println(subStr);

   }

}

If this program were running in Java 1.6, the GC would not be able to reclaim the large char array because it is still being referenced inside the subStr string, although the STR reference is forced to be null. Although subStr only captures a small portion of this large array. When STR is a very large string, this waste is obvious and can even lead to memory leaks.

Take a deep dive into Java 1.6’s Substring design.

public String substring(int beginIndex, int endIndex) {

  if (beginIndex < 0) {

. . .

  }

  if (endIndex > count) {

. . .

  }

  if (beginIndex > endIndex) {

. . .

  }

  return ((beginIndex == 0) && (endIndex == count)) ? this :

   new String(offset + beginIndex, endIndex – beginIndex, value);

}

Constructor of the above method call

String(int offset, int count, char value[]) {

  this.value = value;

  this.offset = offset;

  this.count = count;

}

You should be able to see the light at this point. When we call substring of STR to get subStr, all we’re doing is adjusting the offset and count of subStr, using the value array that precedes STR. A new content character array specific to subStr is not recreated. If the lifetime of subStr is longer than STR or STR is manually set to null, when garbage collection is done and STR is collected but subStr is not, the memory footprint remains because subStr holds a reference to the STR character array.

For the record, this problem occurred in Java 1.6 and has been fixed in Java 1.7. Just because it’s been fixed doesn’t mean we don’t need to know, but if you’re looking for a job, knowing a little bit might help.

3. An if statement causes a complaint

Let’s start with a snippet of the Java LinkedList class.

public E getFirst() {

   final Node<E> f = first;

  if (f == null)

       throw new NoSuchElementException();

   return f.item;

}

There is only one statement after an if statement in the JDK, and this is how most of it is implemented. In principle, an if statement can be omitted if it is followed by only one sentence. But in our actual development, some phenomena will make you wonder, do not believe you take a look at the code snippet below.

A fragment:

 if (f == null)

// Throw an exception, or add a print statement. Add this comment and the logic changes

   throw new NoSuchElementException();

Section 2:

public class Interview {

   public static void main (String[] args) {

       Class c = Interview.class;

       try {

           Object o = c.newInstance();

           if (o instanceof Interview)

Interview tt = (Interview) o; // Why is this error reported? Please explain why

       } catch (Exception e) {

           e.printStackTrace();

       }

   }

}

Formally popular popularization, seemingly a simple coding specification, hidden behind the number of pits ah, so in order to good programming habits, it is recommended to add curly braces, good coding habits is really important.

4. Timing is a problem

Tiago Fernandez did a poll for the worst Java API, and the second worst was the Date API(Date and Calender). The following snippet calculates the number of days between two dates.

public static void main(String[] args) {

   Calendar begin = Calendar.getInstance();

   begin.set(1990, Calendar.JUNE, 17);

   Calendar end = Calendar.getInstance();

   System.out.println(alculatedDays(begin, end));

System.out.println(alculatedDays(begin, end)); // Why is 0 displayed?

}

public static long alculatedDays(Calendar begin, Calendar end) {

   long days = 0;

   while (begin.before(end)) {

       begin.add(Calendar.DAY_OF_MONTH, 1);

       days++;

   }

   return days;

}

The alculatedDays method, if you compute two Date instances in a row, will get 0 for the second time. Since the Calendar state is mutable, it is best to copy a new Calendar in case of double computation

public static long alculatedDays(Calendar begin, Calendar end) {

Calendar calendar = (Calendar) begin.clone(); / / copy

   long days = 0;

   while (calendar.before(end)) {

       calendar.add(Calendar.DAY_OF_MONTH, 1);

       days++;

   }

   return days;

}

However, things are moving forward, because the old date API has been criticized, so the date changes in JDK 1.8 are particularly big, basically introducing a new and easy to use API, you can try it out at some time.

Well, the truth in the joke, that’s all for today. Hope you can get a little bit of resonance, if you are more interested, share with friends around it.