Fundamentals of java language

Keywords: Java C++

IDE (integrated development environment):

Including code editor, compiler, debugger, graphical interface and other tools.

Common integrated development environments:

IDEA

eslipse

java language comments:

Note: describe the function of the code and mark it with specific symbols

1. Single line comment (shortcut key: ctrl + /): / / describe a line of code

2. Multiline comment (shortcut key: ctrl+shift + /): / * * / comment multiline code

3. Document annotation: annotate classes, attributes (member variables) and methods, and you can see your annotated document information when calling

Additional use of comments: debugging code

When calling member methods and member variables directly with class names, member methods and member variables must be decorated with static modifiers

keyword

Words with specific meanings enriched by the java language are mainly used to modify packages, classes, methods and variables (lowercase letters)

Reserved word

Keywords not used in the current version of java may be used later

goto, const

identifier

Definition: a character sequence that names packages, classes, methods, variables, etc. (that is, something we can name ourselves)

Naming conventions

1. Mandatory syntax:

1, What can be used: uppercase and lowercase letters, numbers, underscores, dollar symbols$

2, The number cannot start with a space

3, Cannot be a keyword

4, Strictly case sensitive

2. By convention:

1, See the name and know the meaning

2, Hump rule

3, Class name, interface name, all words are capitalized (big hump)

4, Method name, variable name: the first word is lowercase, and the first letter is uppercase from the second word (small hump)

5, Constant name: all letters are capitalized and words are separated by an underscore

variable

Definition of variables

Variable is the most basic storage unit of a program, and its value is variable at run time.

The nature of variables

The essence of a variable is a small area of memory.

Use of variables

Variables need to be declared before assignment.

int x;
x=10;

perhaps

int x=10;

Variable declaration syntax (java is a strongly typed language)

[modifier] data type variable name = value;

It can be roughly divided into the following types:

int y;
y=10;

or

int y=10;

or

int x,y,z;
x=10;
y=20;
z=30;

or

int x=10,y=20,c=30;

data type

function

Data types can guide how data in a program is stored and how it is calculated

classification

Data types can be divided into basic data types and reference types

Basic data types can be divided into numerical type, character type (char) and boolean type (Boolean).

Numeric types can be divided into integer and floating point types.

Reference types can be divided into class es, interfaces, and arrays ([]).

String (string) is a class (so the first letter is capitalized) that represents the string type

eg: for example, the string a is declared here and assigned China

String a="China";

String is a class defined in the java core class library and represents the string type

plastic

Shaping can be divided into byte (one byte), short (two bytes), int (four bytes) and long (eight bytes).

Note: the default integer literal value is int, which means that the literal value of long type needs to be followed by L (both uppercase L and lowercase L are OK, but we generally use L to distinguish it from the literal value)

For example:

long a=333L;

Each integer type in java has a fixed expression range and field length, which is not affected by the specific operating system to ensure the portability of java programs

For integer literals

Binary representation: integer literal value preceded by 0 (zero) b

For example:

int x=0b11;

The x here is printed as 3 in decimal

Octal representation: the integer literal value is preceded by 0 (zero)

int y=011;

The y here is printed out as a decimal 9

Hexadecimal representation: preceded by 0 (zero) x

int z=0x11;

The z here is printed in decimal 17

The specific function analysis is as follows:

package day1;

public class demo1 {
    public static void main(String[] args){
        int y=2;
        byte x=24;
        short c=59;
        /*
        When declaring a long integer variable, since the default type of the integer variable is int, you need to add L after the literal value 77
        (L here can be uppercase L or lowercase L, but we usually use uppercase L (easy to distinguish))
         */
        long z=77L;
        //0b indicates binary
        int b=0b11;
        //0 indicates octal
        int i=011;
        //0x indicates hexadecimal
        int g=0x11;
        System.out.println(x+"\n"+y+"\n"+z+"\n"+c+"\n"+b+"\n"+i+"\n"+g+"\n");
        //The class that stores the maximum value in int is Interger
        System.out.println(Integer.MAX_VALUE);
        //The rest of the stored maximum value class is its own word, just capitalized
        System.out.println(Short.MAX_VALUE);
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Long.MAX_VALUE);
    }
}

Operation result: [the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-api88mSC-1634382662452)(C:\Users \ Yangmin \ appdata \ roaming \ typora \ typora user images \ 163438048216. PNG)]

float

Floating point types can be divided into float and double.

Floating point: represents a decimal point

The default for floating-point literals is double

Floating point literals are represented in two ways:

1. Decimal representation

2. Scientific notation

float (single precision)

Variables of type float occupy four bytes

The default type of floating-point literal is double. If you want to represent the literal of float type, you need to add f or lowercase f before the literal (used to use f (uppercase f))

For example:

float a=1.23F;

float a=10.0001; This statement is wrong, because the default type of floating-point literal is double, and the range of double is larger than that of float, so implicit conversion is not allowed, that is, forced type conversion is required, so the above statement will report an error

float a=10.0001;  //This statement is wrong

The statement float a=10.0001F is correct

float a=10.0001F;  //This statement is correct
package day1;

public class demo2 {
    public static void main(String[] args) {
        //Boolean variables a can only store true or false
        boolean a=true;
        //char type variables actually store the decimal code corresponding to this character
        char b='a';
        char c='b';
        //Since char type stores the decimal code of characters, it means that char type variables can participate in the operation,
        // Therefore, the value of h is a char variable b, that is, the code of the actual character a is 97 + 0, that is, 97
        int h=b+0;
        /*
        Note here that f or F must be added after 0.001, that is, after the literal value, or an error will occur,
        Because the default type of floating-point literal is double, and because the range of double type is larger than float,
        Therefore, f or F must be added, or an error will be reported
         */
        float d=0.001F;
        /*
        Here, a single precision floating-point variable g is assigned an integer value of 10. At this time, the range of integer values is smaller than that of float
        Therefore, implicit conversion will be triggered, so no error will be reported
         */
        float g=10;
        //As above, this can also be implicitly converted
        double e=10;
        double f=10.13;
        System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g+"\n");
        System.out.println("\n"+h);
    }
}

The operation result is:

[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bkzn1bei-163482662454) (C: \ users \ Yang Minmin \ appdata \ roaming \ typora \ typora user images \ 163480342550. PNG)]

Double (double)

Double type takes up 8 bytes. It is double precision and has higher precision

Boolean

boolean (logical value)

The variable value of boolean type can be true or false, not other values

character

char, which can represent a single character and occupy two bytes of memory

eg char a=“a”; The char type variable a here stores not the character a, but the code of the stored character a in the Unicode code table (decimal code, i.e. 97). Only when displayed on the computer, it will be converted into the character a through the UTF-8 character set

package day1;

public class demo2 {
    public static void main(String[] args) {
        //Boolean variables a can only store true or false
        boolean a=true;
        //char type variables actually store the decimal code corresponding to this character
        char b='a';
        char c='b';
        //Since char type stores the decimal code of characters, it means that char type variables can participate in the operation,
        // Therefore, the value of h is a char variable b, that is, the code of the actual character a is 97 + 0, that is, 97
        int h=b+0;
        /*
        Note here that f or F must be added after 0.001, that is, after the literal value, or an error will occur,
        Because the default type of floating-point literal is double, and because the range of double type is larger than float,
        Therefore, f or F must be added, or an error will be reported
         */
        float d=0.001F;
        /*
        Here, a single precision floating-point variable g is assigned an integer value of 10. At this time, the range of integer values is smaller than that of float
        Therefore, implicit conversion will be triggered, so no error will be reported
         */
        float g=10;
        //As above, this can also be implicitly converted
        double e=10;
        double f=10.13;
        System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g+"\n");
        System.out.println("\n"+h);
    }
}

The operation result is: [the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-nMc7bVvF-1634382662455)(C:\Users \ Yangmin \ appdata \ roaming \ typora \ typera user images \ 1634380462959. PNG)]

char type variables can participate in arithmetic operation, and the decimal coded value in Unicode character set is used to participate in the operation

The character representation in java uses a Unicode encoding table

The Unicode character set can represent all the languages in the world

Unicode mainly stores cross reference characters (only decimal encoding)

UTF-8 character set is a specific representation of Unicode encoding table

UTF-8 is a variable length character set (the length can be changed and can be converted between 1-4 bytes. When the stored characters are different, the storage space is not the same. For example, one byte of English letters can be stored, and three bytes of Chinese letters can be stored)

(there are few English characters and one byte can be stored, so an English letter occupies only one byte in the UTF-8 character set)

(there are many Chinese characters, and one byte cannot represent it. In UTF-8, one Chinese character accounts for three bytes)

Unicode (encode the characters of each country, which can be understood as only encoding)
UTF-8 (there are corresponding codes and characters, which is UTF-8 when displayed in IDEA)

Basic data type conversion

package day1;

public class demo3 {
    public static void main(String[] args) {
        /*
        The default conversion (implicit conversion) was made because the capacity of byte is less than that of int
         */
        byte a=127;
        int b=a;
        /*
        Implicit conversion is carried out, although the variable of double type accounts for eight bytes, while the variable of float type accounts for only four bytes,
        However, when floating-point variables are stored in memory, the storage method is similar to scientific notation,
        Even if it only takes up four bytes, it takes up more capacity than the eight bytes of a variable of type long
         */
        long c=123456L;
        float d=c;
        /*
        Forced type conversion. Since the value of the int type variable e is 127, it is also within the storage range of byte type,
        Therefore, it will not cause data overflow or loss of numerical accuracy
         */
        int e=127;
        byte f=(byte)e;
        /*
        This is also a forced type conversion, but the value of the int variable g here is 258, which is beyond the range of byte type,
        Therefore, it will cause data overflow. After data overflow, the bytes beyond the byte type range will be removed, resulting in the change of variable value
        Here h becomes 2, that is, the output h is 2
         */
        int g=258;
        byte h=(byte)g;
        /*
        Here is also a forced type conversion, except that the value of variable i of float type is assigned to variable j of long type,
        Since the long type can only access integers, the number after the decimal point will be deleted, which will cause progress loss
         */
        float i=10.35F;
        long j=(long)i;
        /*
        Here is also a forced type conversion, but the following values are mixed types, and the final values are double types,
        Because the literal value of mixed types is calculated after conversion to mass capacity, and finally forced type conversion to int type,
        It will cause the loss of data accuracy
         */
        int n=(int)(10*3.5+i);
        System.out.println(b+"\n"+d+"\n"+f+"\n"+h+"\n"+j+"\n"+n+"\n");
    }
}

Operation results:

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-FtBhCa8i-1634382662457)(C:\Users \ Yang Minmin \ appdata \ roaming \ typora \ typora user images \ 1634380566426. PNG)]

In addition to the boolean type, the other seven basic types can be converted to each other

Classification of basic data conversion

Basic data type conversion is divided into default conversion (implicit conversion) and forced type conversion

Default conversion

Convert from basic data type with small capacity to basic data type with large capacity

Force conversion

Convert from basic data type with large capacity to basic data type with small capacity

Possible problems caused by cast

1. Data overflow

When the data overflows, the excess bytes are removed (that is, the overflow part is removed) (the highest bit is the symbol bit)

2. Accuracy loss

In the conversion from floating-point to integer, because the integer can only store the integer part of the value, the decimal part will be lost, resulting in the loss of precision

In mixed operations, the small capacity type will automatically rise to the large capacity type by default

Sort the capacity between the basic data types in the seven (because Boolean variables cannot be type converted, the other seven can be converted to each other)

byte=short=char—>int—>long—>float—>double

Although long variables account for eight bytes and float variables account for four bytes, there are different ways to store floating-point variables and integer variables in the computer. Although long has only four bytes, the capacity of long variables is larger, so type conversion can be carried out from long variables to float variables

Posted by nishanthc12 on Sun, 24 Oct 2021 12:09:03 -0700