Java

 
主要内容:
* 字符串相关类(String、StringBuffer)
* 基础数据类型包装类(Byte、Character、Short、Integer、Long、Float、Double、Boolean)
* Math 类
* File 类
* 枚举类
 
 
 
 
java.lang.String 类 
* java.lang.String 类代表不可变的字符序列。
* “xxxxx” 为该类的一个对象。
* String 类常见的构造方法:
  ** String( String original ):创建一个 String 对象为 original 的拷贝。
  ** String( char[] value ):用一个字符数组创建一个 String 对象。
  ** String( char[] value, int offset, int count ):用一个字符数组为从 offset 项开始的 count 个字符序列创建一个 String 对象。
 
 
 
String 类常用方法(1):
* public char charAt( int index ):返回字符串中第 index 个字符。
* public int length():返回字符串的长度。
* public int indexOf( String str ):返回字符串中第一个 str 字符串的首地址,找不着返回 -1 。
* public int indexOf( String str, int fromIndex ):返回字符串中从 fromIndex 开始第一个 str 字符串的首地址,找不着返回 -1 。
* public boolean equalsIgnoreCase( String another ):比较字符串与 another 是否一样(忽略大小写)
* public String replace( char oldChar, char newChar ):在字符串中用 newChar 字符替换 oldChar 字符。
* 例子程序:
public class Test {
 public static void main( String[] args ) {
 
  String s1 = "sun java", s2 = "Sun Java";
  System.out.println( s1.charAt( 1 ) );//u
  System.out.println( s2.length() );//8
  System.out.println( s1.indexOf( "java" ) );//4
  System.out.println( s1.indexOf( "Java" ) );//-1
  System.out.println( s1.equals( s2 ) );//false
  System.out.println( s1.equalsIgnoreCase( s2 ) );//true
 
  String s = "我是程序员,我在学java";
  String sr = s.replace( '我', '你' );
  System.out.println( sr );//你是程序员,你在学java
 
 }
}
 
 
 
String 类常用方法(2): 
* public boolean stratsWith( String prefix ):判断字符串是否以 prefix 字符串开头。
* public bolean endsWith( String suffix ):判断字符串是否以 suffix 字符串结尾。
* public String toUpperCase():返回该字符串的大写形式。
* public String toLowerCase():返回该字符串的小写形式。
* public String substring( int beginIndex ):返回该字符串从 beginIndex 开始到结尾的子字符串。
* public String substring( int beginIndex, int endIndex ):返回该字符串从 beginIndex 开始到 endIndex(不包括) 结尾的子字符串。
* public String trim():返回该字符串去掉开头和结尾后的字符串。
* 例子程序:
public class Test {
 public static void main( String[] args ) {
 
  String s = "Welcome to Java World!";
  String s1 = " sun java ";
  System.out.println( s.startsWith( "Welcome" ) );//true
  System.out.println( s.endsWith( "World" ) );//false
  String sL = s.toLowerCase();
  String sU = s.toUpperCase();
  System.out.println( sL );//welcome to java world!
  System.out.println( sU );//WELCOME TO JAVA WORLD!
  String subS = s.substring( 11 );
  System.out.println( subS );//Java World!
  String sp = s1.trim();
  System.out.println( sp );//sun java
 
 }
 
}
 
 
 
String 类常用方法(3): 
* 静态重载方法:
  ** public static String valueOf( ... ):可以将基础数据类型的数据转换为字符串;例如:
     *** public static String valueOf( double d )
  *** public static String valueOf( int i )
  *** ... ... ...
* public String[] split( String regex ):返回在字符串中用 regex 字符串进行分割的字符串数组,拖曳(也就是最后)的空字符串省略。
* public String[] split( String regex, int limit ):返回在字符串中用 regex 字符串进行分割的字符串数组,如果 limit 大于 0 ,则分割前 limit - 1 次(注意:如果 limit 为 1 时,也是如此,也就说分割 0 次),若超过可分割次数,按可分割次数分割,拖曳的空字符串保留,若不足可分割次数,按 limit - 1 次分,剩下的全部保留;如果 limit 小于 0 ,则按可分割次数分割,拖曳的空字符串保留;如果 limit 等于 0 ,则按可分割次数分割,拖曳的空字符串忽略。
* 例子程序:
public class Test {
 public static void main( String[] args ) {
 
  int j = 1234567;
  String sNumber = String.valueOf( j );
  System.out.println( "j 是" + sNumber.length() + "位数。" );
  String s = "Mary, F, 1976";
  String[] sPlit = s.split( ", " );
  for( int i=0; i<sPlit.length; i++ ) {
   System.out.println( sPlit[ i ] );
  }
 
 }
}
output result:
j 是7位数。
Marry
F
1976
 
 
 
 
java.lang.StringBuffer 类: 
* java.lang.StringBuffer 代表可变的字符序列。
* StringBuffer 和 String 类似,但 StringBuffer 可以对其字符串进行改变。
* StringBuffer 类的常见构造方法:
  ** StringBuffer():创建一个不包含字符序列的“空”的 StringBuffer对象。
  ** StringBuffer( String str ):创建一个 StringBuffer 对象,包含与 String 对象 str 相同的字符序列。
 
 
 
StringBuffer 常用方法(1): 
* 重载方法 public StringBuffer append( ... ):可以为该 StringBuffer 对象添加字符序列,返回添加后的该 StringBuffer对象引用;例如:
  ** public StringBuffer append( String str )
  ** public StringBuffer append( StringBuffer strBuf )
  ** public StringBuffer append( char[] str )
  ** public StringBuffer append( char[] cha, int offset, int len )
  ** public StringBuffer append( Double d )
  ** public StringBuffer append( Object obj )
  ** ... ... ...
 
 
 
StringBuffer 常用方法(2): 
* 重载方法 public StringBuffer insert( ... ):可为该 StringBuffer 对象在指定位置插入字符序列,返回修改后的该 StringBuffer 对象的引用;例如:
  ** public StringBuffer insert( int offset, String str )
  ** public StringBuffer insert( int offset, double d )
  ** ... ... ...
* 方法 public StringBuffer delete( int start, int end ):可以删除从 start 下标开始到 end (不包括)下标为止的一段字符序列,返回修改后的该 StringBuffer 对象的引用。
 
 
 
StringBuffer 常用方法(3): 
* 和 String 类含有类似的方法:
  ** public int indexOf( String str )
  ** public int indexOf( String str, int fromIndex )
  ** public String substring( int start )
  ** public String substring( int start, int end )
  ** public int length()
* 方法 public StringBuffer reverse() 用于将字符序列逆序,返回修改后的该 StringBuffer 对象的引用。
 
 
 
StringBuffer 例子程序: 
public class Test {
 public static void main( String[] args ) {
 
  String s = "Microsoft";
  char[] a = { 'a', 'b', 'c' };
  StringBuffer sb1 = new StringBuffer( s );
  sb1.append( '/' ).append( 'IBM' ).append( '/' ).append( "Sun" );
  System.out.println( sb1 );
  StringBuffer sb2 = new StringBuffer( "数字" );
  for( int i=0; i<=9; i++ ) {
   sb2.append( i );
  }
  System.out.println( sb2 );
  sb2.delete( 8, sb2.length() ).insert( 0, a );
  System.out.println( sb2 );
  System.out.println( sb2.reverse() );
 
 }
}
output result:
Microsoft/IBM/Sun
数字0123456789
abc数字012345
543210字数cba
 
 
 
基础数据类型包装类: 
* 包装类(如:Integer, Double等)这些类封装了一个相应的基础数据类型数值,并为其提供了一系列操作。
* 以 java.lang.Integer 类为例;构造方法:
  ** Integer( int value )
  ** Integer( String str )
* 以 java.lang.Integer 类为例;常见方法:
  ** public static final int MAX_VALUE:最大的 int 型数(2 的 31 次方 - 1)
  ** public static final int MIN_VALUE:最小的 int 型数(-2 的 31 次方)
  ** public long longValue():返回封装数据的 long 型值
  ** public double doubleValue():返回封装数据的 double 型值
  ** int intValue():返回封装数据的 int 型值
  ** public static int parseInt( String s ) throws NumberFormatException:返回 s 字符串解析成 int 型后的数据
  ** public static Integer valueOf( String s ) throws NumberFormatException:返回 s 字符串解析成 int 型后的数据的 Integer 对象
* 包装类例子程序:
public class Test {
 public static void main( String[] args ) {
 
  Integer i = new Integer( 100 );
  Double d = new Double( "123.456" );
  int j = i.intValue() + d.intValue();
  float f = i.floatValue() + d.floatValue();
  System.out.println( j );
  System.out.println( f );
  double pi = Double.parseDouble( "3.1415926" );
  double r = Double.valueOf( "2.0" ).doubleValue();
  double s = pi * r * r;
  System.out.println( s );
  try {
   int k = Integer.parseInt( "1.25" );
  } catch (NumberFormatException nfe) {
   System.out.println( "数据格式不对!" );
  }
  System.out.println( Integer.toBinaryString( 123 ) + "B" );
  System.out.println( Integer.toHexString( 123 ) + "H" );
  System.out.println( Integer.toOctalString( 123 ) + "O" );
 
 }
}
output result:
223
223.456
12.5663704
数据格式不对!
1111011B
7bH
173O
 
 
 
 
java.lang.Math 类: 
* java.lang.Math 提供了一系列静态方法用于科学计算,其方法的参数和返回值类型一般为 double 型。
  ** abs 绝对值
  ** acos 反余弦, asin 反正弦, atan 反正切, cos 余弦, sin 正弦, tan 正切
  ** sqrt 开平方根
  ** pow( double a, double b ) a 的 b 次幂
  ** log 自然对数(以 e 为底的)
  ** exp 以 e 为指数
  ** max( double a, double b )
  ** min( double a, double b )
  ** random() 返回 0.0 到 1.0 的随机数
  ** long round( double a ) double 型的数据 a 转换为 long 型(四舍五入)
  ** toDegrees( double angrad ) 弧度 --> 角度
  ** toRadians( double angdeg ) 角度 --> 弧度
* 例子程序:
public class Test {
 public static void main( String[] args ) {
 
  double a = Math.random();
  double b = Math.random();
  System.out.prinltn( Math.sqrt( a * a + b * b ) );
  System.out.println( Math.pow( a, 8 ) );
  System.out.println( Math.round( b ) );
  System.out.println( Math.log( Math.pow( Math.E, 15 ) ) );
  double d = 60.0, r = Math.PI / 4;
  System.out.println( Math.toRadians( d ) );
  System.out.println( Math.toDegrees( r ) );
 
 }
}
output result:
0.22724854767821204
3.0369119934905976E-10
0
15.0
1.0471975511965976
45.0
 
 
 
 
java.io.File 类 
* java.io.File 类代表系统文件名(路径和文件名)
* File 类的常见构造方法:
  ** public File( String pathname ):以 pathname 为路径创建 File 对象,如果 pathname 是相对路径,则默认的当前路径在系统属性 user.dir 中存储
  ** public File( String parent, String child ):以 parent 为父路径,child 为子路径创建 File 对象
* File 的静态属性 String separator 存储了当前系统的路径分隔符
 
 
 
java.io.File 类常用方法 
* 通过 File 对象可以访问文件的属性
  ** public boolean canRead()
  ** public boolean canWrite()
  ** public boolean exists()
  ** public boolean isDirectory()
  ** public boolean isFile()
  ** public boolean isHidden()
  ** public long lastModified()
  ** public long length()
  ** public String getName()
  ** public String getPath()
  ** public String getAbsolutePath()
* 通过 File 对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
  ** public boolean createNewFile() throws IOException
  ** public boolean delete()
  ** public boolean mkdir()//创建一个路径
  ** public boolean mkdirs()//创建一系列路径
* 例子程序:
import java.io.*;
public class Test {
 public static void main( String[] args ) {
 
  String separator = File.separator;
  String filename = "myfile.txt";
  String directory = "mydir1" + separator + "mydir2";
  File f = new File( directory, filename );
  if( f.exists() ) {
   System.out.println( "文件名:" + f.getAbsolutePath() );
   System.out.println( "文件大小:" + f.length() );
  } else {
   f.getParentFile().mkdirs();//说明:这时会创建在类(也就是该 .class 文件所在目录)的父目录下,但要注意的是,如果这个类有包目录,则是先找到该类的顶层父包,再找它的父目录,并开始创建(也就说这时完整的包名加上类名才被视为该类的名字)
   try {
    f.createNewFile();
   } catch( IOException e ) {
    e.printStackTrace();
   }
  }
 
 }
}
 
 
 
 
java.lang.Enum 枚举类型 
* 枚举类型
  ** 只能够取特定值中的一个
  ** 使用 enum 关键字
  ** 是 java.lang.Enum 类型
* 例子程序:
public class TestEnum {
 public enum MyColor { red, green, blue };
 
 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 );
 
 }
}
 
 
 
总结: 
* String 相关
  ** 正则表达式
* 基础数据类型包装类
* java.lang.Math
* java.io.File
  ** 递归
* 枚举类型
原文地址:https://www.cnblogs.com/andremao/p/7860565.html