JAVA basics (keywords - identifiers - comments - constants, hexadecimal and hexadecimal conversion - variables - data types and type conversions)

1. Keywords

1. An overview of keywords

words given special meaning by java language

2, the characteristics of keywords

The characters that make up the keywords are all lowercase
. The advanced notepad software similar to notepad++ has special color marks for keywords.

Second, the identifier

1. Identifier Overview

It is the sequence of characters used when naming classes, interfaces, methods, variables, packages, etc.

2. Composition rules

  • English uppercase and lowercase letters
  • numeric characters
  • $ and _

3. Pay attention

  1. cannot start with a number

  2. cannot be a keyword in java

  3. case sensitive

  4. To be well-known
    example

 //Define a student class 
      class  Student {
   
   }
  • 1
  • 2
  • 3
  • 4

4. Package

  • In fact, it is a folder, which is used to distinguish the same class names for classification and storage.
  • All lowercase when naming packages
  • Category:
    single-level bag momo
    multi-level bag com.momo
     com
      -momo

5, class or interface (big hump)

  • One word: The first letter of the word must be capitalized
    Student Dog Cat…
  • Multiple words: Capitalize the first letter of each word
    HelloWorld MeiNv…

6, method or variable (small camel case)

  • One word: word lowercase
    int age String name max() min()
  • Multiple words: start with the second word, capitalize the first letter of each word
    int studentAge
    String studentName
    showAll()

7, constant

One word: all caps
PI AGE NAME

Multiple words: capitalize each letter of each word, separate each word with _
MAX_AGE MIN_AGE STUDENT_MAX_AGE

class  Demo { 
	public  static  void  main ( String [ ] args ) { 
		//correct 
		int age =  18 ; 
		//int 2age = 20; //incorrect 
		int _age2 =  20 ; 
	    // illegal expression start 
	    //int public = 55;
	
	    int Public = 66;
	
	    int MAX_AGE = 777;
	    System.out.println(age);
	    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3. Notes

1. Annotation Overview

Text used for program explanations

2. Classification of annotations in java

  • Single line comment
    Format: //Comment content
  • Multi-line comments
    Format: /* Comment content*/
    Note: cannot be nested
  • Documentation Comment
    Format: /** Comment content*/

3, role

  • explain procedure
  • Improve the readability of the program
  • Can help us with simple tuning programs (advanced tunings follow)
//This is the comment case demonstration 
class  Demo2 { 
	//Here is the main method 
	//It is the entry point of the program 
	//Called by jvm 
	public  static  void  main ( String [ ] args ) { 
		//I want to output I am a handsome guy 
		//Need An output statement 
		System . out . println ( "I am a handsome guy" ) ; 
		System . out . println ( "I am a handsome guy" ) ; 
		System . out . println ( "I am a handsome guy" ) ; 
		// System.out.println("I am a handsome guy");
		//System.out.println "I'm a handsome guy"); 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4. Documentation comments

Documentation comments are unique to java and can be interpreted by the javadoc command to generate a set of documentation in the form of web pages (described later).

5. Pay attention

  • Comments are a good habit that an orderly programmer must have
  • When you write code in the future, write comments first and then write code
  • Arrange your ideas with comments first, and then use code to reflect them. Code is just a concrete manifestation of ideas.

Fourth, constants

1. Constant overview

Its value cannot be changed during program execution

2. Classification

literal constant


  • String constants: "helloworld" "123" "abc" enclosed in ""…
  • ​ Integer constants: all integers​ 1, 111, 11111,
    65464
  • ​ Decimal constants: all decimals
    ​ 1.11 3.14 5.2666
  • ​ Character constant: single content enclosed in single quotes
    ​ 'a' '0'
    'abc' This is wrong
  • ​ Boolean constants: special only true and false
  • ​ Empty constant: null
class  Demo3 { 
	public  static  void  main ( String [ ] args ) { 
		// output of string constant 
		System . out . println ( "I'm a handsome guy" ) ; 
		// output of integer constant 
		System . out . println ( 123 ) ; 
		// output of decimal constants 
		System . out . println ( 1.23 ) ; 
		// output of character constants 
		System . out . println ('a' ) ; 
		System . out . println ( 'A' ) ; 
		System . out . println ( '0' ) ; 
		// output of boolean constants 
		System . out . println ( true ) ; 
		System . out . println ( false ) ; 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3, Java provides 4 representations for integer constants

  • binary
  • Octal
  • decimal
  • hex

4, base

  • Overview: The hexadecimal is the carry system, which is a carry method

  • For any kind of base, the x base means that the number operation at a certain position is to carry one digit every x.

    Binary every two into 18 every octal into 1 decimal every 10 into a hexadecimal every hexadecimal every hexadecimal.

5. The origin of binary

  • Any data in a computer exists in binary form
  • Binary early evolved from electrical signal switches
  • An integer is also binary in its content, but it is very troublesome to use a string of 1 or 0 values.
    insert image description here

6. Data composition of different bases

  • Binary: Consists of 1s and 0s starting with 0b
  • Octal: 0, 1, 2...7 starting with 0
  • Decimal: 0, 1, 2, 3.. . . 9 Integer defaults to decimal
  • Hexadecimal: Consists of 0,1,2…9,abcdef, starting with 0x
//Data in different bases form 
class  Demo4 { 
	public  static  void  main ( String [ ] args ) { 
		System . out . println ( 100 ) ; // Default decimal 
		System . out . println ( 0b100 ) ; // Binary 
		System . out .println ( 0100 ) ; // Octal 
		System .out .println ( _ _0x100 ) ; //hex 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

7, base conversion

  • Other bases to decimals: coefficient * base weight powers added
    insert image description here

    - Exercise: Calculate decimal
    0b101 of the following data: 8 2 +8 0 =65

  • decimal to other bases
    decimal to other bases

    - Exercise: Get the binary, octal, hex
    55
    88 of the data below

  • Fast conversion
    between decimal and binary 8421 code can realize fast conversion between decimal and binary. It is the name of the mainland and is the most commonly used type of BCD code.
    Each 1 in the 8421 code has a fixed value.
    Add up the decimal values ​​represented by the 1 in each digit to get the final decimal result
    1 1 1 1 1 1 1 1
    128 64 32 16 8 4 2 1

  • Binary and octal, hexadecimal can use decimal as a bridge to split first, in combination
    - any conversion from x to y, how to convert?
    x base-------decimal
    decimal---------y base

8. Representation of signed data

In the calculation, there are 3 ways to represent the signed data: the original code, the inverse code, and the complement code.
All data are calculated using the complement code.

  • Original code: It is the binary representation of data, the highest bit is the sign bit, 0 means positive, 1 means negative, and the rest are numeric bits

  • Inverse code: The inverse code of a positive number is the same as the original code, and the inverse code of a negative number is to invert the original code one by one (0 becomes 1, 1 becomes 0) except for the sign bit

  • Complement code: The complement code of positive numbers is the same as the original code, and the complement code of negative numbers is the addition of 1 based on the inverse code.
    insert image description here

    Exercise: Suppose the original code of a data x is 1011 0100 Find the complement and complement of x

​ Inverse code: 1100 1011

​ Two’s complement: 1100 1100

​ Exercise: Suppose the complement of a data x is 1011 0100 to find the original code of x

​ Inverse code: 1011 0011

​ Original code: 1100 1100

V. Variables

1. Overview of variables

The amount by which its value can change within a certain range during program execution

2. Composition rules

  • It must be qualified, using the data type
  • When we are operating, it is impossible to use some of this space to operate. What we really want to use is the value in this space, so we give the space a name----- variable name
  • The type is there, the name is there, but if there is no value in this space, it is meaningless, it is a garbage space, and it needs to be given an initialization value

3. Variable definition format

  • data type variable name = initialization value format is fixed
  • Essentially, a variable is a small area in memory that is accessed using the variable name. Therefore, each variable must be applied (declared) and then assigned to be used before it can be used.
  • Why define variables?
    Used to keep constants of the same type and can be reused.

Six, data types

1. Java is a strongly typed language

For each type of data, a clear specific data type is defined
, and space of different sizes is allocated in memory

2, data type classification

4 types of basic data types and 8 types

Numerical

The range of bytes occupied by integer types
​byte 1 -128 to 127
​short 2 -2 15 to 2 15 -1
​int 4 -2 31 to 2 31
​long 8 -2 63 to 2 63 -1
​Floating point Type
​float 4
double 8

character type

char 2

boolean

​ boolean 1

  • Integers are of type int by default, floating-point types are of type double by default
  • L or l should be used after the long integer. It is recommended to use L
  • Single-precision floating-point data should be marked with F or f. It is recommended to use
    F
class  Demo5 { 
	public  static  void  main ( String [ ] args ) { 
		//define a variable data type variable name = initial value 
		//define a byte type 
		byte b =  11 ; 
		System . out . println ( b ) ; 
		System . out .println ( 11 ) ; 
		b = 22 ; 
		System . out . println ( b ) ; _ 
        //define a short integer type 
        short s =  111 ; 
        System . out . println ( s ) ;

        //define an integer type 
        int i =  1111 ; 
        System . out . println ( i ) ; 
        // error: too large integer 
        // i = 1111111122222; 
        System . out . println ( i ) ;

        //define a long integer type 
        //long l = 11111; 
        //long l = 1111111122222;// oversized integer 
        long l =  1111111122222 L ; 
        System . out . println ( l ) ;

        //define floating point data 
        // error: incompatible types: conversion from double to float may cost 
        float f =  12.13F ; 
        System . out . println ( f ) ;

        double d = 12.13;
        System.out.println(d);

        // character variable 
        char ch =  'a' ; 
        System . out . println ( ch ) ;

        // boolean 
        boolean boo =  true ; 
        System . out . println ( boo ) ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

- Reference data type

​ class class
​ interface interface
​ array[]

3. Notes on variables

  • Scope
    In which level of curly brackets the variable is defined, the scope of the brackets is the scope of the variable
  • Variables with the same name cannot appear in the same scope
  • Initial
    value A variable without an initial value cannot be used
    , as long as the value is given before use, not necessarily when it is defined
  • It is recommended to define a variable above on a line.
    You can define more than one variable, but it is not recommended
class  Demo6 { 
	public  static  void  main ( String [ ] args ) { 
		//define a variable 
		int x =  100 ; 
		x =  111 ; 
		//int x = 111; error: variable is already defined in method main(String[]) x 
		System . out . println ( x ) ; 
		{ 
			System . out . println ( x ) ; 
        int i ; 
            // just give the value before 
            i = 55 ; 
            // error: variable i may not have been initialized 
            System . out . println ( i ) ; 
        } 
        // error: symbol not found 
        //System.out.println(i); 
        // More than one can be defined on a line, but Deprecated 
        //int a = 5;int b = 6;int c = 7; 
        int a =  5 ; 
        int b =  6 ; 
        int c =  7 ;

        //int ii;int jj; 
        //int ii,int jj;wrong 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
/*
+ is an operator that can do addition

Generally speaking, when we operate, the data types involved in the operation must be consistent.
Inconsistency is also possible, but there will be a type conversion process
Note: boolean types cannot be converted to other types

Default conversion (small to large conversion)
	byte,short,char------int----long----float---double
	There is no conversion between byte, short and char. When they participate in the operation, they will be converted to int type first.
*/ 
class  Demo7 { 
	public  static  void  main ( String [ ] args ) { 
		// Directly output 2 constants for addition operation 
		System . out . println ( 5 + 6 ) ;
		
        //2个int类型
        int x = 5;
        int y = 6;
        int z = x+y;
        System.out.println(x+y);
        System.out.println(z);

        // a byte, an int 
        byte b =  6 ; 
        int i =  6 ; 
        System . out . println ( b + i ) ; //12 
        // error: incompatible types: conversion from int to byte may cost 
        / /byte c = b+i; 
        int c = b + i ; 
        System . out . println ( c ) ;

        byte bb =  5 ; 
        short ss =  3 ; 
        // incompatible types: conversion from int to short may cost you 
        // short sss = bb+ss; 
        int sss = bb + ss ; 
        System . out . println ( sss ) ; 
        System . out . println ( bb + ss ) ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

4. Type conversion

  • boolean types cannot be converted to other types

  • Default conversion (conversion from small to large)
    byte, short, char------int----long----float—double
    byte, short, char are not converted between them, they will all be used when they participate in the operation Convert to int first

  • coercion (large to small conversion)
    target type variable name = (target type) (data to be converted)

  • Note: Do not use casts casually, as it may lose precision

class Demo8{
	public static void main(String[] args){
		byte a = 3;
		int b = 4;
		
        //int c = a+b;//There is definitely no problem// 
        System.out.println(c);

        byte aa =  7 ; 
        // byte cc = a+b;//this 
        is a problem // you can use
         
        cast byte cc =  ( byte ) ( a + b ) ;
    

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • Let's see if there is any problem with the following:
    double d = 12.345;
    float f = d;

  • Let's see if there is any difference below
    float f1 = (float)12.345;
    float f2 = 12.345F;

class  Demo9 { 
	public  static  void  main ( String [ ] args ) { 
		//double d = 12.345; 
		//float f = (float)d; 
        float f1 =  ( float ) 12.345 ; 
        float f2 =  12.345F ; 
        //f1 is passed The double type is coerced from 
        //f2 itself is a float type 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
/*
  byte b1 = 3,b2 = 4,b;
  b = b1+b2;
  b = 3+4;
  Q, that sentence will fail to compile? why?
   b = b1+b2; this has a problem,
   This is the addition of variables, look at the type first, and also consider the type when assigning the final result
   To add constants, do the addition operation first, and then check whether the result is within the type range of the assignment.
*/ 
class  Demo9 { 
	public  static  void  main ( String [ ] args ) { 
		//Defines 3 byte-type variables b1, b2, b1 The value of b1 is 3, the value of b2 is 4, and b has no value 
		byte b1 =  3 , b2 =  4 , b ; 
		//b = b1+b2; error: incompatible types: conversion from int to byte may cost 
		you //b = 3+4;//add the calculation result 7 first to see if it is byte or not No error is reported in the range 
		//byte bb = 3+4;//7 byte bb = 7; 
	System . out . println ( b ) ; 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
/*
Q: byte b = 130; is there any problem? If so, how can I make it not report an error? What is the result? why?
analyze:
	To know why the result is -126, we need to know how it is calculated
	As we said earlier, the data is calculated using the complement code. To obtain the complement code, the original code must be obtained first.
	The original code is simply the binary representation of the data
	130 in binary: 00000000 00000000 00000000 10000010
	Because it is a positive number, even if the original code is the inverse code, it is also the complement code.
	

Then we performed a forced conversion (byte occupies one byte), so we intercepted,
		sign bit value bit
 One's complement 1 0000010 The sign bit is 1 to indicate a negative number
 One's complement 1 0000001
 Original code 1 1111110 - 64+32+16+8+4+2= -126
*/ 
class  Demo10 { 
	public  static  void  main ( String [ ] args ) { 
		//The range of byte is -128 to 127 130 is out of range, so an error is reported 
		//byte b = 130;// error: incompatible types: from int to byte conversion may be lossy 
		// use cast 
		byte b =  ( byte ) 130 ; 
		System . out . println ( b ) ; //-126 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • Exercise: byte b = 330;

  • Characters involved in operations (ASCII table)

/*
 With this simple demonstration, characters can be added to integers and the result is an integer
 For the society? Because each character actually has a corresponding value
 This value is in the ASCII table
	After simply looking at this table, we need to remember 3
	'a'   97
	'A'   65
	'0' 48


*/ 
class  Demo12 { 
	public  static  void  main ( String [ ] args ) { 
		// print a character 
		System . out . println ( 'a' ) ; //a 
		// print a character and add an integer 
		System . out . println ( 'a' + 1 ) ; //98 
	} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • ***String participates in operation
/*
String data and other data do +, the result is a string
The + here is not a + operation, but a string concatenation
*/ 
class  Demo11 { 
	public  static  void  main ( String [ ] args ) { 
		//output a string 
		/*
		System.out.println("aaa");
		System.out.println("aaa"+111);
		System.out.println(111+"aaa");
		System.out.println(11+"aaa"+11);
		System.out.println(11+22+"aaa"+111);//33aaa111
		System.out.println("aaa"+true);
		*/
		System.out.println("hello"+'a'+1);//helloa1
		System.out.println('a'+1+"hello");//98hello
	
        System.out.println("6+6="+6+6);//6+6=66
        System.out.println(6+6+"=6+6");//12=6+6
        int a = 5;
        int b = 6;
        System.out.println("a="+a);
        System.out.println("b="+b);
        System.out.println("a="+a+",b="+b);

        } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Related: JAVA basics (keywords - identifiers - comments - constants, hexadecimal and hexadecimal conversion - variables - data types and type conversions)