Basic Java Tutorials-Common classes (iv)

Source: Internet
Author: User

Iv. Common Categories

4.1 String-related classes (string stringbuffer)

4.1.1String class

    

4.1.2StringBuffer class (representing variable)

Explain the mutable and immutable

String S1 = "Hello";

String s2 = "World";

S1+=S2;

It actually opens up a piece of memory, and the Hello and world copy go in, S1 point to the new memory

And StringBuffer just add a piece of memory behind the S1, add the world, do not need copy

  String and StringBuffer only this certain difference.

4.2 Basic Data Type wrapper class (Integer,double,character, etc.) (Java.lang)

Encapsulate a base data type value as an object

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

 String separator = file.separator;//file delimiter, Windows is \, and Linux is/, but both systems apply to/.    String filename = "MyFile.txt"; String directory = "Mydir1" + separator + "MYDIR2"; program full version package Bjsxt;import java.io.*;p ublic class Testfile {public STA    tic void Main (string[] args) {String separator = File.separator;    String filename = "MyFile.txt";    String directory = "Mydir1" + separator + "MYDIR2";    String directory = "Mydir1/mydir2";    String directory = "Mydir1\\mydir2";    File F = new file (directory, filename);      if (f.exists ()) {System.out.println ("file name:" + F.getabsolutepath ());    SYSTEM.OUT.PRINTLN ("File size:" + f.length ());      } else {f.getparentfile (). Mkdirs ();      try {f.createnewfile ();      } catch (IOException e) {e.printstacktrace (); }    }  }}
Print all things for a folder and its sub-files import java.io.*;p Ublic class FileList {public static void main (string[] args) {file F = new file ("d:/a") ; System.out.println (F.getname ()); Tree (f, 1);} private static void Tree (File f, int level) {String prestr = ' "; for (int i=0; i<level; i++) {prestr + = "    ";} file[] Childs = F.listfiles (); for (int i=0; i<childs.length; i++) {System.out.println (prestr + childs[i].getname ()); if (Childs[i].isdirectory ()) {tree (Childs[i], level + 1);}}}

4.4Enum Class (Java.lang.Enum)

Used to fetch only one of the specified values.

public enum MyColor {red, green, blue};//defines a type

MyColor m = mycolor.red;//value

Program full version public class Testenum {public enum MyColor {red, green, blue};p ublic enum Mydooropener {me, mywife}; public static void Main (string[] args) {MyColor m = Mycolor.red;switch (m) {case red:System.out.println ("Red"); Break;case Green: System.out.println ("green"); Break;default:system.out.println ("Default");} System.out.println (M);}}

Basic Java Tutorials-Common classes (iv)

Related Article

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.