Java knowledge point

Source: Internet
Author: User

 

Input/Output and control processes

1. Input and Output

1.1 console input: enter a token object in the console and associate it with the standard input stream System. in.

For example, callback in = new callback (System. in );

1.2 you can use nextLine of iterator to read a row of data, use next to read a string, and use nextInt to read an integer.

1.3 keystore does not apply to password reading from the Console. java SE 6 introduces the Console class for this purpose.

For example, Console con = System. console ();

Char [] password = con. readPassword ("password:"); // to ensure security, place the password in the character array instead of the string. After processing, overwrite the array element with the filling value immediately. However, it is not as convenient as limit. Only one row of data can be read at a time, instead of one value or word.

1.4 formatted output: java SE5.0 follows the printf method in the C language library function.

For example, System. out. printf ("a is % d, B is % s", age, name );

1.5 The following parameter indexes start from 1. In the above example, the age index is 1 and the name index is 2. You can use the <flag to use the previous parameter.

For example, System. out. printf ("% 1 $ s % 2 $ tB % 2 $ te % 2 $ tY", "Due date:", new Date ());

System. out. printf ("% s % tB % <te % <tY", "Due date:", new Date ());

Output: Due date: February 9 2004

1.6 you can use the static String. format method to create a formatted String.

For example: String message = String. format ("a is % d, B is % s", age, name );

1.7 input and output of Files

1.7.1 to read files, you need a File object to construct a readable object.

Example: Sanner in = new partition (new File ("myfile.txt "));

★You can construct the delimiter of a string, but the string is interpreted as data rather than the file name.

1.7.2 to write data to a file, you need to construct a PrintWriter object. You only need to provide the file name.

Example: PrinterWriter out = new PrinterWriter ("myfile.txt ");

★If you use a non-existing file to construct a pair or a file that cannot be created to construct a PrinterWriter object, an exception occurs.

2. Control Process

2.1 block scope: several simple java statements enclosed by curly braces. It determines the scope of the variable; one block can be nested in another block. In addition, the same variable cannot be declared in the nested two blocks. This c ++ variable is different.

2.2 use conditional statements, cyclic statements, and so on. You can use blocks to place multiple statements where the original statement is placed.

2.3 use loops to check whether two floating point numbers are equal.

For example, for (double x = 0; x! = 10; x + = 0.1) // The loop is an endless loop, because 0.1 cannot be accurately expressed in binary.

2.4 If a variable is defined inside the for statement, it cannot be used outside the loop.

For example, for (int I = 0; I <10; I ++) {} // I cannot be used externally. If used, it must be defined outside the loop.

2.5 another independent loop can define variables with the same name

For example: for (int I = 0; I <10; I ++) {} for (int I = 11; I <20; I ++) {} // This is allowed

2.6 for the switch statement, if there is no break statement after the case, it will continue to trigger the processing of the next case. This is very dangerous, so avoid using the switch statement.

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.