The article directories

  • Eight basic types
    • Char
    • boolean
  • The literal value of the base type
  • Unit conversion of bits, bytes, etc
  • Understand base conversion
  • Basic type operation rules
  • Data type conversion
  • practice
    • Displays the maximum and minimum values of byte,short,int, and long
    • Switching variable
    • Double, find the distance of free fall
    • How many magpies are needed to meet the cowherd and the Weaver maid

Eight basic types

The Java language provides eight basic types:

Byte 1 byte; Range: -128 to 127 (-2^7 to 2^7-1) Short 2 bytes; Range: -32768 ~ 32767 (-2^15 ~ 2^15-1) int 4 bytes; Range: -2,147,483,648 to 2,147,483,647(-2^31 to 2^31-1) long 8 bytes; Scope: – 9223372036854775808 ~ 9223372036854775807 (2 ^ 63 ~ 2 ^ 63-1) float 4 bytes; Single precision double 8 bytes; Char 2 bytes; Boolean 1 byte;

Char

2 bytes, range: 0-65535 CHAR Data type can store any character:

Char a = ‘a’; // Add single quotation marks to any single character. Char a = ‘middle’; // Add single quotation marks to any single Chinese character. char a = 111; / / integer. 0 ~ 65535. Decimal, octal, hexadecimal can be. The corresponding character in the output character encoding table.

Note: char can only hold a single character.

char c1 = 'a';
char c2 = 97; Output: A aCopy the code

boolean

1 byte, with only two values: true and false. True true 00000001 False false 00000000

The literal value of the base type

Int a = 123; long b = 9999999999; // Error, right side is int, but out of range

2. Byte A = 127 can be assigned to integers shorter than the range of int. Byte b = 128; // this is an int

Double a = 3.14; Float b = 3.14; float b = 3.14; // The right literal is of type double, but the left is defined as float

4. Suffix L long long a = 9999999999L; // This is correct

F float float float a = 3.14f;

D double

double a = 3.0;

double a = 3D;

The binary prefix is 0B. The octal prefix is 0. The decimal prefix is 0x

We can print 11 in different bases in our program

tv.setText("Binary:"+0b11+"\n"+
                "Octal:"+011+"\n"+
                "Decimal system:"+11+"\n"+
                "Hex:"+0x11+"\n");
Copy the code

Run the program and the result is as follows:

Unit conversion of bits, bytes, etc

The data in the computer is binary, with 01 denoting high and low potential. Each bit is 1 bit, and each 8 bits is 1 byte. If your computer has 4 gb of memory, this translates to 1024(K) * 1024(M) * 1024(G) * 4 bytes

Bit bit byte — — — — — — — — — — — — — — — — 1 byte (eight bits) KB — — — — — — — — 1024 byte MB — — — — — — — — 1024 KB gb — — — — — — — — a 1024 MB TB — — — — — — — — 1024 gb pb——–1024tb

Understand base conversion

Binary numbers are represented in binary as follows, advancing 1 for every 2

00000000 — — — — — — — — 0 00000001 — — — — — — — — 1 — — — — — — — 00000010-2, 00000011 — — — — — — — — 3, 00000100 — — — — — — — — 4… 11111111 — — — — — — — — 255

Since the left bit in Java does not participate in counting but is used to represent positive and negative numbers, 0 for positive numbers and 1 for negative numbers, the range of byte is as follows

10000000 — — — — — — — — — 128… 11111101 — — — — — — — — – 3, 11111110 — — — — — — — — — 2, 11111111 — — — — — — — — — 00000000 — — — — — — — — 1 0… 01111111 — — — — — — — — 127

Binary to decimal 01111111 converted to decimal number can be calculated like this, each digit after the number of digits multiplied by 2 power, so the calculation process is as follows:

26 + 1 * 1 * 25 23 + 1 + 1 + 1 * * 24 * 21 + 22 + 1 * 1 * 20 = 127

Common binary and decimal conversion 1 1 10 2 8 32 1000000 64 10000000 128 100000 100 1000

The rule is simple: for every zero added to binary, the decimal result is multiplied by two. The reason is simple: the analogy is decimal, which multiplies by ten for every zero added to it. That makes sense.

Binary to hexadecimal In mathematics, hexadecimal is a system of 1 – digits in 16. It is usually represented by numbers 0 to 9 and letters A to F (or A to F), where A to F indicates 10 to 15. These are called hexadecimal numbers

Every four bits in binary can be represented by one bit in hexadecimal, for example: binary: 0101-> hexadecimal: 5 This is easy to calculate, as we talked about binary 100-> decimal 4. So binary 101-> decimal 5. So 0101 is a decimal 5. Since the hexadecimal 0123456789abcdef represents decimal numbers from 0 to 15, binary: 0101-> hexadecimal: 5

Binary: 1101-> hexadecimal: d Similarly. According to the binary to decimal method, 1101 represents decimal 13, while d represents 13 in hexadecimal

Binary to octal octal is a counting method based on eight digits, 0,1,2,3,4,5,6,7, carried by one

Every three bits in binary can be represented by one bit in octal for 000, 0, 101, 5

‘\u0061’ decimal 97, which stands for a, must be written in two full bytes, so it is summed up as follows: 1. All other decimal digits can be calculated as follows: 1. Hex \u0061 to decimal 1*160+6×161+0x162+0x163=97 1*160+6×161+0x162+0x163=97 2. Divide the number to be converted by the number to be converted to obtain the quotient and remainder 97 ÷ 16 = 6 + 1, so the hexadecimal 61, must write the whole two bytes, plus the prefix that is \u0061 3, other base to other base, can let decimal as the medium

Of course, it doesn’t matter if you can’t remember it, you can convert it directly online

Basic type operation rules

1. The data type of the operation result is consistent with the maximum range of operation type

3/2=1// both are int, and the result is int
3d/2=1.5//3d is a double, so the result is double
Copy the code

2. Byte /short/char Operations on integers smaller than int are converted to int first

This was designed by Java developers to allow for spillover effects when performing operations of this basic type

byte a = 2;
byte b = 3;
byte c = a + b;// the result of the operation is int
Copy the code

3. Integer arithmetic overflows

Integer.MAX_VALUE + 1 overflows into integer.min_value

This is incorrect because the multiplication of the first two numbers is outside of an int. The correct number is 300000000L606024365

4. Floating point arithmetic is not accurate

System.out.println(2-1.9); Output:0.10000000000000009
Copy the code

5. Special floating-point values

The floating-point number /0 yields Infinity

System.out.println(3.14/0); Output: InfinityCopy the code

Nan Not a number Math. SQRT (negative) yields Nan

System.out.println(Math.sqrt(-1)); Output: NanCopy the code

Data type conversion

Small data range -> large data range direct conversion can be

byte a = 127;
int b = a;
Copy the code
byte a = -128;
int b = a;
Copy the code

Large data range -> Small data range requires a cast character to implement the cast

int a = 127;
byte b = (byte)a;
Copy the code

practice

Displays the maximum and minimum values of byte,short,int, and long

In our first Android application yesterday, we put a TextView control inside the layout, the XML file

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Copy the code

Then place the following code in the Activity’s onCreate method

        TextView tv = findViewById(R.id.textView1);
        byte a = -128;
        byte b = 127;

        short c = Short.MIN_VALUE;
        short d = Short.MAX_VALUE;

        int e = Integer.MIN_VALUE;
        int f = Integer.MAX_VALUE;

        long g = Long.MIN_VALUE;
        long h = Long.MAX_VALUE;
        
        float i = Float.MAX_VALUE;
        float j = Float.MIN_VALUE;

        Double k = Double.MAX_VALUE;
        Double l = Double.MIN_VALUE;

        tv.setText(String.valueOf(a));
        tv.append("\n"+b);
        tv.append("\n"+c);
        tv.append("\n"+d);
        tv.append("\n"+e);
        tv.append("\n"+f);
        tv.append("\n"+g);
        tv.append("\n"+h);
        tv.append("\n"+i);
        tv.append("\n"+j);
        tv.append("\n"+k);
        tv.append("\n"+l);
Copy the code

The results of running the program are as follows:



The minimum and maximum values of Float and Double are printed in scientific notation, endingE + digitalIt tells us how many powers of 10 we have to multiply the number before E. Such as3.14 e3So 3.14 x 103 = 3,140, 3.14E-3 = 3.14 x 10-3 =0.00314.

So 1.4E-45 represents 1.4 x 10-45

Switching variable

The XML file is changed to

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="Exchange" />
Copy the code

Add the following methods to the Activity

    public void doClick(View view) {
        EditText et1 = findViewById(R.id.editText);
        EditText et2 = findViewById(R.id.editText2);
        TextView tv = findViewById(R.id.textView1);

        String a = et1.getText().toString();
        String b = et2.getText().toString();
        String c = "";

        c = a;
        a = b;
        b = c;

        tv.setText("a=" + a);
        tv.append("\nb=" + b);
    }
Copy the code

Run the program and the result is as follows:

Double, find the distance of free fall

XML code

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input fall time" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="Find the landing range." />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Copy the code

Java code

 public void doClick(View view) {
        TextView tv = findViewById(R.id.tv);
        EditText et = findViewById(R.id.et);

        Double t = Double.parseDouble(et.getText().toString());
        Double d = 0.5 * 9.8 * t * t;

        tv.setText("Landing distance is + d);
    }
Copy the code

Run the program and the result is as follows:

How many magpies are needed to meet the cowherd and the Weaver maid

Niu LAN Zhinu apart 14.6 light years, the speed of light 299792458 meters /s, magpie body length 0.46 meters, the number of magpies needed

		//1 light year away
		long ly = 299792458L*60*60*24*365;
		// The cowherd and the Weaver Girl
		double d = 14.6*ly;
		// Count magpies
		double s = d/0.46;
		// Round up the number of magpies
		long l = (long)Math.ceil(s);
		System.out.println(l);
Copy the code

Try it out for yourself and see how many magpies you need