Javase Basic Review Guide "six"

Source: Internet
Author: User
Tags natural logarithm pow

Learning Java students know that sun for us to encapsulate a lot of commonly used classes, this chapter for you to summarize the classes we often use. Previous blog A friend of a message asked me if the string is a reference data type? I found out that string belongs to the application data type by finding the data, so let's start by looking at the string class.

1. String class

The Java.long.String class represents the immutable sequence of characters. Examples of string classes:

 Public classSTR {/**     * @paramExamples of string classes*/     Public Static voidMain (string[] args) {String A1= "ABC"; String A2= "ABC"; String A3= "ABCD"; SYSTEM.OUT.PRINTLN (A1==A2);//trueSystem.out.println (A1==A3);//falseSystem.out.println (); String B1=NewString ("ABC"); String B2=NewString ("ABC"); String B3=NewString ("ABCD"); System.out.println (B1==B2);//falseSystem.out.println (B1==B3);//falseSystem.out.println (B1.equals (B2));//trueSystem.out.println (B1.equals (B3));//falseSystem.out.println (); Char[] C = {' A ', ' B ', ' C ', ' d '}; String C1=NewString (c); String C2=NewString (c,0,3);//C: a character array; 0: The first element in an array; the length of the 3:C2SYSTEM.OUT.PRINTLN (C1);//ABCDSystem.out.println (C2);//ABC    }}

2. Common methods in the string class:

public char charAt (int index): Returns the first index character in a string

public int Length (): Returns the length of the string

public int indexOf (string str): Returns the position of the first occurrence of STR in a string

public int indexOf (string str, int fromIndex): Returns the position of the first str in a string from FromIndex

public boolean equalsignorecase (string another): Compares strings to another (ignoring case)

public string Replace (char Lastchar, char newchar): replace Lastchar character with Newchar in string

Public Boollean StartsWith (String str): Determines whether a string starts with Str

Public Boollean EndsWith (String str): Determines whether a string ends with Str

 Public classTest { Public Static voidmain (string [] args) {string A1= "Sun JAVA"; String A2= "Sun Java"; System.out.println ("Length of A1:" +a1.length ()); System.out.println ("The fifth character in A1 is:" +a1.charat (5)); System.out.println ("The first occurrence of Java in A1:" +a1.indexof ("Java"));        System.out.println (A1.equals (A2));        System.out.println (A1.equalsignorecase (A2)); System.out.println (A1.startswith ("Sun"));//determines whether to start with the specified prefixSystem.out.println (A1.endswith ("JAVA"));//determines whether to end with the specified suffixString A3= "I Love Play computer game!"; String A4= A3.replace (", '-')); System.out.println ("A3=" +A3); System.out.println ("A4=" +A4); }}

3. The basic data type is converted to a string:

static method public string ValueOf (...), which converts the base data type to a string. Instance:

int B1 = 1234;         double b2 = 12.13;         Boolean true ;         // Type Conversions        String b4 = string.valueof (b1);         = string.valueof (b2);         = String.valueof (b3);

4, StringBuffer class:

Java.long.StringBuffer represents a variable sequence of characters, instance code:

  

5. Math class:

Java.long.Math provides a series of static methods for scientific calculations, and the methods of the parameters and return value types are generally double type.

ABS: Absolute Value

sqrt: square root

Pow (double A, double b): Power B of a

LOG: Natural logarithm

Max (double A, double b): Larger value in a, b

Min (double A, double b): Smaller value in a, b

Random (): Returns the number 0.0 to 1.0

Long round (double a):d data A of type ouble is converted to long (rounded)

Todegrees (double Angrad): radians--angle

Toradians (double angdeg): angle, radians

 Public classText {/**     * @paramMath class*/     Public Static voidMain (string[] args) {DoubleA1 =Math.random (); DoubleA2 =Math.random (); System.out.println (MATH.SQRT (A1*a1+a2*A2)); System.out.println (Math.pow (A1,8));        System.out.println (Math.Round (A1)); System.out.println (Math.log (Math.pow (MATH.E,15))); }}

6. File class:

The Java.io.File class represents the System file name (path and file name)

 Public classfile {/**     * @paramFile Class*/     Public Static voidMain (string[] args) {String separator=File.separator; String FileName= "MyFile.txt";//file nameString Directory= "Mydir1" +separator+ "Mydir2";//file path//String directory = "Mydir1/mydir2"; //String directory = "Mydir1\\mydir2";File F=NewFile (directory, fileName); if(F.exists ()) {//determine if a file existsSystem.out.println ("File name:" +f.getname ()); System.out.println ("File Size:" +f.length ()); }Else{f.getparentfile (). Mkdirs ();//Create file path            Try{f.createnewfile ();//Create a file}Catch(IOException e) {e.printstacktrace (); }        }    }}

For the commonly used classes in Java, you can review the Java Help documentation to better understand the use of classes in Java.

Javase Basic Review Guide "six"

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.