Java starts from scratch, and the basics of learning notes & lt; encapsulation Class & gt; (11)

Source: Internet
Author: User
Tags time in milliseconds

Introduction to the Integer class: package com. ibm. four; public class Test {public static void main (String [] args) {Integer I = 123; byte b1 = I. byteValue (); System. out. println (b1); double d1 = I. doubleValue (); System. out. println (d1); float f1 = I. floatValue (); int i1 = I. intValue (); long l1 = I. longValue (); short s1 = I. struct value (); String str1 = I. toString (); int ii = 15; int ii1 = Integer. bitCount (ii); System. out. println (ii1 ); Int ii2 = Integer. parseInt (str1, 8); System. out. println (ii2); String str2 = Integer. toBinaryString (ii); System. out. println (str2); String str3 = Integer. toHexString (ii); System. out. println (str3); String str4 = Integer. toString (ii); System. out. println (str4); Integer iii = Integer. valueOf (ii); System. out. println (iii) ;}} String class: some usage methods: package com. ibm. strings; import java. io. unsupportedEncodingExc Eption; public class StringDemo {public static void main (String [] args) throws UnsupportedEncodingException {String str = "welcome softomm"; // method for constructing a new String; // 1. the concat method is added after str! String newStr = str. concat ("! "); System. out. println (newStr); // 2. replace the char value in the String with the new String newStr2 = str. replace ('E', 'E'); System. out. println (newStr2); CharSequence cs = str. subSequence (0, str. length (); CharSequence cs2 = str. subSequence (0, str. length ()/2); String newStr3 = str. replace (cs2, cs); System. out. println (newStr3); // 3. subString truncation // It is used to intercept a new String from the specified index to the end of the String newStr4 = str. substring (8); Sys Tem. out. println (newStr4); // If the str String is truncated to come soft String newStr5 = str. substring (3,12); System. out. println (newStr5); // 4. toUpperCase converts a String into a String newStr6 = str according to the system environment. toUpperCase (); System. out. println (newStr6); // 5. toLowerCase converts String to lowercase String newStr7 = str. toLowerCase (); System. out. println (newStr7); // 6. trim removes the leading and trailing spaces of the string. If there is a space in the middle of the string, the leading space is not removed from the System. out. println (str. trim (); // String SEARCH method String [] Strs = {"a ", "a ", "B ", "aa", "bb", "cc"}; // 1. startWith determines whether a String starts with XX for (String s: strs) {if (s. startsWith ("a") {System. out. println (s);} if (s. endsWith ("handsome") {System. out. println (s + "=========") ;}}// 2. indexof lastIndexOf // determines the index position of the substring that appears for the first time in a string. int index = str. indexOf ('E'); System. out. println (index); // 3. determines the index position of the substring in a string. int last = str. lastIndexOf ('E'); System. out. println (last ); // 4. method for comparing strings // compare this string with another string, case-insensitive //, note that the length of the string and the visiting sequence of each character in the string boolean flag = str. equalsIgnoreCase ("WelCome SofTomM"); System. out. println (flag); // 5. compareTo compares two strings alphabetically. String str8 = "a"; int j = str8.compareTo ("B"); System. out. println (j); // 6. charAt returns the char cc = str. charAt (3); System. out. println (cc); // 7. format: format the string in one format. // 8. getBytes converts a string into a byte array byte [] B = str. getBytes (); // encode and convert a String. If garbled characters exist, run the following command: String newStr9 = new String (str. getBytes ("iso-8859-1"), "UTF-8"); // 9. split Splits a String into one String. String [] stres = str. split (""); System. out. println (stres [0]); System. out. println (stres [1]) ;}} StringBuffer: package com. ibm. strings; public class StringBufferDemo {public static void main (String [] args) {// create a StringBuffer object, representing a variable length String. stringBuffer sb = new StringBuffer (); // call the append method to add the string to the sb object to remove sb. append ("softomm"); sb. append (true); sb. append ("you"); sb. append ("aaaaaaaaa"); // Add "OK" to your backend to become sb. insert (12, "good"); System. out. println (sb); // reverse string reverse AB-> ba sb. reverse (); System. out. println (sb); // setCharAt sets the corresponding string sb for the specified index location. setCharAt (1, 'B'); System. out. println (sb); // setLength specifies the length of sb for a Variable Length string. setLength (2); System. out. println (sb );}} stringBuilder StringBuffer String comparison/* String StringBuffer StringBuilder * variable length * thread safety * efficiency low efficiency Medium efficiency high * single thread support multithreading *** /execution efficiency: package com. ibm. strings; public class StringSpeed {public static void main (String [] args) {// String str = ""; /// obtain the System time in milliseconds. Return the number of milliseconds between and the current time. // long start = System. currentTimeMillis (); // cyclically add an empty str string for 10000 operations // for (int I = 0; I <10000; I ++) {// str = str + I; //} // After the loop is completed, obtain the System time // long end = System. currentTimeMillis (); // System. out. println (end-start); // StringBuffer str = new StringBuffer (); // long start = System. currentTimeMillis (); // cyclically add an empty str string for 10000 operations // for (int I = 0; I <10000; I ++) {// str. append (I); //} // After the loop is completed, obtain the System time // long end = System. currentTimeMillis (); // System. out. println (end-start); StringBuilder str = new StringBuilder (); long start = System. currentTimeMillis (); // cyclically add an empty str string for (int I = 0; I <10000; I ++) {str. append (I);} // After the loop is completed, obtain the System time long end = System. currentTimeMillis (); System. out. println (end-start); // System. out. println (end );}}

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.