码迷,mamicode.com
首页 > 编程语言 > 详细

java12.API

时间:2016-08-10 21:10:58      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

day12
面试题
  String s1 = "abc";
  String s2 = "abc";
  System.out.println(s1 == s2); //true 比较的是地址,如果常量池中已经有了"abc"字符串,那么就直接将该字符串地址给s2而不是新建
  System.out.println(s1.equals(s2)); //true 比较的是属性值,相等的
  
  String s1 = new String("abc"); //新建2个对象,一个是常量池中的"abc"一个是String对象
*/  
  /*String s1 = new String("abc");   
  String s2 = "abc";
  System.out.println(s1 == s2);  //false 因为new的对象进堆内存,不是常量池中的"abc"因而地址也不同
  System.out.println(s1.equals(s2)); //true 比较的是属性值,相等的
*/ 
 /* String s1 = "a" + "b" + "c";
  String s2 = "abc";
  System.out.println(s1 == s2);   //true 常量优化机制,s1编译的时候就变成"abc"了~
  System.out.println(s1.equals(s2)); //true
*/  
  /* String s1 = "ab";    //常量池的地址
   String s2 = "abc";    //常量池的地址
   String s3 = s1 + "c";   //先创建StringBuffer对象,ToString对象的地址赋给s3,s3存着的地址是堆里的地址
   System.out.println(s3 == s2); //false 
   System.out.println(s3.equals(s2)); //true 
*/

String构造函数:
 * public String():空构造
 * public String(byte[] bytes):把字节数组转成字符串
 * public String(byte[] bytes,int index,int length):把字节数组的一部分(从索引index开始以length为长度)转成字符串
 * public String(char[] value):把字符数组转成字符串
 * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
 * public String(String original):把字符串常量值转成字符串 

空串和null的区别:空串("")是一个字符串常量同时也是一个String类的对象,既然是对象当然可以调用String类中的方法
 null是空常量,不能调用任何的方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值

String常用方法:
1.判断:
 * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
 * boolean contains(String str):判断大字符串中是否包含小字符串
 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
 * boolean isEmpty():判断字符串是否为空。

2.获取 
 * int length():获取字符串的长度。
 * char charAt(int index):获取指定索引位置的字符
 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
 * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
 * lastIndexOf(int ch,int fromIndex)
 * lastIndexOf(String str,int fromIndex)
 * String substring(int start):从指定位置开始截取字符串,默认到末尾。
 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

3.转换:
 * byte[] getBytes():把字符串转换为字节数组。
 * char[] toCharArray():把字符串转换为字符数组。
 * static String valueOf(char[] chs):把字符数组转成字符串。
 * static String valueOf(int i):把int类型的数据转成字符串。
  * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
 * String toLowerCase():把字符串转成小写。(了解)
 * String toUpperCase():把字符串转成大写。
 * String concat(String str):把字符串拼接。

java12.API

标签:

原文地址:http://www.cnblogs.com/meng726477179/p/5758276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!