Java Beginner Knowledge points

Source: Internet
Author: User

public class Enumtest {public          static void Main (string[] args) {                   Size s=size.small;                   Size T=size.large;                   S and T refer to the same object?                   System.out.println (s==t);                   //is the original data type?                   System.out.println (S.getclass (). isprimitive ());                   Converts a                   Size u=size.valueof ("SMALL") from a string;                   System.out.println (s==u);  True                   //lists all its values for                   (Size value:Size.values ()) {                            System.out.println (value);}                   }         } enum Size{small,medium,large};

  

Run will get results

This shows that the different constants defined by the enumeration type are not the same object and are no longer the original data type, and the results obtained are the same. However, different constants are defined.

Different data types in Java are stored differently and occupy different spaces

Short 2byte
int 4byte
Long 8byte
Char 1byte
Float 32byte
Double 64byte

It is common sense to lose precision when converting from a small byte to a large byte, but a double from int to float can also lose precision because the integer portion of int is 32 bits and the float integer part is only 16 bits, so it loses precision.

public class Testdouble {

public static void Main (String args[]) {

System.out.println ("0.05 + 0.01 =" + (0.05 + 0.01));

System.out.println ("1.0-0.42 =" + (1.0-0.42));

SYSTEM.OUT.PRINTLN ("4.015 * 100 =" + (4.015 * 100));

System.out.println ("123.3/100 =" + (123.3/100));

}

}

There are some problems with double calculation, and the result is this

A conclusion is calculated using a numeric value of type double , the result is imprecise.

public class test1{

publicstatic void Main (String args[])

{

intx=100;

inty=200;

System.out.println ("x+y=" +x+y);

System.out.println (x+y+ "=x+y");

}

}

The result of the execution is this.

This is because the value of the plus sign at the time of the output is from left to right if there is a string on both sides of the plus sign, it will add the back to the back of the previous one, two numbers will be added,

Java Beginner Knowledge points

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.