Part I: Theoretical Knowledge learning Section
Java's BASIC program design structure
A simple Java application
annotation data type variable operator string input output control flow large Data array
3.1 Basic knowledge
Identifier
Identifiers consist of letters, underscores, dollar signs, and numbers, and the first symbol cannot be a number. L The following are legal identifiers: Hello, $1234, program name, www_123 l
Identifiers can be used as: class name, variable name, method name, array name, file name, and so on.
Key words
Keywords are words that have been given a particular meaning in the Java language.
Common: Class, public, try, catch, if, float, import, void, and so on.
Keyword does not make a variable name.
Comments
There are three ways to annotate Java: 1. Note the content is from//until the end of this line. 2./* and * * Define a comment block. 3./** Start, */end this annotation method can be used to automatically generate the document.
3.2 Data types
Java is a strongly typed language. This means that each variable must declare one as a type.
In Java, there are 8 basic types.
– the integer type (int, short, long, byte) is used to represent numeric values that do not have fractional parts.
– Floating-point types (float, double) are used to represent numeric values that have a decimal part.
– The character type (char) is used to represent a single character.
– Boolean Type (Boolean) Boolean type has two values: false and True to determine the logical condition. Integer and Boolean values cannot be converted to and from each other
There is no unsigned type in java.
3.3 Variables
In Java, each variable belongs to a type. When declaring a variable, the type to which the variable belongs is before the variable name.
–double salary;
–int vacationdays;
–long earthpopulation;
–boolean done;
In Java, a row can declare multiple variables. Declaring each variable individually can improve the readability of the program.
–int I,j;//both is INTEGERSL
Variable initialization
Once a variable is declared, it must be explicitly initialized with an assignment statement-never use the value of an uninitialized variable.
In Java, you can make variable declarations anywhere in your code.
The declaration of a variable is as close as possible to the place where the variable was first used, which is a good program writing style. L in Java, you cannot declare two variables of the same name within the same scope.
Constant
In Java, the keyword final is used to indicate constants.
The keyword final indicates that the variable can only be assigned once. Once assigned, it is not possible to change the value again.
In Java, you often want a constant to be used in more than one method in a class, which is often referred to as a constant.
3.4 operator
Various operators: related to object-oriented operations
Arithmetic operators
The increment operator and the self-decrement operator
Relational operators
logical operators
Bitwise operators
Precedence and binding of operators
Mathematical Functions and constants
3.5 Type Conversions
3.6 string
A Java string is a sequence of Unicode characters that is the basic data structure for organizing characters, similar to a character array.
There is a built-in string type, but a Java-predefined class string is provided in the standard Java class library. In Java, strings are treated as objects.
The strings that need to be used in a program can be divided into two main categories:
– A string class of immutable strings that cannot be modified or changed after creation;
– Build string StringBuilder class that allows to make changes and changes after creation.
All classes are specified considered to be subclasses or indirect subclasses of the object class in the Java.lang package----kindred ancestry, and all classes can enjoy some basic functions.
3.7 Input and output
Read input
Formatted output
File input and output
3.8 Control Process
Conditional statement: If statement l switch statement
Loop statement: While L Do-while l for
Interrupt Control Flow statement:
Break action: Ends the execution of the entire loop and goes to the next statement.
Continue effect: End this cycle and start the next cycle.
Return statement: Return value
The return statement for Java is closely related to the Java approach, and when the program executes to this statement, it returns to the previous-level method immediately.
L
3.9 Large values
If the basic integer and floating-point data do not meet the required precision, then you can use the two classes in the Java.math package, BigInteger and BigDecimal. These two classes can manipulate arbitrary long numbers.
The BigInteger class realizes the integer operation of arbitrary precision, and BigDecimal realizes the floating-point operation with arbitrary precision.
3.10 Arrays
An array is a data structure that is a collection of ordered data, with the same data type for each element in the array.
The determination of an element is implemented by the array name and its subscript, such as A[0], which represents the first element of array A, a[1] represents the second element of array A, and so on.
Part II: Experimental part
1. Experiment name: Experiment two Java BASIC program design structure.
2. Purpose of the experiment:
(1) Further familiar with the basic steps of Java Program Development in command line and IDE two ways;
(2) Mastering the process of importing Java source program under Eclipse integrated development environment;
(3) Mastering the Java language constructs the basic program the data type, the variable, the operator, each kind of expression, the input, the Process control
3. Experimental Steps and Contents:
Experiment 1: write the Java application and output the values of the following 4 expressions.
int i=1; Double d=1.0; (1) 45+45*50%i-- (2) 1.5*3+d++ (3) (true) && (3>4) (4) (i>0) | | (i<0) |
The program is shown below
The results of the above procedure are as follows
Experiment 2: Write a Java application that contains the following code fragment, outputting the value of the String class object S3.
String s1= "hello!"; String s2= "World"; String s3=s1+s2; |
The program is shown below
The results are as follows
Experiment 3: change experiment 2 s1, S2, S3 as StringBuilder class object, observe the program running results and compare with experiment 2 results, understand the difference between the string class object and the StringBuilder class object.
The program is shown below
The results are as follows
Experiment 4: run the following program in command-line mode to understand the use of Java Application command-line parameters.
public class Message { public static void Main (string[] args) { if (Args[0].equals ("-H")) System.out.print ("Hello"); else if (args[0].equals ("-G"); System.out.print ("Goodbye,"); for (int i=1;i<args.length;i++) System.out.print ("+args[i"); SYSTEM.OUT.PRINTLN ("!"); } } |
Results
(1) New Java project such as:
(2) Select File->import->file ystem->next, open the File import window such as, click on the above browse select Import source program and select, click on the below browse to select the source program import location for the new project inputtest/ SRC location, click Finish to complete the import.
(3) Open the default package of the Inputtest Project Src folder, and double-click Inputtest.java to open the file in the IDE's source program editing area.
(4) Right-click on the Inputtest.java file name to open the shortcut menu, choose Run As->java application running the program, in conjunction with the program run results, understand the code scanner class object usage, master the Java console input method.
First create a new text document, write the corresponding program in Notepad, save the D-Drive folder to Inputtest.java and then import the document. Import into Eclipse to run the program and produce the appropriate results.
The import is shown below
After importing, the program looks like this
The results of the program run are as follows
Experiment 6: Follow the procedure of Experiment 5, Import Writereadfiletest.java sample program, understand program code with program running result, observe the contents of file MyFile.txt under project folder, master the input and output operation of file.
The results of the import program run according to experiment five are as follows
Experiment 7: Follow the procedure of Experiment 5, import the 3rd Chapter sample program, each sample program summarizes the learning content from the syntax and algorithm two angles.
(1) test Retirement.java,retirement2.java,lotteryodds.java to master the cyclic control structure;
The results of the Retirement.java are as follows
The results of the Retirement2.java are as follows
The Lotteryodds.java results are as follows
(2) test Bigintegertest.java, master the use of large numerical classes;
Experimental results:
(3) test Lotterydrawing.java, master the use of arrays;
Experimental results:
(4) test Compoundinterest.java, master the use of multidimensional arrays;
Experimental results:
(5) Test Lotteryarray.java, master the use of irregular arrays.
Experimental results:
4. Experiment Summary:
Through the study of this chapter, I learned the basic knowledge of identifiers, keywords and annotations, and learned the contents of data types, variables, operators, type conversions, strings, input and output, control flow, large values, arrays, etc.
Get familiar with the basic steps of Java program development in two ways, command line and IDE. Master the process of importing Java source programs in the Eclipse integrated development environment, mastering the data types, variables, operators, types of expressions, input, and process control of the Java language Construction Basic program.
Good text to the top concern me to collect the article
201771010124 Wang Haijian "Object-oriented Programming (Java)" Second week study summary