Java binary and data type (i)

Source: Internet
Author: User
Tags decimal to binary

First, Java supports decimal, octal, and hexadecimal, but does not support binary

Java supports decimal:

Java indicates that decimal does not require any processing of numbers

Java represents the decimal int a = 13; System.out.println ("Java represents decimal:" +a);

Operation Result:

Java supports octal:

Java represents octal, preceded by "0"

Java represents octal int b = 013; System.out.println ("Java represents octal:" +b);

Operation Result:

Java Support hex

When Java represents hexadecimal, it starts with "0x"

Java represents the hexadecimal int c = 0x13; System.out.println ("Java represents hexadecimal:" +c);

Operation Result:

Java does not explicitly support binary

Because the binary is too long,only strings can be used in Java to represent the binary

Second, the Java operation mode

Described above, Java Support decimal, octal, hexadecimal, but the careful small partner immediately found that the result of the above is not what we want to specify the number of numbers, but unified in decimal notation, which shows thatJava in the operation always use binary as the method of operation.

Three, the conversion of the input system

Integer converted to string

First, declare a decimal integer number:

int result = 13;

octal, decimal

The result is converted from decimal to octal string octalstr = integer.tooctalstring (result); System.out.println (OCTALSTR);

Operation Result:

hexadecimal, decimal

Converts result from decimal to hexadecimal string hexstr = integer.tohexstring (result); System.out.println (HEXSTR);

Operation Result:

decimal--Binary

The result is converted from decimal to binary string binarystr = integer.tobinarystring (result); System.out.println (BINARYSTR);

Operation Result:

The string is converted to an integral type:

interger.valueof (string, Binary) method to convert decimal to an integer representation of the other binary

String resultstr = "13";//convert to octal result = integer.valueof (ResultStr, 8); System.out.println ("converted into octal:" +result); result = Integer.valueof (resultstr,16); System.out.println ("to Hex:" +result);

Operation Result:

So what happens now when we turn retultstr into binary?

result = Integer.valueof (resultstr,2); System.out.println ("Into binary:" +result);

Operation Result:

It turns out thatJava really cannot represent the binary in integer form

Java binary and data type (i)

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.