The byte type in Java and the order of three numbers using if-else

Source: Internet
Author: User

1. the byte type is the basic data type of Java, which occupies one byte, 2 ^ 8, the number of tables range:-128 ~ 127 [java] public class TestByte {public static void main (String [] args) {byte b1 = 1; b1 ++; b1 = b1 + 1 ;}} the following error occurs during compilation: TestByte. java: 5: Error: possible loss of precision b1 = b1 + 1; ^ required: byte found: int1 error because constant 1 in b1 + 1 is an int data by default, the addition result cannot be assigned to byte type [java] byte b1 = 1; byte b2 = 1; byte b3 = 1 + 1; byte b4 = b1 + b2; // you can change it to byte b4 = (byte) (b1 + b2) when compiling the 4th rows, the same problem still occurs, because the byte must first be converted to the int Type During computation, the result cannot be assigned to the byte type. The difference between three and four rows: during compilation, the compiler finds that the value on the right is of the int type (a constant "), however, the system checks whether the data is of the byte type (-128 ~ 127) if the value is within the range, the int type on the right is forcibly converted by default, and the last byte is assigned to the variable b3, b1 and b2 are "variables", which means these two values are uncertain and may change at any time. The compiler cannot check or determine whether they are within the byte type range, if it fails to be checked, an error will be reported (precision may be lost). If it is a constant, the compiler can judge it. 2. Input three numbers and then output the process in ascending order: 1. Import class util. java2: Create intent object 3. Call the nextInt () method of intent object to return an int type data 4. Sort. Sort num1 and num2 first, and store num1 small, num2 memory amplification, num3 is only three possible? Num3? Num1? Num3? Num2? Num3? ---------------- + ---------------------- + ---------------> Small and large code list: [java] import java. util. role; public class TestSort {public static void main (String [] args) {role = new role (System. in); System. out. print ("num1 ="); int num1 = numeric. nextInt (); System. out. print ("num2 ="); int num2 = numeric. nextInt (); System. out. print ("num3 ="); int num3 = numeric. nextInt (); int tmp; if (num1> num2) {tmp = num1; num1 = num2; num2 = tmp;} if (num3> num2) {System. out. print (num1 + "" + num2 + "" + num3);} else if (num3> num1) {www.2cto.com System. out. print (num1 + "" + num3 + "" + num2);} else {System. out. print (num3 + "" + num1 + "" + num2 );}}}

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.