A、文字列
、特徴:
--String字符串类,用来表示"",String类被final修饰,也就是不可以被继承。
--String类底层维护了一个char[],而且是final的,也就是说数组一旦创建长度和值都不能被修改了,字符串就变成了一个常量。
--源码摘抄:
--public final class String{}
--private final char value[];
B、オブジェクトを作成します。
--String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
C、一般的に使用される方法:
--char charAt(int index) 返回指定索引处的 char 值。
--String concat(String str) 将指定字符串连接到此字符串的结尾。
--boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
D、テスト:
public class Test1_String {
public static void main ( String[ ] args) {
char [ ] c = new char [ 10 ] ;
String s = new String ( c) ;
String s2 = "abc" ;
System. out. println ( s2. charAt ( 1 ) ) ;
System. out. println ( s2. concat ( "mno" ) ) ;
System. out. println ( s2. contains ( "bc" ) ) ;
System. out. println ( s2. endsWith ( "opq" ) ) ;
System. out. println ( s2. equals ( "abc" ) ) ;
System. out. println ( s2. hashCode ( ) ) ;
System. out. println ( s2. getBytes ( ) ) ;
System. out. println ( Arrays. toString ( s2. getBytes ( ) ) ) ;
s2= "abcaaaccc" ;
System. out. println ( s2. indexOf ( "c" ) ) ;
System. out. println ( s2. lastIndexOf ( "c" ) ) ;
System. out. println ( s2. isEmpty ( ) ) ;
System. out. println ( s2. length ( ) ) ;
System. out. println ( s2. startsWith ( "iop" ) ) ;
System. out. println ( s2. substring ( 3 ) ) ;
System. out. println ( s2. substring ( 3 , 6 ) ) ;
System. out. println ( s2. toCharArray ( ) ) ;
System. out. println ( s2. toLowerCase ( ) ) ;
System. out. println ( s2. toUpperCase ( ) ) ;
s2 = " abc " ;
System. out. println ( s2. trim ( ) ) ;
System. out. println ( String. valueOf ( 100 ) + 1 ) ;
s2= "a|b|c|d|e|f" ;
String[ ] strs = s2. split ( "\\|" ) ;
System. out. println ( Arrays. toString ( strs) ) ;
}
}
二、StringBuilderの
A、オブジェクトを作成します
StringBuilder()はキャラクタジェネレータ、初期容量が16文字である文字列を構築します。
B、一般的な方法
StringBuilderのはappend(String str)文字この文字列に指定された文字列。
C、テスト:文字列の結合効率
public class Test2_StringBuilder {
public static void main ( String[ ] args) {
method1 ( ) ;
}
private static void method1 ( ) {
String str = "abcdefghijklmnopqrstuvwxyz" ;
StringBuffer sb = new StringBuffer ( ) ;
long start = System. currentTimeMillis ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
sb. append ( str) ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
private static void method ( ) {
String str = "abcdefghijklmnopqrstuvwxyz" ;
String result = "" ;
long start = System. currentTimeMillis ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
result = result+ str ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
}
第三に、パッケージ
コンセプト:
8つの基本タイプについては、より良い機能を提供するために、対応するラッパークラスを持っている - 8つの基本タイプ:ダブルブールバイトロングショートint型フロート文字 -ラッパークラス:バイト短整数ロングフロートダブル文字ブール
B、ラッパークラスの継承構造:
-- 所有的数字类型的父类 叫Number
--源码摘抄:public abstract class Number
-- byte byteValue() 以 byte 形式返回指定的数值。
--abstract double doubleValue() 以 double 形式返回指定的数值。
--abstract float floatValue() 以 float 形式返回指定的数值。
--abstract int intValue() 以 int 形式返回指定的数值。
--abstract long longValue() 以 long 形式返回指定的数值。
--short shortValue() 以 short 形式返回指定的数值。
C、整数
--源码摘抄:public final class Integer
--创建对象:
--Integer(int value) 构造一个新分配的 Integer 对象,它表示指定的 int 值。
--常用方法:
--int intValue() 以 int 类型返回该 Integer 的值。
--static int parseInt(String s)将字符串参数作为有符号的十进制整数进行解析。
--static Integer valueOf(int i)返回一个表示指定的 int 值的 Integer 实例。
テスト:
public class Test3_Number {
public static void main ( String[ ] args) {
Integer i = new Integer ( 10 ) ;
Integer i2 = Integer. valueOf ( 10 ) ;
Integer i3 = Integer. valueOf ( 10 ) ;
System. out. println ( i2== i3 ) ;
System. out. println ( i == i3 ) ;
System. out. println ( i2. MAX_VALUE) ;
System. out. println ( i2. intValue ( ) ) ;
int sum = Integer. parseInt ( "100" ) ;
System. out. println ( sum ) ;
}
}
D、ダブル
-- 就是基本类型double 对应的包装类型,用来为double 提供更丰富的功能。
-- 创建对象:
-- Double ( double value) 构造一个新分配的 Double 对象,它表示基本的 double 参数。
-- static Double valueOf ( double d) 返回表示指定的 double 值的 Double 实例。
-- 常用方法:
-- double doubleValue ( ) 返回此 Double 对象的 double 值。
-- static double parseDouble ( String s) 返回一个新的 double 值
-- 测试:
package cn. tedu. number;
public class Test3_Number {
public static void main ( String[ ] args) {
Integer i = new Integer ( 10 ) ;
Integer i2 = Integer. valueOf ( 10 ) ;
Integer i3 = Integer. valueOf ( 10 ) ;
System. out. println ( i2== i3 ) ;
System. out. println ( i == i3 ) ;
System. out. println ( i2. MAX_VALUE) ;
System. out. println ( i2. intValue ( ) ) ;
int sum = Integer. parseInt ( "100" ) ;
System. out. println ( sum ) ;
Double d = new Double ( 3.14 ) ;
Double d2 = Double. valueOf ( 3.14 ) ;
System. out. println ( d2. MAX_VALUE) ;
System. out. println ( d2. doubleValue ( ) ) ;
double count = Double. parseDouble ( "3.14" ) ;
System. out. println ( count) ;
}
}
第四に、クラス日付
、コンセプト
Dateクラスは、ミリ秒の精度で、特定のモーメントを表し、
B、オブジェクトを作成します
日付()Dateオブジェクトを割り当て、その割り当てられた時間(ミリ秒単位まで正確に)示すためにオブジェクトを初期化します。
C、テスト
public class Test4_Date {
public static void main ( String[ ] args) {
Date date = new Date ( ) ;
System. out. println ( date. getDate ( ) ) ;
System. out. println ( date. getDay ( ) ) ;
System. out. println ( date. getHours ( ) ) ;
System. out. println ( date. getMonth ( ) ) ;
System. out. println ( date. getMinutes ( ) ) ;
System. out. println ( date. getSeconds ( ) ) ;
System. out. println ( date. getTime ( ) ) ;
System. out. println ( date. getYear ( ) ) ;
System. out. println ( date. toLocaleString ( ) ) ;
System. out. println ( date. hashCode ( ) ) ;
}
}
第五に、日付ツールのSimpleDateFormat
、コンセプト
特に、入力日付とString型の日付の間で変換するために使用されます
B、オブジェクトを作成します
指定されたパターンとデフォルトの日付形式ロケールシンボル構造のSimpleDateFormatとのSimpleDateFormat(文字列パターン)。
Cは、この方法は、使用します
フォーマット(日):フォーマットされた文字列の日付
パース(文字列):文字列を日付に解析
D、あなたが生き残るの日数を計算します。
public class Test5_SimpleDate {
public static void main ( String[ ] args) throws ParseException {
String birthday = new Scanner ( System. in) . nextLine ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date birth = sdf. parse ( birthday) ;
long start = birth. getTime ( ) ;
long now = System. currentTimeMillis ( ) ;
System. out. println ( ( now- start) / 1000 / 60 / 60 / 24 ) ;
}
}