Some problems encountered in Java learning and Solutions __java

Source: Internet
Author: User

1, when using the BigDecimal data type to do division operations, the following pop-up exception:

public static void Main (string[] args) {
        BigDecimal a = new BigDecimal (1000);
        BigDecimal B = new BigDecimal (1500);
        System.out.println (A.divide (b));

Exception in thread "main" java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.
    At Java.math.BigDecimal.divide (bigdecimal.java:1690) at
    Test. Test.main (TEST.JAVA:16)

The problem arises because:
The result of dividing A and B cannot be represented by a finite decimal number.
So in order to solve this problem, we should set a scope for the result. The code is as follows

public static void Main (string[] args) {
        BigDecimal a = new BigDecimal (1000);
        BigDecimal B = new BigDecimal (1500);
        System.out.println (A.divide (b, 2, bigdecimal.round_half_up));
}

This problem, I also looked for some answers on the internet, such as, there is a say to the Divide method set a decimal point precision, a.divide (b, 2), on the value of the above code input, the result is 1, obviously this is not necessarily the answer we want.

2 When using Bytebuffer, if you want to read byte in buffer, you can call Buffer.readbyte () or buffer.readbytes (length). Buffer.readbyte () returns a byte of data directly, buffer.readbytes (length) returns the length byte of data. Here, if you need an array type of data, you can use Buffer.readbytes (length). Array () directly. When you need only one byte of data, the length is set to 1.

3 In the course of today's code codes, it is pointed out that some of the code is somewhat redundant.

public void Test () {
    String Hexaltposi = ' E8 ';
    Channelbuffer posi = Channelbuffer.copiedbuffer (Strutil.hexstringtobytes (Hexaltposi));
    Hexstringtobytes () is the conversion of Hexaltposi from 16 to bytes
    //Want to remove the number from the buffer and convert to int, first write out the previous code
    SYSTEM.OUT.PRINTLN ( Util.asint (Strutil.bytestohexstring (posi.readbytes (1). Array ()));
    Code
    System.out.prinlen ((int) posi.readunsignedbyte ());

The reason for this is because of their lack of understanding of the buffer API.
I'm going to list some of the API tips here.
①buffer.readablebytes () does not increase or decrease the read index value of the buffer
②buffer.skipbytes (length) increases the length of the read index value of the buffer directly. When your buffer contains some fields that you do not know about, or at the end of the buffer, you can skip it by using this method directly.
③ sometimes (int) buffer.readbyte () The data read becomes a negative value, which needs to be used (int) buffer.readunsignedbyte ().
...

4 for integer var =. In the range of 128 to 127, the integer object is generated in the Integercache.cache, will reuse the existing object, the interval of the integer value can be directly used to judge , but beyond this interval All data, will be generated on the heap, and will not reuse existing objects , this is a large pit, recommended to adapt to the Equals method to judge. (This paragraph is quoted in the Alibaba Java Development Manual)

This blog continues to be updated ...

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.