java的帮助文档(API)

java的帮助文档(API)

1课程目标
1: 学会查询查看帮助文档。
2: 通过java的帮助文档,能过自我学习更多类和方法。

2课程笔记
2.1如何查看帮助文档。

3String 常用方法:

/**
*
*

  • String API
  • 构造方法 String();
  •      String(byte[] bytes);
    
  •      String("abc");
    
  •      String("char[] chars);
    

*/
public class Test {

public static void main(String[] args) {
	// String 空参构造方法。
	String s1 = new String();
	System.out.println(s1);
	// byte数组构造方法。
	byte[] bs = {1,2,3};
	String s2 = new String(bs);
	System.out.println(s2);
	
	// char类型数
	char[] chars = {'a','b','c'};
	String s3 = new String(chars);
	System.out.println(s3);
	
	//String字符串构建
	String s4 = new String("abc");
	
	// String 
	String s5 = "abc";

}

}

/**
*

  • String 对象实例化对比。

*/
public class Text {

public static void main(String[] args) {
	String str = "abc";// 直接使用常量赋值的字符串,常量值就在堆的字符串缓冲区。
	String str1 = new String("abc");// 在堆中创建空间。
	
	// str==str1
	System.out.println(str==str1);
	// str.equals(str1);
	System.out.println(str.equals(str1));
	
	
	String str2 = new String("abc");
	String str3 = new String("abc");
	// str2==str3
	System.out.println(str2==str3);// 不相等。
	
	
	String str4 = "abc";
	String str5 = "abc";
	System.out.println(str4==str5);// abc在字符串缓冲池里是个常量。
	
	
	// 问题: String str = "abc"; 表示字符串定义的常量值不会发生改变。
	       str4 = str4+"d";
	System.out.println(str4);// str4 的常量值不会发生改变。
	
	// String 定义的字符串是唯一的不可变的。
	
	String a = "a";
	 a = "a"+"b";
	 a = a+"c";
	 a = "a"+a+"d";
	 a = "a"+a;

}

}

public class StringDemo {

public static void main(String[] args) {
	String url = "www.baidu.com";
	// charAt 返回指定位置的字符。
	char a = url.charAt(4);// 根据索引差到字符。
	System.out.println(a);
	
	// indexOf
	int b = url.indexOf(".");// 根据字符返回下标
	System.out.println(b);
	
	// lastindexOf
	int c = url.lastIndexOf(".");// 根据最后一次出现的字符,返回下标。
	System.out.println(c);
	
	
	// 字符串比较
	String d = "hello";
	String e = "hello1";
	System.out.println(d.compareTo(e));
	
	// 字符串拼接。
	d  = d.concat("word");
	System.out.println(d);

	// 是否包含某个字符串
	containsDemo();
	
	startWithDemo();
	
	equalshDemo();
	
	getBytesDemo();
	
	toCharArrayDemo();
	
	toLowerCaseDemo();
	
	trimDemo();
	
}
// trim 去除字符串前后的空格。
public static void trimDemo(){
	String url1 = " www.baid u.com ";
	System.out.println("原字符串长度"+url1.length());
	String trim = url1.trim();
	System.out.println(trim);
	System.out.println("去除空格后的长度"+trim.length());
	
	
	
}



//toLowerCase() 转换成小写   toUpperCase() 转换成大写
public static void toLowerCaseDemo(){
	String url = "HELLO WORD";
	String lowerCase = url.toLowerCase();
	System.out.println(lowerCase);
	String upperCase = url.toUpperCase();
	System.out.println(upperCase);
	
}
  



// toCharArray
public static void toCharArrayDemo(){
	String url = "abc";
	char[] charArray = url.toCharArray();
	System.out.println(Arrays.toString(charArray));
	
}



// getBytes
public static void getBytesDemo(){
	String url = "abc";
	byte[] bytes = url.getBytes();
	System.out.println(Arrays.toString(bytes));
	
}



// equals    equalsIgnoreCase
public static void equalshDemo(){
	String url = "www.baidu.com";
	String url1 = "WWW.baiDU.Com";
	System.out.println(url.equals(url1));
	System.out.println(url.equalsIgnoreCase(url1));// 忽略大小写比较
	
}



// startsWith  endsWith// 判断前缀后缀是否相同。
public static void startWithDemo(){
	String url = "www.baidu.com";
	System.out.println(url.startsWith("www"));
	System.out.println(url.endsWith("com"));
}



// contains
public static void containsDemo(){
	String url = "www.baidu.com";
	System.out.println(url.contains("baid5"));
}

}

public class StringDemo02 {

public static void main(String[] args) {
	split();
	subStringDemo();
	
	// 完成以下演示。
	/*
	 * replace();
	 * valueOf();
	 * ifEmpty();
	 */

}

public static void subStringDemo(){
	// 字符串截取。
	String url = "http://www.baidu.com/news";
	// 获取http:
	String substring = url.substring(7,10);// 从哪里开始,到哪里结束。
	System.out.println(substring);
	
	
	// 获取www,我有一个办法。
	int index = url.indexOf("/");
	int last = url.indexOf(".");
	System.out.println(index);
	System.out.println(last);
	String substring2 = url.substring(index+2, last);
	System.out.println(substring2);
}


// 字符串切割
public static void split(){
	String str = "a.b.c.d.e.f";
	String[] split = str.split("\\.");// 某些字符需要转义。
	System.out.println(split[0]);
	System.out.println(split[1]);
	System.out.println(split[2]);
	System.out.println(split[3]);		
}

}

4StringBuffer & StringBuilder
/**
*

  • String 对象,以及方法。
  • StringBuffer StringBuilder
  • StringBuffer: 字符串缓冲类:程安全的可变字符序列。一个类似于 String 的字符串缓冲区
  • StringBuilder: 一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步.线程不安全。
  • String 和 StringBuffer StringBuilder 区别。
  • String 表示一组不可变的字符序列。对于字符串的操作,将会创建新的字符串对象。 不适用于频繁操作字符串。
  • StringBuffer 线程安全的可变字符序列。对于字符的操作,将在其对象方法中完成,不会创建新的字符串对象。适用于多线程操作
  • StringBuilder 非线程安全的可变字符序列。对于字符的操作,将在其对象方法中完成,不会创建新的字符串对象。适用于单线程操作。

*/
public class Test {

public static void main(String[] args) {
	// String 的使用。
	String a = "a";
	       a = a+"b";
	       
	// 问题:一共有几个String对象。
	       
	// StringBuffer的使用。
	StringBuffer sb = new StringBuffer("a");
	// 向其内部追加。
	sb.append("b");
	// 向其内部插入,只允许前后多加1位。
	sb.insert(0, "[");
	sb.insert(sb.length(), "]");
	sb.insert(4, "z");
	// 删除某一位
	sb.delete(0,1);
	// 反转。
	sb.reverse();
	System.out.println(sb.toString());
	
	// 问题:sb一共有几个对象?
	

	// StringBuilder 的使用
	StringBuilder sb1 = new StringBuilder("a");
	sb1.append("b");
	sb1.insert(0, "{");
	sb1.insert(sb1.length(), "}");
	sb1.delete(0,1);
	System.out.println(sb1.capacity() );
	System.out.println(sb1.length() );
	
}

}

58种基本数据类型包装类。
/**
*

  • 基本数据类型 包装类
  • byte Byte
  • short Short
  • int Integer
  • long Long
  • float Float
  • double Double
  • char Character
  • boolean Boolean

*Integer Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。
*

  • int Integer 的区别:
  • int 是基本数据类型,Integer是引用数据类型,也是int包装类。提供了对整数类型操作的方法。允许自动拆装箱。

*/
public class Test2 {

public static void main(String[] args) {
	// 基本数据类型
	int a = 10;
	// 包装类型   自动装箱。
	Integer b = 10;
	// 包装类型,自动拆箱
	int c = b;
	
	// 包装类提供了关于基本数据转换的方法。和 属性。
	// 包装类的属性
	System.out.println(Integer.MAX_VALUE);
	System.out.println(Integer.MIN_VALUE);
	
	// 构造方法。
	Integer d = new Integer("123");
	System.out.println(d+1);
	
	// 常用方法。
	System.out.println(b.compareTo(d));// 比较前后两个值大小,返回一个整数。
	// 
	System.out.println(d.doubleValue());
	
	String abc = "154";
	int parseInt = Integer.parseInt(abc);
	System.out.println(parseInt);
	
	// Integer 的默认值是什么 null.

	
	
	// 测试对象相等的方法。
	IntegerEquals();
	

}

public static void IntegerEquals(){
	Integer a = 128;
	Integer b = 128;// 在Integer中,默认开辟1字节空间。
	System.out.println(a==b);
	System.out.println(a.equals(b));
	
	System.out.println("*********************");
	Integer c = new Integer(10);
	Integer d = new Integer(10);
	System.out.println(c==d);
}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值