201771010123 Wanghui and "object-oriented Programming Java" Second week study summary

Source: Internet
Author: User
Tags mathematical functions

Part One of theoretical knowledge

1. Identifiers consist of letters, underscores, dollar signs, and numbers, and the first symbol cannot be a number. Identifiers can be used as: class name, variable name, method name, array name, file name, and so on. Part II: Theoretical Knowledge Learning Section

2. Keywords are some of the words in the Java language that have been given a particular meaning. Common: class, public, try, catch, if, float, import, void, and so on. Keyword does not make a variable name.

3, Java has three kinds of comments://Comments from//until the end of this line.  /* and/* Define a comment block. /** Start, */end this annotation method can be used to automatically generate the document.

4. Java is a strongly typed language. This means that each variable must declare one as a type.

There are 8 basic types of Java: integer type (int, short, Long, byte) – floating-point type (float, double) – character type (char) – Boolean type (Boolean)

Note: There is no unsigned type in java.

6, Character set: Java using Unicode character encoding set, the encoding set 16 bits, containing 65,536 characters.

7, Boolean type: Constant true, false variable definition booleanx;  Booleanx,y; Define simultaneous assignable booleanx=true, Y=false;

Note: In Java, Boolean values and integers cannot be converted to each other.

8. In Java, each variable belongs to a type. When declaring a variable, the type to which the variable belongs is before the variable name.

In Java, a row can declare multiple variables. Declaring each variable individually can improve the readability of the program.

9. After a variable declaration, 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.

In Java, you cannot declare two variables of the same name within the same scope.

10. Java provides two special operators:

(1) New

(2) Instance

11. Mathematical functions are included in the math class.

– Power function – Trigonometric functions – exponential function and its inverse function.

Java also provides two constants. –math.pi–math.e

If you do not want to prefix the math method name with the constant name "math.", you can add the following code at the top of the source file. –import static java.lang.math.*;

12. Most numeric conversions are done automatically by following the priority relationship.

The conversion principle is as follows: if one of the two operands is of type double, the other is converted to a double type. Otherwise, if one of the two operands is of type float, the other will be converted to the float type. Otherwise, if one of the two operands is of type long, the other will be converted to a long type. Otherwise, the two operands are converted to the int type.

13. The syntax for forcing type conversions:

(target type) variable name

Beware of data loss when forcing type conversions.

14. The 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 categories: the immutable string class that will not be modified or changed after creation, and the build string StringBuilder class that allows for changes and changes after creation.

15, the creation of a string object and initialization, you need to call the constructor of the class string, mainly the following creative methods: ①string (): Create an empty string ②string (string value) ③string (char value[]) ④string (Char[],intstartindex,intnumchars) ⑤string (byte[],byte hibyte) ⑥string (Byte[],byte hibyte,intstartidnex,int NumChars)

16. You can get the length of a string by using the length () method in the String class.

You can use the Equals method to detect the equality of two strings. such as s.equals (t); Note: S and T can be string variables or string constants.

You must not use the "= =" operator to detect the equality of two strings! This operator can only determine whether two strings are placed in the same position.

The integer, long, float, and double classes in the Java.lang package provide a corresponding method for converting between strings and values.

The string is converted to a numeric value.

You can convert a numeric value to a string by using the valueof () method defined in the string class.

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.

The object class has a method: ToString () can be used to get the string representation of an object.

17, the StringBuilder class description:

If more than one small string connection is required to create a string, a new string object is constructed each time the string is concatenated, which is time consuming and wasted space. This problem can be avoided by using the StringBuilder class.

18. Read input:

When entering through the console, a scanner object needs to be constructed and associated with the standard input stream system.in. –scannerin=newscanner (system.in);

The scanner class is defined in the Java.util package, so it is necessary to load the corresponding package in.

Because the scanner class input is visible, it does not apply to reading passwords from the console. JavaSE6.0 specifically introduced the console class for this purpose.

19. Using System.out.print (x) to output the value x to the console, this command will print out X for the maximum number of non-0 digits allowed by the data type x corresponds to.

20, the file input and output:

To read a file, you need to construct a scanner object with the Files object. For example: –scanner in = new Scanner (new file ("MyFile.txt")); to write to a file, you need to construct a PrintWriter object, in the constructor, simply provide the file name: –printwriterout = new PrintWriter ("MyFile.txt");

21, Java has 5 kinds of statements:

(1) Method call statement System.out.println ("hello!");

(2) expression statement x=23; i++;

(3) Compound statement

Enclosing some statements with {} makes up a statement.

{z=x+23;

System.out.println ("Hello");

}

(4) Control statements (the process is determined by these languages) if, switch, for, while, Do-while

(5) Packege statement and import statement.

22. Java has two kinds of conditional statements

(1) if statement (2) switch statement

Attention:

(1) The value of the expression value, the constant I, is the integer type, and cannot be a string.

(2) constant I! = constant J

(3) Execution: The expression value is evaluated first, if the same as the constant I, the execution of the statement sequence I; if they are different, then the statement sequence n+1 is executed.

(4) Note the break!! after the case clause

23, the circular statement is divided into three kinds:

(1) while (2) do-while (3) for

24. 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.

Note: (1) There is no goto statement in Java!!! (2) A tagged break statement is provided in Java to jump out of multiple nested loop statements.

25. Return statement:

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.

Format: return value;

26, Large value:

(1) 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.

(2) The BigInteger class realizes the integer operation of arbitrary precision, and BigDecimal realizes the floating-point operation with arbitrary precision.

27. Array:

(1) An array is a data structure that is a collection of ordered data, with the same data type for each element in the array.

(2) The element is determined by the array name and its subscript implementation, such as A[0] represents the first element of array A, a[1] represents the second element of array A, and so on.

28, the Declaration of the array:

One-dimensional array format: array element type array name []; array element type [] array name;//Recommended Use

Two-dimensional array format: array element type array name []; array element type [] [] array name;//Recommended Use

Where the array element type can be any type in Java, including the base type and the composite type.

You can also declare an array using a defined class: point[] line;

27, the Declaration of the array:

(1) In contrast to C + + +, Java does not allocate memory for arrays when they are declared.

(2) In Java, arrays are independent classes and have their own methods.

(3) An array is an object that consists of a basic data type.

28, the creation of data:

(1) After an array declaration, to allocate memory space with the new operator, the length of the array must be specified when allocating memory space.

(2) The format is as follows: array name =new array element type [number] such as: Boy=new float[5]; Int[] Age=new int[10];

(3) When a numeric array is created, all array elements are automatically initialized to 0, the array elements are initialized to false after the Boolean array is created, and the object array is initialized to null.

(4) The size can no longer be changed after the array is created.

29. Initialization of one-dimensional data:

Initialize the elements of an array at the same time that it is declared, for example: int[] smallprimes= {2, 3, 5, 7, 11, 13};

The system automatically calculates the length of the array according to the number of initial values given, and allocates the appropriate space.

In Java, you can initialize an anonymous array. For example: New int[] {17, 19, 23, 29, 31,37}

You can use this syntax form to reinitialize an array without creating a new variable.

30. Initialization of multidimensional arrays:

The system automatically calculates the size of the array based on the initial value.

In the Java language, a two-dimensional array is treated as an array of arrays and an array as an object.

31. Foreach Loop

The FOR Loop statement outputs the program segment for each element of a array, for example: for (inti= 0; i< a.length; i++) System.out.println (A[i]);

JavaSE5.0 adds a highly functional looping structure that can be used to sequentially process each element in the array without having to specify the subscript value, and the following program also has the same effect: for (INTELEMENT:A); SYSTEM.OUT.PRINTLN (Element);

The statement format for this for loop is: for (variable:collection) statement

32. Array Copy:

When Java allows you to copy an array variable to another variable, all two variables point to the same array. Example: float Girl[]=boy;

If you just want to copy the values from one array to another, use the Copyof method of the Arrays class: Int[]copiedgirl=arrays. CopyOf (girl, girl.length);

This method can be used to increase the size of the array: Girl=array. CopyOf (girl, 2*girl.length);

33, Data sorting:

Call the Sort method in the Java.util.Arrays class to sort a numeric array.

This method uses the optimized fast sorting algorithm.

34. Multidimensional Arrays:

In programming, if you need to store two-dimensional tabular data, where each data type is the same, you can use a two-dimensional array.

Assigning values to each element of a two-dimensional array is typically done through nested double loops.

35. Irregular arrays:

Java can use an irregular array, where each row of an array has a different length.

When creating irregular arrays, allocate space for each dimension array starting from the highest dimension.

36. Summary of the array:

To determine the size of an array before it is used, you can dynamically generate the array as needed in your program (such as calculating the size with an expression).

Once an array is generated, the size cannot be changed. The variable-length array is not supported in Java. The array's properties-length: The number of array elements.

An array in Java, as an object type, can be used as a parameter to a method, and a method call simultaneous a reference to an array.

ArrayIndexOutOfBoundsException exception.

Part II: Experimental part

(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 basic Grammar of the basic program of Java language construction, such as data type, variables, operators, various expressions, input and output, and process control;

(4) Mastering the use of the string class, the StringBuilder class, and the array class.

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)

Write the program in Eclipse with the program and the results as shown:

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;

Write the program in Eclipse with the program and the results as shown:

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 changed program and results are as follows:

The difference:The string itself is immutable, it can only be assigned once, each time the content changes, will generate a new object, and then the original object refers to the new object, and each time the generation of new objects will have an impact on the system performance, which will be reduced. NET compiler to work efficiently.

And the StringBuilder class is different, each operation is to operate on their own objects, rather than generate new objects, the space will be expanded as the content increases, so that when doing a lot of modifications, do not generate a large number of anonymous objects to affect system performance.

When you need to do a lot of work on a string in your program, you should consider applying the StringBuilder class to handle the string, which is designed to be an improvement against a large number of string operations, to avoid creating too many temporary objects, and when the program is just one or several operations on a string , you can use the String class.

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 ("!");

}

}

The results of the operation are as follows:

Lab 5: In the Eclipse Environment, import the 3rd chapter of the sample program Inputtest.java steps:

(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.

The results 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 program and results 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;

(2) test Bigintegertest.java, master the use of large numerical classes;

(3) test Lotterydrawing.java, master the use of arrays;

(4) test Compoundinterest.java, master the use of multidimensional arrays;

(5) Test Lotteryarray.java, master the use of irregular arrays.

Third, the experimental summary:

Through this experiment, we learned about Java data types, variables, type conversions, operator precedence and binding, input and output, multidimensional arrays, and other related knowledge. Learn some of the differences between Java and C: for example, character-based Java uses the universal code. In the experiment, I mastered the import of files into Eclipse and mastered some programming-related knowledge, such as: the difference between string and StringBuilder.

In the name of the name of the class can not be preceded by a space, the beginning of the identifier can not be a number, through the experiment to understand the rigor of Java, punctuation must be, and is in English state. In the experiment encountered a lot of problems, through the seniors and the teacher's explanation finally solved. It also makes me realize that the Java language needs to be more hands-on to the code, to think more.

201771010123 Wanghui and "object-oriented Programming Java" Second week study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.