The article directories

  • Java Basic Syntax (2) — Data types
  • Int int type
    • 1. Basic syntax format
    • 2. Value range
    • 3. Default initial value of a variable when undefined
    • 4. Precautions
  • Type long integer
    • 1. Basic syntax format
    • 2. Value range
    • 3. Long defines the variable
    • 4. Precautions
  • A floating-point number with a double precision
    • 1. Basic syntax format
    • 2. Double defines the variable
  • A single-precision floating-point number
    • 1. Basic syntax format
    • 2. Float defines variables
  • Char character type
    • 1. Basic syntax format
    • 2. Char Defines variables
    • 3. Precautions
  • Byte Indicates the byte type
    • 1. Basic syntax format
    • 2. Value range
    • 3. Byte Defines variables
    • 4. Precautions
  • 7. Numerical improvement
    • 1. Mix int and long
    • 2. Byte and byte operations
    • 3. Summary
  • The type of the short integer
    • 1. Basic syntax format
    • 2. Value range
    • 3. Precautions
  • Boolean Indicates the Boolean type
    • 1. Basic syntax format
    • 2. Precautions
  • Summary of basic data types
  • String The value is a String
    • 1. Basic syntax format
    • 2. Precautions
    • 3. Escape characters
    • 4. Common operations on strings
    • (1) + operation, indicating string concatenation
    • (2) + can be concatenated with strings and integers
    • (3) Summary
  • Xi. Variables
    • 1. Scope of variables
    • 2. Naming rules for variables
    • (1) Naming rules
    • (2) Soft requirements
    • (3) Small hump naming rules
  • Xii. Constants
    • 1. Literal constants
    • 2. Final keyword modifier constants
  • Type conversion
    • 1. Int and long/double assign to each other
    • 2. Int and Boolean assign to each other
    • 3. Assign a byte value to an int literal constant
    • 4. Cast
    • 5. Summary
  • Finish!!!!!


\

Following up on basic Java syntax (I) – Getting to know Java


\

Java Basic Syntax (2) — Data types


\

Outline of the content of this introduction

This paper mainly introduces the eight basic data types in the figure above.

\

Int int type

\

1. Basic syntax format

\

Int variable name = initial value;

Code examples:

int num = 10; // Define an integer variable
System.out.println(num) ;
Copy the code

2. Value range

\

What is the range int can represent?

In C, there are signed and unsigned numbers, but remember, there is no such thing as unsigned numbers in Java!

So let’s look at the range of int

In Java, ints take up four bytes, 32 bits, the highest bit is a sign bit, and the remaining 31 bits are all numeric bits.

Use the following code to view the scope of integer data in Java:

public class Test {

    public static void main(String[] args) { System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); }}Copy the code

Compile result:



So we verify that the maximum value of int is 2 ^ 31-1, and the minimum value is -2 ^31.

So if we wanted to print Max+1, min-1, what would we get?

 public static void main(String[] args) {

        System.out.println(Integer.MAX_VALUE + 1);

        System.out.println(Integer.MIN_VALUE - 1);

    }
Copy the code

Compile result:

Max +1 = -2 ^ 31

Min-1 =2^31 +1

Why did this happen?

The range of values for int looks like this:

So when int = Max_value + 1, positive numbers become negative

This applies to all types (except decimal float double)

\

3. Default initial value of a variable when undefined

\

When a variable is undefined, there is no default value…

When we print the value of a variable without defining it, the Java editor will directly report an error, which demonstrates Java security.





An overflow occurs if the result of an operation exceeds the maximum range of int. The figure of 2.1 billion is easily exceeded in the current era of big data.

In this case, we need to use a wider range of data types. The Long type is provided in Java to store larger numbers.

\

4. Precautions

\

(1) int indicates that the type of the variable is an integer

(2) The variable name is the identification of the variable. This name is used for the subsequent use of variables

(3) In Java, = means assignment (unlike math), which means to set an initial value to a variable.

(4) Initialization is optional, but it is recommended that variables be initialized when they are created.

(5) Finally, don’t forget the semicolon, otherwise the compilation will fail.

(6) // indicates a comment. Comments are part of the code explanation and do not participate in the compilation.

Type long integer

\

1. Basic syntax format

\

Long variable name = initial;

Code examples:

long num = 10L; // Define a long integer variable with an initial value of 10l (lowercase L, not 1).
System.out.println(num) ;
Copy the code

2. Value range

\

Long is four bytes in C, but eight bytes in Java.

So the long range is 0

Minus 2 to the 63 minus 2 to the 63 minus 1

Let’s look at the values in Java code

 public static void main(String[] args) {
        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
    }
Copy the code

Print result:

Eight bytes, so that’s a big enough number. This data range is much larger than the representation range of int. Enough for most engineering scenarios.

\

3. Long defines the variable

\

public static void main(String[] args) {
       // System.out.println(Long.MAX_VALUE);
       // System.out.println(Long.MIN_VALUE);

        long a=10L;
        System.out.println(a);
    }
Copy the code

\

When we define long integer variable a, if = = 10, the default value is an int. To more accurately represent the value of a, we add L to the end of the value (upper case does not matter, but upper case L is easier to distinguish, lower case L is easier to treat as 1). It’s more accurate to say that we gave 10 as a long integer.

\

4. Precautions

\

(1) The basic syntax is basically the same as creating int variables, except that the type is changed to long

(2) The initial value is 10L, indicating a long integer number. 10L is also acceptable.

10 is an int, and 10L is a long. It is better to use 10L or 10L.

(4) In Java, the long type is 8 bytes. The range of data represented is -2^ 63- > 2^ 63-1

\

A floating-point number with a double precision

\

A Double in Java is 8 bytes

\

1. Basic syntax format

\

Double variable name = initial value;

Code examples:

double num = 1.0;
System.out.println(num)
Copy the code

\

2. Double defines the variable

\

A code:

public static void main(String[] args) {
        double num=12.5;
        System.out.println(num);
    }
Copy the code

Code compilation effect:

The variable value of double num was successfully printed

\

Code 2:

    public static void main(String[] args) {

        int a=1;
        int b=2;
        System.out.println(a/b);

    }
Copy the code

Code compilation effect:



A /b is equal to 0. A /b is equal to 0

In Java, int divided by int is still int(the decimal part is discarded).

If you want 0.5, you need to use double.

public static void main5(String[] args) {

        double a = 1.0;
        double b = 2.0;
        System.out.println(a / b);

    }
Copy the code

Code 3:

 public static void main(String[] args) {
        double c=1.1;
        double d=1.1;
        System.out.println(c*d);
    }
Copy the code

Code compilation effect:



We know that 1.1 * 1.1 = 1.21, so why 1.210000… 2? Where does the last 2 come from?

Well, that’s due to the precision of double itself, because decimals don’t have exact numbers.

Java’s double memory layout complies with the IEEE 754 standard (as does C), and attempts to represent potentially infinite decimals with finite memory space are bound to have certain precision errors.

\

A single-precision floating-point number

\

1. Basic syntax format

\

Float variable name = initial value;

Code sample

  public static void main(String[] args) {
        float f=3.5 f;
        System.out.println(f);
    }
Copy the code

Compile the results

2. Float defines variables

\

If we define a variable f=3.5 for float, let’s look at the result



In C, 3.5 can be assigned to a variable of type float.

Here we are going to popularize a knowledge point:



In Java, 3.5, 12.5, and 3.14 decimals are double by default and will fail compilation if received as a float. We’ll add an f after the decimal to emphasize that the number is of type float, and the compilation will pass.

\

 public static void main(String[] args) {
        float f=3.5 f;
        System.out.println(f);
    }
Copy the code

\

Char character type

In Java, a variable of type CHAR takes up two bytes in memory

\

1. Basic syntax format

\

Char variable name = initial value;

The code says

public static void main(String[] args) {
        char ch='a';
        System.out.println(ch);
    }
Copy the code

Compile the results

2. Char Defines variables

\

A code

public static void main(String[] args) {
        char ch='a';
        System.out.println(ch);
    }
Copy the code

Compile the results

Code 2

 public static void main(String[] args) {
        char ch='Chen';
        System.out.println(ch);
    }
Copy the code

Compile the results



We can use the char type to define a Character

Code 3:

 public static void main(String[] args) {
        char ch=97;
        System.out.println(ch);
    }
Copy the code

Compile the results



We can define a number for the char type, which compiles to the corresponding character in Unicode.

Code 4:

 public static void main(String[] args) {
        char ch=-10;
        System.out.println(ch);
    }
Copy the code

Compile result:





We know that C language can use numbers to represent the corresponding English characters, common characters, this table is ASCII table

Java, on the other hand, has a table called Unicode, which represents a much larger range of characters, arguably including the ASCII range.

However, what they have in common is that characters and numbers start at 0, and there are no negative numbers.

So a Char in Java must represent a positive number

\

3. Precautions

\

(1) Java uses single quotation marks + single letters to represent character literals.

(2) A character in a computer is essentially an integer. ASCII is used for characters in C, while Unicode is used for characters in Java. So a character takes up two bytes and represents a wider variety of characters, including Chinese.

\

Byte Indicates the byte type

Byte contains one byte in Java

\

1. Basic syntax format

\

Byte variable name = initial value;

Code examples:

byte value = 0; 
System.out.println(value);
Copy the code

\

2. Value range

\

Byte is 1 byte and 8 bits, so its range is

Minus 2 to the seventh — 2 to the seventh minus 1

-128——127

\

3. Byte Defines variables

A code:

public static void main(String[] args) {
        byte a=10;
        System.out.println(a);
    }
Copy the code

Compile result:



We assign a value of 10 and compile and print 10.

Code 2:

public static void main(String[] args) {
        byte a=128;
        System.out.println(a);
    }
Copy the code

Compile result:



We assigned 128 to a byte variable, but found compilation error, what does that mean?

When assigning a value, do not assign a number outside the current range, otherwise an error will be reported. The same is true for int types.

Code 3:

public static void main(String[] args) {
      byte b1=10;
      byte b2=20;
      byte b3=b1+b2;
        System.out.println(b3);
    }
Copy the code

Compile result:



B1 +b2=30; b1+ B2 =30;

Description:

4. Precautions

\

(1) The byte type also represents an integer. Only one byte, indicating a small range (-128 -> +127)

(2) Byte types and character types are incompatible.

\

7. Numerical improvement

\

When introducing byte types above, we mentioned integer promotion, so let’s look at the specific promotion rules.

For more details on the rules for integer promotion, check out my previous blog, Implicit Integer promotion for type conversions

\

1. Mix int and long

Conclusion:

When int and long are mixed, int is promoted to long, and the result is still of type long, requiring a variable of type long to receive the result. If you must use an int to receive the result, you need to use a cast.

The meaning of type promotion: speed up the computer CPU calculation

\

2. Byte and byte operations



conclusion

Byte and byte are of the same type, but a compilation error occurs. If c is assigned to a, the error will occur. If c is assigned to b, the error will occur. If c is assigned to b, the error will occur.

Because a computer’s CPU usually reads and writes data from memory in units of four bytes. For hardware convenience, types less than 4 bytes, such as byte and short, are promoted to ints before they participate in computation.

Correct way to write it:

3. Summary

\

Type promotion summary:

\

(1) Mixed operation of different types of data, the small range will be promoted to the large range.

(2) For short and byte types smaller than 4 bytes, they will be promoted to 4 bytes of int before operation.

\

The type of the short integer

\

1. Basic syntax format

\

Short variable name = initial value;

Code examples:

public static void main(String[] args) {
        short value = 12;
        System.out.println(value);
    }
Copy the code

\

2. Value range

\

In Java, short is 2 bytes and 16 bits

So the range of values that he represents is

– 32768 – > + 32767

\

3. Precautions

\

(1) Short occupies 2 bytes and represents the data range of -32768 -> +32767

(2) This expression has a relatively small range and is generally not recommended.

\

Boolean Indicates the Boolean type

Boolean has this type in Java and C++, but not in C

1. Basic syntax format

\

Boolean variable name = initial value;

Code examples:

 public static void main(String[] args) {
        boolean value = true;
        System.out.println(value);
    }
Copy the code

\

2. Precautions

\

(1) Boolean variables have only two values: true and false.

(2) Java Boolean types and int cannot be converted to each other. There is no such thing as 1 for true and 0 for false.

Some JVM implementations are 1 byte, some are 1 bit, and this is not specified.

A code



The result is as follows:



So int and Boolean are two unrelated types that cannot be evaluated

Code 2



Assign 0,1 to Boolean, and the editor returns an error

Summary of basic data types

The data type A wrapper class byte
int Integer 4
short Short 2
char Character 2
byte Byte 1
long Long 8
float Float 4
double Double 8
boolean Boolean Not clearly defined

\

String The value is a String

\

A string is formed by putting characters together

\

1. Basic syntax format

\

String variable name = “initial value”;

Code examples:

 public static void main(String[] args) {
        String name = "zhangsan";
        System.out.println(name);
    }
Copy the code

Compile result:

2. Precautions

\

(1) Java uses double quotes + several characters to represent string literals.

(2) Unlike the above type, String is not a primitive type, but a reference type (more on that later).

(3) Some special characters in the string that are not easy to express directly need to be escaped.

3. Escape characters

\

Escape character explain
\n A newline
\t Horizontal TAB character
Single quotes
Double quotation marks
\ The backslash

There are many more, for now these escape characters…

\

4. Common operations on strings

\

(1) + operation, indicating string concatenation

\

A code

   public static void main(String[] args) {
        String a = "hello";
        String b = "world";
        String c = a + b;
        System.out.println(c);
    }
Copy the code

The result is as follows:

(2) + can be concatenated with strings and integers

\

Code 2:

    public static void main(String[] args) {
        String str = "result = ";
        int a = 10;
        int b = 20;
        String result = str + a + b;
        System.out.println(result);
    }
Copy the code

The result is as follows:

(3) Summary

\

The above code shows that string concatenation is performed whenever a string is present in a + expression.

So we can easily print multiple strings or numbers at the same time using system.out.println.

\

Xi. Variables

\

1. Scope of variables

Scope is the scope in which the variable is valid, usually the code block in which the variable is defined (curly braces).

A code:

 public static void main(String[] args) {

        int x = 10;
        System.out.println(x); // The compiler passes;
    }
Copy the code

Compile result:



Successfully printed 10

Code 2:

    public static void main(String[] args) {{int x = 10;
            System.out.println(x); // The compiler passes;
        }
        System.out.println(x);
    }
Copy the code

Compile result:



At this point, x is defined in a local code block, and after the local scope, x is destroyed, so the second print x compilation fails.

Corrected code:

    public static void main(String[] args) {{int x = 10;
            System.out.println(x); // The compiler passes;
        }
        int x=5;
        System.out.println(x);
    }
Copy the code

Compile result:



It compiles and prints out two x values: 10 and 5

\

2. Naming rules for variables

\

(1) Naming rules

(2) Soft requirements

(3) Small hump naming rules

\

When a variable name consists of more than one word, capitalize all but the first word.

Naming examples:

int maxValue = 100;

String studentName = “studentName”;

\

Xii. Constants

\

The variables discussed above are of various types, and each type of variable corresponds to a constant of the same type.

Constants mean that the runtime type cannot change.

Constants are mainly embodied in the following two forms:

\

1. Literal constants

\

10 Int Literal constants (decimal)
010 An int literal constant (octal) starts with the number 0. 010 is the decimal 8
0x10 Int Literal constants (hexadecimal) start with the number 0x. 0x10 is decimal 16
10L Long literal constant. Or you could write 10L (lowercase L).
1.0 Double is a literal constant. It can also be written as 1.0D or 1.0D
1.5 e2 Double is a literal constant. Scientific notation. That’s 1.5 times 10 squared
1.0 f Float literal constant, also written as 1.0f
true Boolen literal constants, as well as false
‘a’ Char literal constants that can contain only one character in single quotes
“ABC” String literal constants that can have more than one character in double quotes

\

2. Final keyword modifier constants



The result is as follows:



Final in Java is similar to const in C, making the modified variable a constant that cannot be changed during program execution.

\

Type conversion

\

As a strongly typed programming language, Java requires strict verification when assigning values to variables of different types

We use the following code to understand the data type conversion:

\

1. Int and long/double assign to each other

\

A code:



Long represents a larger range. You can assign an int to a long, but you can’t assign a long to an int.

Code 2:



Double has a larger range. You can assign an int to a double, but you cannot assign a double to an int.

Conclusion:

Assignments between variables of different numeric types indicate that smaller types can be implicitly converted to larger types, and vice versa.

\

2. Int and Boolean assign to each other

code:



conclusion: int and Boolean are unrelated types and cannot be assigned to each other.

\

3. Assign a byte value to an int literal constant

The following code:



Pay attention to: byte indicates that the data range is -128 -> +127. 256 is out of the range and 100 is still in the range.

Conclusion: When literal constant assignments are used, Java automatically does some checking to determine if the assignment is reasonable.

\

4. Cast

\

How do I use casts?

Example code 1:



Compile result:



The result is a successful compilation that uses a cast to convert data of type int to byte so that the value of A can be assigned to B.

Code 2:



Compile result:



So we know that not all types of data can be cast.

Conclusion:

5. Summary

\

(1) Assign values between variables of different numeric types, indicating that a type with a smaller range can be implicitly converted to a type with a larger range.

(2) If you need to assign a large range of types to a small range, you need to cast, but the accuracy may be lost.

(3) When assigning a literal constant, Java automatically checks for numeric ranges.

Finally, we’ll link cast and implicit casting together to see the differences:













Well, this time the Java data type knowledge to share here, thank you for your appreciation and attention!

Thank you !!!! Java basic syntax (3) – operators have been updated, welcome to pay attention to oh!! \

Finish!!!!!