JAVA Study Notes-Basic JAVA syntax (5)

Source: Internet
Author: User

Some simple and common JAVA classes

1. The String class represents an unchangeable String sequence.

String str1 = new ("hello"); the difference from String str = "hello" is that an object is created and saved in the heap, the latter string is placed in a static memory area.

Think: Is the str1 and str2 equal? Why?

Understand various constructor methods in the String class, and write down common methods in the String class.

For example:

CharAt(int index)  Returns the character at the index position.

EndsWith(String suffix)  Determines whether the string ends with this string.

StartsWith(String prefix)Determines whether the string starts with the string.

EqualsIgnoreCase(String anotherString)  Set thisStringWith anotherStringComparison, not case sensitive.

IndexOf(int ch, int fromIndex)Returns the index that appears at the specified character for the first time in this string and starts searching from the specified index.

Replace(char oldChar, char newChar) Returns a new string by usingnewCharReplace alloldChar.

Substring(int beginIndex)

Returns a new string, which is a substring of this string.

Trim()

Returns a copy of the string, ignoring leading and trailing spaces.

ToLowerCase(Locale locale)

Use the givenLocaleThe ruleStringAll characters in are converted to lowercase letters.

ToUpperCase()

Use the rules of the default language environment
StringAll characters in are converted to uppercase letters.

Split(String regex)

Splits the string based on the matching of the given regular expression.

2. StringBuffer class
Variable string Sequence

Suitable for a variable character sequence, which is more efficient than a String. You can append a String to the end and delete a String.

The initialization and simple usage statements are as follows:

StringBuffer str = new StringBuffer ("hello"); create an object

Str. append ("world ");
Append a world directly after hello.

The output result of the console is helloworld.

3. Packaging

The Int packaging class is Integer.

The current JAVA5.0 provides automatic packing and automatic unpacking.

Integer I = 10; this is called automatic packing

Int n = I; this is called automatic unpacking.

 

The following code converts a string array to an int type in the int packaging class.

Public class test {

Public
Static void main (String [] args ){

Int
Num1 = Integer. parseInt (args [0]);

Int
Num2 = Integer. parseInt (args [1]);

Int
Num3 = num1 + num2;

System. out. println (num3 );

}

}

The Char packaging class is Character.

4. Math class (all static methods)

Method to generate a random number:

1) Math class

Math.Random()

Returns the positive valuedoubleValue. The value must be greater than or equal0.0And less1.0. (Functions that can be converted to other numbers flexibly)

2) under the JAVA. util. Random package

    New Randow.NextInt(int n)

Returns a pseudo-random number, which is evenly distributed between 0 (inclusive) and the specified value (excluded) from the sequence of the random number generator.
intValue.

Abs(Data Type)

ReturnData TypeThe absolute value of the value.

5. JAVA. io. File

To learn how to create a folder, including creating a single folder and Multiple folders, use mkdir and mkdirs. To understand the differences between listFile () and list (), list () is the subdirectory that outputs the contents of a directory.

As shown in the following program:

Public class test {

Import java. io. File;

Public class test {

Public
Static void main (String [] args ){

File
File = new File ("e:/jxauzhongkewen ");

// File. mkdirs ();
Create multiple folders in a path

// File. mkdir ();
Create a single folder under a path

File []
Fs = file. listFiles (); // cyclically output the directory in a folder

For (int
I = 0; I <fs. length; I ++ ){

System. out. println (fs [I]);

}

}

}

 

The following is also the most common idea in the File class. The idea of using a tree structure shows the files contained in the File hierarchically:

Import java. io. File;

Public class test {

Public
Static void main (String [] args) throws Exception {

Test. show (new
File ("e:/zhongkewen"), 0 );

}

Public
Static void show (File f, int level ){

StringBuffer
Str = new StringBuffer ("");

For (int
I = 0; I <level; I ++ ){

Str. append ("
");

}

File []
Fs = f. listFiles ();

For (int
I = 0; I <fs. length; I ++ ){

System. out. println (str. append (fs [I]. getName ()));

If (fs [I]. isDirectory ()){

Show (fs [I], ++ level );

}

}

}

}

 

 

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.