10 most popular Java problems on Stackoverflow (1)

Source: Internet
Author: User

10 most popular Java problems on Stackoverflow (1)

1. Why do we get a strange result when we subtract the time of the two October 1927 s?

3623 likes)

If the following program is executed, the program parses and compares two date strings separated by 1 second:

 
 
  1. public static void main(String[] args) throws ParseException { 
  2.  
  3.     SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  4.  
  5.     String str3 = "1927-12-31 23:54:07";  
  6.  
  7.     String str4 = "1927-12-31 23:54:08";  
  8.  
  9.     Date sDt3 = sf.parse(str3);  
  10.  
  11.     Date sDt4 = sf.parse(str4);  
  12.  
  13.     long ld3 = sDt3.getTime() /1000;  
  14.  
  15.     long ld4 = sDt4.getTime() /1000; 
  16.  
  17.     System.out.println(ld4-ld3); 
  18.  

The output is:

 
 
  1. 353 

Why is the ld4-ld3 not 1 because I want the two time difference to be one second), but 353?

If you add a date string to each second:

 
 
  1. String str3 = "1927-12-31 23:54:08";  
  2.  
  3. String str4 = "1927-12-31 23:54:09"; 

The ld4-ld3 result is 1.

 
 
  1. sun.util.calendar.ZoneInfo[id="Asia/Shanghai", 
  2.  
  3. offset=28800000,dstSavings=0, 
  4.  
  5. useDaylight=false, 
  6.  
  7. transitions=19, 
  8.  
  9. lastRule=null] 
  10.  
  11.   
  12.  
  13. Locale(Locale.getDefault()): zh_CN 

Solution

This is the Shanghai time zone, and there was a change in December 31.

Refer to this website to learn more about the time zone changes in Shanghai on January 1, 1927. Basically at midnight at the end of 1927, the system always calls back at 5 minutes 52 seconds. So "23:54:08" actually happened twice. It seems that Java parses the last time as the local date and time, leading to a difference.

2,Is Java "reference transfer" or "value transfer "?

2480 likes)

I always think that Java isReference TransferHowever, I read a bunch of blogs, such as this one, and I claim that this is not the case. I don't think I understand the differences between them.

Give an explanation?

Solution

Java has always beenValue Transfer. Unfortunately, they decided to call pointers as references, so new people are always confused. BecauseReferenceIt is also passed through the value.

3. AboutJava ++ = OperatorProblems

2223 likes)

Till today I think this example:

 
 
  1. i += j; 

It is simply abbreviated:

 
 
  1. i = i + j; 

However, if you do this:

 
 
  1. int i = 5; 
  2. long j = 8; 

However, I = I + j; cannot be compiled, and I + = j; can be compiled.

This meansi += j;Actuallyi = (type of i) (i + j)Is it short?

Solution

Some people always ask such questions, and there are answers in JLS. See section 15. 26.2 compound assignment operators. Excerpt:

The compound assignment expression of E1 op = E2 is equivalent to E1 = (T) (E1) op (E2). Here T is the type of E1, the difference is that E1 is calculated only once.

For example, refer to Section 15. 26.2.

[...] The following code is correct:

  
  
  1. short x = 3; 
  2. x += 4.6; 

The result of x is 7 because it is equivalent:

  
  
  1. short x = 3; 
  2. x = (short)(x + 4.6); 

In other words, your assumptions are correct.

4. What is the difference between HashMap and Hashtable?

1769 likes)

JavaHashMapAndWhat is the difference between Hashtable?

Which of the following is more efficient for non-multithreading applications?

Solution

There are several differences between HashMap and HashTable in Java:

Because synchronization is not a problem for you, I recommend using HashMap. If synchronization becomes a problem, you may need to take a look.ConcurrentHashMap。

5. How to read or convert an InputStream into a String

1724 likes)

If you have a java. io. InputStream object, such as processing this object and generating a string?

Assume that I haveInputStreamObject that contains text data. I want to convert it into a string, for example, so that I can write the stream content to a log file ).

InputStreamWhat is the simplest way to convert data into a String?

Solution

Use Apache commonsIOUtils library to copy InputStream to StringWriter isA good method, like this:

 
 
  1. StringWriter writer = new StringWriter(); 
  2. IOUtils.copy(inputStream, writer, encoding); 
  3. String theString = writer.toString(); 

Even

 
 
  1. // NB: does not close inputStream, you can use IOUtils. closeQuietly for that
  2. // Note: Do not disable inputStream. You can use IOUtils. closeQuietly
  3. String theString = IOUtils. toString (inputStream, encoding );

Alternatively, if you do not want to mix Stream and Writer, you can useByteArrayOutputStream。


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.