We know that in CProgramThe main entry function (usually called the main function, namely the main () function) can obtain the parameters passed to the program by the system. The main function is defined as follows:
# Include <stdio. h> int main (INT argc; char * argv []) {statement;
Return 0 ;}
In C, the form parameter of the main function: The argv pointer array stores the passed parameter pointer, And the argc stores the number of parameters obtained by the main function, that is, argc stores the index of the array argv.
And argv [0] stores the file name of the executable program (this includes the path? Hey, I want to sell it here ).
Is there such a mechanism in Java?
[Java command line parameters]
Java's entry function main also provides the same mechanism as the C language. Let's look at the main definition of the entry function:
/**/Public class class_name {public static void main (string ARGs []) {statements ;}}
As you can see, the main function also has the form parameter: String ARGs []
Here, the parameters passed to the Java program are stored in the form parameter ARGs []. You can see that the form parameter in Java does not include the argc count item? Why?
/* This programCodeTest the for each statement and command line parameter */public class foreach {public static void main (string [] ARGs) {// test the parameters of the for each statement and main function // For (string getmain: ARGs) // system. out. println (getmain); For (INT I = 0; I <args. length; I ++) system. out. println (ARGs [I]);}
The execution result of the program is as follows:
Through the program, we can see that the main function of Java does accept the parameters passed by the system and does not store the name of the Java program. At the same time, we can see that
The argc count can also completely output input parameters. This is the difference between Java and C main functions.
[Java for each loop]
If you have been familiar with VB, you will be familiar with the for each loop structure provided in the VB structure. This loop structure is also provided in Versions later than SDK 5.0 in Java.
The for each loop can be used to process data with set features, such as arrays and object sets. Its syntax structure is:
For (Object object_var: object set) statement;
Execute statement for every object_var object in the Set object set.
We can test this statement:
/* The program code tests the for each statement and the command line parameter */public class foreach {public static void main (string [] ARGs) {// test the for each statement and the main function parameter for (string getmain: ARGs) system. out. println (getmain); // For (INT I = 0; I <args. length; I ++) // system. out. println (ARGs [I]);}
The execution result of the program is as follows:
We can see that the above output result is consistent with that driven by the for loop.
[Return Value of Java program execution]
As you can see, the entry function of the Java program is defined as void, that is, no value is returned, but sometimes we need to return a value. What should we do? Can we be the same as in C?
Define the Java entry function as int type (or other return types )?
Unfortunately, the main function of the Java program must be defined as the void type. Can we return no value to the system?
Although JAVA does not provide the return mechanism of the entry function, the method of the provided system class can be said to achieve this purpose. Syntax:
System. Exit (x); // return value to the system through this method.
The specific instance will not be used.
[Java string type]
String: in Java, a string is a Unicode Character Sequence. Similar to the C language, Java does not have a built-in standard data type to represent strings, but uses
Provide"Pre-defined classesTo support string processing. This class is: String
According to OOP, any string can be considered as an instance of the string class. Java follows the habit of C/C ++.
Double quotation marks are used to represent strings.
Exp:
"ABCDE"; // Java string
String emptystr = ""; // empty string
String greeting = "Hello World"; // string "Hello World"
1. substring
A small string extracted from a string is a substring, including the string itself, which is implemented through the substring method provided by string.
Its syntax is:
Object. substring (index_start, index_end );
Because the index referenced by a string starts from index_start and index_end is the first character that does not need to be extracted, [index_start, index_end) indicates the left-closed and right-open interval,
Therefore, pay attention to it.
The length of the extracted substring is index_end-start_index.
Exp:
String greeting = "Hello World ";
String sub_greeting = greetring. substring (0, 5 );
String sub_sub_greeting = sub_greeting.substring (1, 3 );
In this way, sub_greeting is "hello", while sub_sub_greeting is "El ";
/*This program tests the string class*/Public ClassString {Public Static VoidMain (string ARGs []) {string greeting= "Hello world! "; String sub_greeting= Greeting. substring (0, 5); String sub_sub_greeting= Sub_greeting.substring (1, 3); System. Out. println (sub_greeting); system. Out. println (sub_sub_greeting); system. Exit (0);}}
The execution result is as follows:
2. String concatenation
We know that in C ++, through the operator overload function, the + overload provides String concatenation operations. Does Java have such a mechanism?
Java does not support Operator overloading, but we can still use the + operator to connect two strings.
Exp:
String string_link = "hello" + "" + "world! ";
Here: string_link indicates "Hello World !" This string.
3. unchangeable string
Operations on strings that have been in contact with C/C ++ may cause some misunderstandings here, as we know:
Exp: *. c
Char P [] = "abcdefg ";
P [1] = 'X'; // you can change the content of a string in this way. In fact, the content of "abcdefg" is not changed, but the content of Variable P [1] cannot be changed.
In Java, this mechanism cannot be used to change the content of the string object, because the string object is more like a reference than an array variable.
Exp: *. Java
String STR = "ABC ";
STR [1] = 'X'; // This syntax is incorrect, and the string class of Java cannot be changed; this is similar to char * P = "ABCD" in C/C ++; it cannot pass p [1]
// Change the content of "ABCD.
To change the content of a string, you can use the stringbuffer class.
4. Check whether the strings are equal
Sometimes we need to check whether the strings are equal. How can we check whether the two strings are equal?
A method is provided in the string class to implement this function:Object. Equals (string OBJ );
If the object to be tested is the same as the OBJ object, true is returned; otherwise, false is returned.
Exp:
String STR = "hello ";
Str. Equals ("hello"); // return true here. Of course, you can also test "hello". Equals (STR); similarly, return true.
It must be noted that the equals method is case sensitive, that is, "hello". Equals ("hello"); the returned value is false.
If you do not need to distinguish between small and small sizes, you can use the equalsignorecase method for testing.
Exp:
"Hello". equalsignorecase ("hello"); // the return value is true;
Note:
The = symbol cannot be used to test whether two string objects are equal, although sometimes = can indeed be compared. = Is used to compare two strings
Whether the storage locations are consistent. If they are consistent, the calculation result is true. Otherwise, the calculation result is false.
= The operator can be used to check whether the shared strings are equal. substrings are shared, and the strings obtained by the + or substring method are not shared.
5. String storage space and string length (code points and code units)
We know that the characters in Java are unicode encoded, so that the space of string storage is twice as large as that of the char type.
Generally, code point count is the string storage space count, and code unit count is the string length (that is, the number of characters) count. You can use the following
To obtain the string length:
Int str_length = "Hello world". Length ();
To obtain the code point count, use the following method: codepointcount (start_index, end_index );
Exp: get the bucket size of the string "Hello World.
Int cp_str = "Hello world". codepointcount (0, "Hello world". Length ());
Sometimes we need to obtain the content of the Code unit at the specified position of the string. We can use the: charat (INDEX); method, because the string index starts counting from 0.
So 0 <= index <object. Length-1.
Exp: returns the fourth character of the string "Hello World ".
Char four_in_str = "Hello World ". Charat (4-1); // in this way, four_in_str stores the character 'l'
6. Java string APIs
Java provides a lot of string-class API functions, which are very easy to process strings.
[Summary]
At this point, the third chapter of the book has basically been read, and the next thing is Chapter 4, a rough look, Chapter 4 mainly introduces the content of OOP.
Ah, the learning progress is a little slow. It really takes time.