类库、应用程序的编程接口(Application Programming Interface,API):Java系统提供了大量的类和接口供程序开发人员使用,并且按照功能的不同,存放在不同的包中。
java API的概念:
java.lang.Object类:
public String toString(){ return getClass().getName()+"@"+integer.toHexString(hashCode()); }
class TestObject1 { public static void main(String[] args) { Object o = new Object(); System.out.println(o.hashCode());//十进制哈西码 System.out.println(Integer.toHexString(o.hashCode()));//十六进制哈西码 System.out.println(o.toString());//等于System.out.println(o) } }
class Car { String color; String name; public String toString(){ return "车的颜色是:"+color+" 车的名字是:"+name; } } public class TestObject2 { public static void main(String[] args) { Car c = new Car(); c.color = "红色"; c.name = "奥迪"; System.out.println(c); System.out.println(c.toString()); } }车的颜色是:红色 车的名字是:奥迪
class TestEquals { public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("abc"); System.out.println(s1==s2);//f不是同一对象 System.out.println(s1.equals(s2));//t是同一内容 s2 = s1; System.out.println(s1==s2);//t System.out.println(s1.equals(s2));//t String s3 = "abc"; String s4 = "abc"; System.out.println(s3==s4);//t System.out.println(s3.equals(s4));//t String s5 = "abc"; String s6 = new String("abc"); System.out.println(s5==s6);//f System.out.println(s5.equals(s6));//t } }判断字符串数据是否相等时,适合用equals()
class TestString1 { public static void main(String[] args) { byte[] b = {67,68,66,100,101}; System.out.println(new String(b));//=String s = new String(b);System.out.println(s); char[] c = {'a','b','c','c','b','a'}; System.out.println(new String(c)); System.out.println(new String("Hello,World"));//System.out.println(new String(c));//String str3 = "abc"; System.out.println(new String("abc")); } }CDBde
class TestString1 { public static void main(String[] args) { String str1 = "Java Application",str2=" and Apples"; System.out.println(str1.trim().concat(str2)); System.out.println(str1.replace('A','a')); System.out.println(str1.toUpperCase()) System.out.println(str2.toLowerCase()) System.out.println(str1.substring(5,8)); } }public int lastIndexOf(int ch)和public int lastIndexOf(String str):
class TestString3 { public static void main(String[] args) { String s = "java App and app"; for(int i=0;i<s.length();i++){ System.out.print(s.charAt(i));//java App and app } System.out.println(); System.out.println(s.startsWith("java"));//t System.out.println(s.startsWith("va",2));//t System.out.println(s.endsWith("app"));//t System.out.println(s.indexOf("java"));//0 System.out.println(s.lastIndexOf("p"));//15 } }查找某字符串a出现的次数
class findA { public static int getCount(String str){ int count = 0; for(int i=0;i<str.length();i++){ char ch = str.charAt(i); if(ch == 'a'){ count++; } }return count; } public static void main(String[] args) { String s = "java App and app"; int count = getCount(s); System.out.println(count);//4 } }查找字符串中某个字符出现的次数
class findA { public static int getCount(String str,char c){ int count = 0; for(int i=0;i<str.length();i++){ char ch = str.charAt(i); if(ch == c){ count++; } }return count; } public static void main(String[] args) { String s = "java App and app"; int count = getCount(s,'n'); System.out.println(count);//1 } }查找字符串中某子串出现的次数
//1.定义一个计数器 //2.获取查找字串出现的位置 class FindStr { public static int getCountString(String str,String sub){ int count=0; int index=0; while(true){ index=str.indexOf(sub,index); //defabcdefefabcdedf if(index==-1){ break; } index=index+sub.length(); count++; } return count; } public static void main(String[] args) { int count=getCountString("defabcdefefabcdedf","de"); System.out.println(count); } }
class TestString4 { public static void main(String[] args) { String s1 = "Hello I am Javk"; String s2 = "Hello I am Javk"; String s3 = "Hello i am Javk"; System.out.println(s1.equals(s2));//t System.out.println(s1.equalsIgnoreCase(s3));//t System.out.println(s1.compareTo(s2));//0 System.out.println(s1.compareTo(s3));//-32 } }
import java.util.*; class TestStringBuffer { public static void main(String[] args) { StringBuffer s = new StringBuffer("aaa"); System.out.println(s.append("bbb"));//aaabbb System.out.println(s.insert(3,"+"));//aaa+bbb System.out.println(s.reverse());//bbb+aaa System.out.println(s.append(new Date()));//bbb+aaaMon...... } }
java.lang.System
class TestSystem { public static void main(String[] args) { long timeStart = System.currentTimeMillis(); long count = 0; for(long i=0; i<100000000;i++){ count+=i; } long timeEnd = System.currentTimeMillis(); System.out.println("运行时间是:"+(timeEnd-timeStart));//运行时间是:47 } }
java,lang.Runtime
import java.io.*; class TestRuntime { public static void main(String[] args) { try{ Runtime r = Runtime.getRuntime();//通过静态方法获得该类实例 Process p = r.exec("C:\\Windows\\System32\\Notepad.exe");//启动子进程,返回代表子进程的Person对象 }catch(IOException ex){ System.out.println(ex.toString());//打开一个记事本 } } }
封装类
public void test(Object o){ System.out.println(o.toString()); }//该方法可处理任何引用类型的数据,不能处理基本类型数据
基本数据类型 | 封装类 |
int | Integer |
char | character |
short | Short |
long | Long |
byte | Byte |
float | Float |
double | Double |
boolean | Boolean |
封装类均被定义为final,不能被继承,内容不被改变,只读型
class TestWrapper { public static void main(String[] args) { Integer int1=new Integer(786);//串---int Integer int2=new Integer("786"); int i = int1.intValue();//串---int System.out.println(i);//786//串---int System.out.println(int1==int2);//f System.out.println(int1.equals(int2));//t i=Integer.parseInt("777"); //串---int System.out.println(i);//777串---int System.out.println(Integer.toHexString(-1));//ffffffff System.out.println(String.valueOf(i)); //===将参数d转化为字符串的形式返回 777 System.out.println(Integer.toString(i));// ===将整型i转换为字符串的形式返回777 } }
class TestAutoBoxing { public static void main(String[] args) { int j = 4; Integer obj1 = j;//自动封装 Integer obj2 = new Integer(3);//自动拆封 int i = obj2; System.out.println(i); System.out.println(obj1.toString()); } } //自动封装 Integer obj1; int num1=33; obj1=num1; //自动拆封 Integer obj2=new Integer(22); intnum2; num2=obj2
日期
import java.util.*; class TestDate { public static void main(String[] args) { Date d1 = new Date(20000); long time = d1.getTime(); System.out.println(time);//20000 System.out.println(d1);//系统日期,1097。。。 Date d2 = new Date(); System.out.println(d2.getTime());//毫秒数 System.out.println(d2);//今天日期 System.out.println(d2.compareTo(d1));//1 System.out.println(d1.equals(new Date(20000)));//t } }
import java.util.*; class TestCalendar { public static void main(String[] args){ Calendar c = Calendar.getInstance(); display(c); c.set(Calendar.YEAR,2009); c.set(Calendar.MONTH,4); c.set(Calendar.DATE,30); display(c); c.set(2009,8,12); display(c); c.set(2009,8,12,10,23,15); display(c); c.set(Calendar.MONTH,2); display(c); Date d = c.getTime(); System.out.println(d); } public static void display(Calendar c){ String s = c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日"+c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND)+" "+(c.get(Calendar.AM_PM)==0?"上午":"下午"); System.out.println(s); } }
基本数学功能类Math