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:
- public static void main(String[] args) throws ParseException {
-
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
- String str3 = "1927-12-31 23:54:07";
-
- String str4 = "1927-12-31 23:54:08";
-
- Date sDt3 = sf.parse(str3);
-
- Date sDt4 = sf.parse(str4);
-
- long ld3 = sDt3.getTime() /1000;
-
- long ld4 = sDt4.getTime() /1000;
-
- System.out.println(ld4-ld3);
-
- }
The output is:
- 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:
- String str3 = "1927-12-31 23:54:08";
-
- String str4 = "1927-12-31 23:54:09";
The ld4-ld3 result is 1.
- sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
-
- offset=28800000,dstSavings=0,
-
- useDaylight=false,
-
- transitions=19,
-
- lastRule=null]
-
-
-
- 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:
- i += j;
It is simply abbreviated:
- i = i + j;
However, if you do this:
- int i = 5;
- 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:
- short x = 3;
- x += 4.6;
The result of x is 7 because it is equivalent:
- short x = 3;
- x = (short)(x + 4.6);
In other words, your assumptions are correct.
4. What is the difference between HashMap and Hashtable?
1769 likes)
JavaHashMap
AndWhat 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 haveInputStream
Object 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 ).
InputStream
What is the simplest way to convert data into a String?
Solution
Use Apache commonsIOUtils library to copy InputStream to StringWriter is
A good method, like this:
- StringWriter writer = new StringWriter();
- IOUtils.copy(inputStream, writer, encoding);
- String theString = writer.toString();
Even
- // NB: does not close inputStream, you can use IOUtils. closeQuietly for that
- // Note: Do not disable inputStream. You can use IOUtils. closeQuietly
- String theString = IOUtils. toString (inputStream, encoding );
Alternatively, if you do not want to mix Stream and Writer, you can useByteArrayOutputStream。