Tag: Case Pac Ase char method string match trim in put file
Representation of string classes and characters in 1.Java
2. Distinguishing between String,stringbuilder and StringBuffer
3. Passing parameters to the main method from the command line
4. File Operation
1 strings and characters in Java
1.1 String class
A string is a sequence of characters that is stored in an array in many languages, such as the C Language. Now using Java's idea, it is necessary to think of the string as an object, in which the Java-defined string class has 11 construction methods, there are at least 40 methods of implementation, through the learning of the string class, can well understand object-oriented class and object Ideas.
1.2 Construction of a class
String newstring = new String (stringliteral);
String Newstr = new string ("Hello java! ");
This syntax allows you to construct a string class that holds references to objects that store string Values. Strictly speaking, string variables, string objects, string values are not the same, but most of the time these differences are Ignored. But according to the above example of understanding the NEWSTR variable points to a storage "hello java" data area, at this time if the NEWSTR = "I do like Java", at this time the operation and the previous programming idea is not the same, when the original data "Hello java" is still in, Just create a string of "I do like Java" and then point the variable to the String. Take a look at the example:
public class Welcome {public static void main (string[] Args) { System.out.println ("Welcome to java!" ); String S1 = "Welcome to Java"; String s2 = new string ("Welcome to Java"); String s3 = "Welcome to Java"; System.out.println ("s1 is" + s1); System.out.println ("s2 is" + s2); System.out.println ("s3 is" + s3); System.out.println ("s1 = = S2 is" + (s1==s2)); System.out.println ("s1 = = S3 is" + (s1==s3));}}
The results of the run can be seen s1,s2,s3, the string variable points to the same value "Welcome to Java" but 1 and 3 are the same string, 2 and the other two are Not. "= =" simply means that two string variables are not pointing to the same value, and you can use +equals (string2) to verify that the contents of two characters are not the Same. The same string class, which provides equalsignorecase,comparetoignorecase and Regionmatches methods to compare strings.
Properties of the String: the string class has a method to read the string length and get a character, or you can concatenate two strings. Based on the discrete math search algorithm string matching problem, Java string also has a related method to the problem in place, get substring +substring (beginindex:int): string,+substring (beginindex:int, Endindex:int): String. The same string can also be converted to uppercase and Lowercase.
Conversion of strings and character arrays, as shown in the following example:
char[] chars = "Java". ToCharArray ();
2. Distinguishing between String,stringbuilder and StringBuffer
Stringbuilder/stringbuffer class
Stringbuilder/stringbuffer is optional for string, and in general, Stringbuilder/stringbuffer can be used in the case where string can be used, and these two classes are more flexible than string , the string after the object that should be generated for the string class is fixed, and does not add content like stringbuilder/stringbuffer, for multi-tasking cases where StringBuffer is used, for a single task, Use Stringbuilder.
StringBuilder
StringBuilder has three construction methods, +stringbuilder (), +stringbuilder (capacity:int), +stringbuilder (s:string).
Tostring,capacity,length,setlength and Charat methods
StringBuilder provides an additional way to implement a string and get its contents:
+tostring (): String object returned from StringBuilder
+capacity (): int Gets the contents of StringBuilder
+charat (index:int): Char Returns a single character from the Inex position
+length (): int Returns the length of a string
+setlength (newlength:int): Void
+substring (startindex:int): String
+substring (startindex:int, endindex:int): String
+trimtosize (): Void
Note: the length of the String, which is the actual length of the bulider, is the length of the capacity is now
3. Passing parameters to the main method from the command line
1. Pass the parameters to the main method, and when you are programmed to pass in from the command line, you can follow the example below
Java testmain arg0 arg1 arg2
These parameters are strings, but they do not appear in the command line two times, they are separated by a space, when the main method is called the Java interpreter created an array to store the command line parameters, and passed the parameters to Args. For example, you invoke an N-parameter program, and the Java interpreter creates an array as follows
args = new string[n];
The Java interpreter then passes args to invoke the main method.
Note: if the string is not passed, but the program is run, then the array will be created as new string[0], in which case the length of the array space-time is 0,args reference an empty array, so args is not null, but args.length = 0;
4. File Operation
About the operation of the file class. Data exists in variables, arrays and objects, but these are temporary, when the program is interrupted, the data disappears, in order to permanently store these data created in the program, you need to put these data in a file, and then put these files on disk or cd, files can be passed, These files can also be accessed by subsequent Programs.
Each file is placed directly in the file system, and the absolute filename contains the file name and all path names, which can be seen at the operating system Level. From a certain point of view, the Java Virtual machine can also be regarded as some kind of operating System.
File class
Method: +file (pathname:string), +file (parent:string, child:string), +file (parent:file, Child:string)
+exists (): Boolean +canread (): boolean +canread (): boolean +canwrite (): Boolean
Input and output of files
The file object encapsulates the contents of the files or the path of the file, but does not contain the method to create the file, or read or write from the file, in order to implement the input and output, you must create the object, and then use the Java IO class, which contains the file read and write METHODS.
Write Data using PrintWriter
The Java.io.printWriter class can be used to create a file and write data to the file, creating the PrintWriter object first
PrintWriter output = new PrintWriter (filename);
Then you can use the print, println and printf methods.
Writedata.javapublic class WriteData {public static void main (string[] Args) throws Exception {java.io.File File = New Java.io.File ("scores.txt"), if (file.exists ()) {System.out.println ("File already exists"); System.exit (0);}//Create a file java.io.PrintWriter output = new Java.io.PrintWriter (file);/ Write formatted out Put to the Fileoutput.print ("John T Smith"); output.println;output.print ("Eric K Jones"); o Utput.println;//Close The fileoutput.close ();}
Reading data through the scanner Class: the Java.util.Scanner class is used for reading strings and the original Values-"read input from the console",
Syntax for reading from the keyboard: Scanner input = new Scanner (system.in);
Syntax for read-in from file: Scanner input = new Scanner (new file);
Notice that new Scanner (string) creates a Scanner to give the string, so that Scanner can read the data from the file, you use the Java.io.File class to create a file instance, using the following creation method, new File (filename), and then use Scanner (file) to create a Scanner for the Files.
Java Learning notes--string and file IO