基本数据类型不具有方法属性,而引用数据类型可以有属性和方法 使用更加灵活
Byte Short Integer Long Float Double Character Boolean
Byte Short Integer Long Float Double 是Number的子类 可以通过xxValue()将某一类型转换成其他类型
Integer a = 100; Byte b = a.byteValue(); Short c = a.shortValue(); Long d = a.longValue(); Float e = a.floatValue(); Double f = a.doubleValue(); Integer g = a.intValue(); //将字符串转换成int或者是double String s = "123"; //方式1: int i = Integer.parseInt(s); //方式2: int i2 = Integer.valueOf(s);缓冲区
public class Demo02 {
public static void main(String[] args) {
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.out.println(i1 == i2); //false
System.out.println(i1.equals(i2));//true
Integer i3 = 1000; //Integer i3 = new Integer(1000);
Integer i4 = 1000; //Integer i3 = new Integer(1000);
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4));//true
Integer i5 = 100; //IntegerCache.cache[i + (-IntegerCache.low)]
Integer i6 = 100; //IntegerCache.cache[i + (-IntegerCache.low)]
System.out.println(i5 == i6);//true
System.out.println(i5.equals(i6));//true
}
}
String
常用方法
String str = "abcn12c3fcds"; charAt(index)
获取指定下标对应的字符,返回char类型
indexOf("字符串")
System.out.println(str.indexOf("cn2"));
获取指定字符串在原字符串中的下标,如果不包含该字符串则返回-1
System.out.println("abcd".equalsIgnoreCase(str))
判断两个字符串是否相等,忽略大小写
System.out.println(str.startsWith("12"));
判断字符串是否以指定的字符串开头,指定开始位置
str = str.substring(0, 5);
字符串截取,从指定的下标开始和结束 范围是左闭右开
//去除字符串前后的空格 String str = " n你好。。 哈哈 "; str = str.trim(); System.out.println(str);
去除字符串前后的空格
//String str = "abcADC你好"; //将字符串中的字母变成大写 //System.out.println(str.toUpperCase()); //将字符串中的字母变成小写 //System.out.println(str.toLowerCase());
大小写转换
StringBuffer、StringBuilder进行拼接时,Buffer线程安全 但是效率低 Builder线程不安全 但是效率高
public class StringDemo {
public static void main(String[] args) {
String s1 = "ab";
String s2 = "c";
String s3 = new String("abc");
String str1 = "abc";
String str2 = "abc";
String str3 = "ab" +"c";
String str4 = s1 +s2;
String str5 = new String("abc");
String str6 = new String("ab")+"c";
String str7 = s3.intern();
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//true
System.out.println(str1 == str4);//false
System.out.println(str1 == str5);//false
System.out.println(str5 == str6);//false
System.out.println(str1 == str6);//false
System.out.println(str1 == str7);//true
}
}
暑期编程PK赛
得CSDN机械键盘等精美礼品!


