The second day of basic Java learning

1, Review content

1, Fundamentals of software development
 1. What is JAVAEE
 2. Common development languages
 2, Construction of development environment
 1. What are JDK and JRE
 2. Install JDK
 3. Configure environment variables
 4. Test whether the environment configuration is successful
 3, Write the first Java program
 1. Common commands
 2. Write Java source program in Notepad
 3. Compile and execute in cmd command window
 4. Program compilation and execution process

2, First Java program extension

1. Detailed code

/**
    author:Rongxi
      time:2021.7.20
*/
​
//The public class is called Hello
public class Hello{
    
    //Main method
    public static void main(String[] args){
        //Output statement ("content")
        System.out.println("HelloWorld...1");
        System.out.println("HelloWorld...2");
        System.out.println("HelloWorld...3");
    }
}
/**
    public-Public class Hello class name
    static-Static void - main method name without return value
    String-String [] - string array
*/
public-Public class-class Hello-Class name static-static state void-With no return value  main-Method name (main method)
String-String array    args-Array name
public static void main(String[] args){} This fixed method is called main method, entry method, main function and entry function. When the function is run, the system will automatically call the main method and execute the code in the main method

2. Code details

1. The class name modified by public must be consistent with the file name
 2.Java is a case sensitive language
 3. Brackets and double quotes must be in pairs
 4. The code in the method is executed from top to bottom - sequential execution
 5. The code in the method is also called execution statement, and the execution statement ends with a semicolon

3, Java coding specification

1. Writing format

Using the tab key makes the code more hierarchical and readable

2. Code comments

Meaning: explain the written code
1.Text notes:
/**
*Prepared by:
*Written on:
*/
2.Multiline comment:
/*
public-Public class Hello class name static static void main method name without return value (main method)
String[]-String array args - array name
*/
3.Single line note:
//Hello!   
 

 

notesdescribe
//Note contentSingle-Line Comments
/*Note content*/multiline comment
/**Note content*/Text Annotation

Note: java file compiled into class file, comments will be ignored during compilation

4, Role of Path

%JAVA_ HOME%/bin--> javac. Exe and Java exe
A folder is configured in path, which means that the files in the configured folder can be opened in any other folder

5, Cross platform features of Java

Java is special and is generally classified as an interpretative language. This language does not belong to the traditional interpretative language or the traditional compiled language. This language compiles the source code into bytecode files (intermediate files / class files) through the compiler. Bytecode files cannot be directly executed by the system, However, it can be interpreted and executed by the Java virtual machine (VM) installed on the platform. In other words, the Java source code is compiled into a bytecode file, which is interpreted and executed by the Java virtual machine on each major platform, realizing the cross platform feature, but the cross platform premise is that the matching Java virtual machine must be installed on the platform. See the figure above, cross platform characteristic diagram

6, Division of Java System

JavaSE -J2SE - Standard Version (extended enterprise version and mini version)
JavaEE -J2EE - enterprise version (technology for back-end development)
JavaME -J2ME - mini version (the technology for front-end development - is outdated and now uses Android and Hongmeng)

7, Development history of Java

employer: sun(Stanford University Network)---2009 $7.4 billion in sales oracle company
Java Father: James·Gosling (James Gosling)
OAK by Java predecessor
 Version change
1995 On May 23, Java The birth of language;
1996 January, the first JDK-JDK1.0 be born;
1996 In April, 10 major operating system vendors announced that they would embed in their products JAVA Technology;
1996 September 2012, about 8.3 Million Web Applications JAVA Technology to make;
1997 On February 18, JDK1.1 release;
1997 On April 2, JavaOne The conference was held with more than 10000 participants, setting a record for the scale of similar conferences in the world at that time;
1997 In September, JavaDeveloperConnection More than 100000 community members;
1998 In February, JDK1.1 Downloaded more than 2,000,000 Times;
1998 On December 8, JAVA2 Enterprise platform J2EE release;
1999 In June, SUN Company release Java Three versions of: Standard Version, enterprise version and micro version;
2000 On May 8, JDK1.3 release;
2000 On May 29, JDK1.4 release;
2001 On June 5, NOKIA It was announced that 100 million support units would be sold by 2003 Java Mobile phone;
2001 On September 24, J2EE1.3 release;
2002 On February 26, J2SE1.4 Published since Java The computing power of has been greatly improved;
2004 September 30, 2018:00PM,J2SE1.5 Publish, become Java Another milestone in the history of language development. To show the importance of this version, J2SE1.5 Renamed Java SE 5.0;
2005 In June, JavaOne The general meeting was held, SUN Company Disclosure Java SE 6. At this point, Java Various versions of have been renamed to remove the number "2": J2EE Renamed Java EE,J2SE Renamed Java SE,J2ME Renamed Java ME;
2006 In December, SUN Company release JRE6.0;
2009 April 7 Google App Engine Start support Java;
2009 On April 20, 2007, Oracle acquired it for $7.4 billion Sun. obtain Java Copyright of;
2010 In November, because Oracle Java The community is unfriendly, so Apache Threatened to withdraw JCP;
2011 On July 28, 2007, Oracle released Java 7.0 Official version of;
2014 On March 19, 2009, Oracle released Java 8.0 Official version of

8, What is IDEA

Meaning: coding environment, software for writing code
 Notepad -- > Notepad + + / EDITPLUS -- > eclipse -- > idea

9, Coding problem

Reason for garbled Code: encoding and decoding do not use the same encoding format
 Solution: set the same encoding format for the two ports
ps: javac -encoding UTF-8 Hello.java
ps: notepad++Set encoding format (select settings)-->be the first choice-->newly build-->code ANSI,Then delete the original file and recreate it (use);

10, Identifier

Meaning: the character sequence used when naming classes, methods, variables and interfaces
 Components: English upper and lower case letters or number or$or_or chinese
 Naming conventions:
    1.Cannot start with a number
    2.Case sensitive
    3.Cannot use except $and_Special symbols other than
    4.Do not use Chinese(Chinese is not allowed for enterprise level naming conventions)
    5.out of commission Java Keywords for
public class identifier {
    public static void identifier (String[] args){
        int identifier =100;
    }
}

11, Keywords

Meaning: a word with special meaning defined by Java in advance
 Experience: don't remember. With the deepening of the later knowledge points, we will master them one by one
Key to defining access modifiers
privateprotectedpublic
Keywords defining class, method, variable modifiers
abstractfinalstaticsynchronized
Keywords that define the relationship between classes
extendsimplements
Define keywords for creating instances and referencing real columns to judge instances
newthissuperinstanceof
Keywords for exception handling
trycatchfinallythrowthrows
Keywords for packages
packageimport
Keywords defining data types
classvoidbyteshortint
longfloatdoublecharboolean
interface
Keywords defining data type values
truefalsenull
Define keywords for process control
ifelseswitchcasedefault
whiledoforbreakcontinue
return

12, Variable

Meaning: writing format of variable that can be changed during program execution: data type variable name [= initial value]; 
be careful: 
1. The contents in brackets can be written or not, and do not violate the syntax rules of variables 
2. = is the assignment number. Assign the value on the right to the variable on the left 
3. Variables are similar to containers, but variables can only store a single value (to store multiple values, consider using arrays and sets)

13, Precautions for variables

1. Variable names cannot be duplicate under the same scope 
2. Variables can only be used after initialization

14, Basic data type

See the java basic data type diagram

 

1. Integer

typebytedigitValue rangeDefault value
byte1810000000(-128)-01111111(127)0
short21616 bit binary range0
int43232-bit binary value range0
long86464 bit binary value range0

2. Floating point type

typebytedigitValue rangeDefault value
float432Positive: 1.4e-45 to 3.4e+38 negative: - 3.4e+38 to -1.4e-450.0
double864Positive: 4.9e-324 to 1.798e+308 negative: - 1.798e+308 to -4.9e-3240.0

3. Boolean

typebytedigitValue rangeDefault value
Boolean432true,falsefalse

Boolean takes up 4 bytes. The bottom layer is represented by int value. 1-true, 0-float CPU processes 32 bits of data each time

4. Character type

typebytedigitValue rangeDefault value
char2160~65535empty

14, Conversion between basic data types

Note: conversion of value type value range of value type: 
 byte<short<int<long<float<double
​
1. Automatic transformation: from small value range to large value range
byte b = 10;
        short s = b;
        int i = s;
        long l = i;
        float f = l;
        double d = f;
        System.out.println(d);//10.0
                
        //Directly convert byte to double
        byte b = 100;
        double d = b;
        System.out.println(d);
2. Forced transformation / forced Transformation: the value range is large, but the value range is small
    Syntax: data type variable = (target type to be forced) variable;
double d = 123.12;
        float f = (float)d;//The variable d of double type is strongly converted to float type, and then assigned to the variable f of float type
        long l = (long)f;
        int i = (int)l;
        short s = (short)i;
        byte b = (byte)s;
        System.out.println(b);
                
        //Directly convert double to byte
        double d = 50.12;
        byte b = (byte)d;
        System.out.println(b);
3. Special points
Case 1:
        //1 is a numeric literal of type int, but 1 is within the value range of type byte, so it is assigned directly
        byte b1 = 1;
        //128 is a numeric literal of int type, but 128 is not within the value range of byte type, so it must be forcibly converted
        //byte b2 = 128;
        //128 int 32-bit: 000000000000000000000000000000000000
        //Strong conversion byte 8 bits: 10000000
        byte b2 = (byte)128;
        System.out.println(b1);//1
        System.out.println(b2);//-128
                    
    Case 2:
        //12345678901 is a numeric literal of type int, but the number exceeds the maximum value range of type int, so an error is reported
        int i = 12345678901;
                    
    Case 3:
        //12345678901 is a numeric literal of type int, but the number exceeds the maximum value range of type int, so an error is reported
        long l1 = 12345678901;
        //12345678901L is a numeric literal of long type, which does not exceed the maximum value range of long type, so no error is reported
        long l2 = 12345678901L; 
            
    Case 4:
        //int to float (auto transition)
        float f1 = 1;
        //double to float (CAST)
        //float f2 = 1.0;
        float f2 = 1.0F;//It is more efficient to directly consider 1.0 as float type
        float f2 = (float)1.0;//Turn double's strong 1.0 to float
        System.out.println(f1);
        System.out.println(f2);
                    
    Case 5:
        char c = 'a';
        //Assign ASCII of character 'a' to variable i
        int i = c;
        System.out.println(i);//97 - ASCII

Definition: converts one basic data type to another

byte  short int long float double char boolean

1. Those data types can be converted to each other

byte,short,int,long,float,double

2. Basis for conversion

Accuracy from low to high:
    byte<short<int<long<float<double
​
Implicit type conversion:
    definition:
        Convert values or variables from low precision to high precision, and the system will execute automatically
        
    byte a=10;
    int  b=10;
    int  c=a; The conversion from low precision to high precision is automatically executed by the system
 Properties: usually, the largest data type in an expression determines the data type of the final result of the expression
    byte a=10;
    int  b=10;
    int  c=a+b; The largest result type in an expression is the final result type
    short d=(short)(a+b);
Explicit type / cast:
    definition:
        To convert a value or variable from high precision to low precision, you must explicitly convert: (type name) the value to be converted
    byte a=10;
    int  b=10;
    byte c=(byte)b; Cast / explicit
    short d=(short)b;
    

Keywords: Java

Added by imawake on Sun, 16 Jan 2022 18:27:53 +0200