——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
1.基本数据类型包装类:
基本数据类型对象包装类:
byte:基本数据类型 Byte:引用数据类型
short Short
int Integer
boolean Boolean
float Float
double Double
char Character
基本数据类型对象包装类的最常见作用
就是用于基本数据类型和字符串数据类型之间做转化
基本数据类型转成字符串
基本数据类型+””
基本数据类型.toString(基本数据类型值);
如:Integer.toString(56);//将56正数抓换成”56”;
字符串转成基本数据类型
基本数据类型 a = 引用数据类型.parse基本数据类型(String)
例如:int a = Integer.parseInt(“1134”);
double b = Double.parseDouble(“12.34”);
boolean b = Boolean.parseBoolean(“true”);
JDK1.5版本以后,自动装箱,如果装箱的是一字节,那么该数据会被共享,不会重新开辟空间。
代码1:
class IntegerDemo
{
public static void main(String[] args)
{
//正数类型最大值
sop("int max:"+Integer.MAX_VALUE);
int num = Integer.parseInt("1011",16);
sop("num="+num);
sop(Integer.toHexString(50));
boolean b = Boolean.parseBoolean("true");
sop(b);
}
public static void sop(String str)
{
System.out.println(str);
}
}
代码2:
class IntegerDemo1
{
public static void main(String[] args)
{
Integer x = new Integer("123");
Integer y = new Integer(123);
sop("x==y:"+(x==y));//x和y是两个不同对象,所以为false。
sop("x.equals(y):"+x.equals(y));//Integer复写了Object的equals的方法,x和y的值相同。
//Integer d = 2;//自动装箱,相当于Integer d = new Integer(4);
//d=d+3;
//d+3:d自动拆箱,变成了int类型,和3进行加法运算,再将和进行装箱赋值给d。
Integer a = 128;
Integer b = 128;//false
sop("a==b:"+(a==b));//超过了byte的范围,开辟了新空间。
int z = a.compareTo(b);
System.out.println(z);
Integer m = 127;
Integer n = 127;//true,因为m和n指向了同一个Integer对象。
sop("m==n:"+(m==n));//因为当数值在byte范围内容,对于新特性,如果该数值已经存在,就不会再开辟新空间。
}
public static void sop(String str)
{
System.out.println(str);
}
}
2.String类:
2.1 String:
字符串最大特点:
一旦被初始化就不可以被改变。
String s1 = “ace”
s1是一个类类型变量,”ace”是一个对象。
例如:
String s1 = “ace”;
String s1 = new String(“ace”);
s1==s2 false
s1和s2有什么区别:
s1,s2是两个不同对象。(s2是new后的新对象)—
s1.equals(s2) true
String类复写了Object类中equals方法,该方法用于判断字符串是否相同。
String类是用于描述字符串事物。
那么它就提供了多个方法对字符串进行操作
基本操作代码:
class StringDemo
{
public static void main(String[] args)
{
method_get();
method_trans();
method_replace();
method_split();
method_sub();
method_compare();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void method_get()
{
String str = "StringDemo.java";
sop(str.length());
//获取字符长度
sop(str.charAt(3));
//获取该位置上的某个字符
sop(str.indexOf('q'));
//获取该字符在字符串中第一次出现的位置
sop(str.indexOf('a',13));
//获取从指定位置开始,该字符在字符串中第一次出现的位置
sop(str.lastIndexOf('a'));
//获取从最后开始查询,字符第一次出现的位置(角标仍然不变,从前到后固定的)
sop(str.startsWith("String"));
//判断字符串是否以指定内容开头
sop(str.endsWith(".java"));
//判断字符串是否以指定内容结尾
sop(str.isEmpty());
//判断字符串中是否有内容,原理也就是判断长度是否为0
sop(str.contains("Demo"));
//判断字符串中是否包含某一个子
if(str.indexOf(".java")!=-1)
//等于-1说明字符串中不存在这个子串(注:该方法既可以判断字符串中是否存在该字符或者子串,又可以获取该字符或者子串出现的位置。)
{
System.out.println("cunzai");
//因为存在.java这个子串,所以条件成立。
}
sop(str.equals("stringdemo.JAVA"));
//判断字符串内容是否相同,复写了Object类中的equals方法。
sop(str.equalsIgnoreCase("stringdemo.JAVA"));
//判断内容是否相同,并忽略大小。
}
public static void method_trans()
{
char[] arr={'a','b','c','d','e','f','g'};
//字符数组
String str=new String(arr);
//将字符数组转化成字符串
sop("str="+str);
String st=new String(arr,2,5);
//2起始角标,5是字符串长度。
sop("st="+st);
String s1 = "lifetime";
char[] ch1 = s1.toCharArray();
//将字符串转化为字符数组
sop("ch1="+ch1);//输出的是字符数组的地址值
for(int x=0;x<ch1.length;x++)
{
System.out.println(ch1[x]);
}
}
public static void method_replace()
{
String s = "hello java";
String s2=s.replace('a','b');
//如果要替换的字符不存在,返还的还是原字符串。
String s3=s.replace("java","world");
//替换字符串
sop("s="+s);
sop("s2="+s2);
sop("s3="+s3);
}
public static void method_split()
{
String s = "zhangsan,lisi,wangwu";
String[] arr=s.split(", ");
//切割
for (int x=0; x<arr.length;x++)
{
sop(arr[x]);
}
}
public static void method_sub()
{
String s="abcdefg";
sop(s.substring(0));
//从指定位置开始到结尾,如果角标不存在,会出现字符串角标越界异常。StringIndexOutOfBoundsException
sop(s.substring(2,5));
//包含头,不包含尾 s.substring(0,s.length):整个字符串;
}
public static void method_compare()
{
String s = " HellO jaVa ";
sop(s.toUpperCase());
//将字符串转成大写
sop(s.toLowerCase());
//将字符串转成小写
sop(s.trim());
//将字符串两端的多个空格去除
String s1 = "abe";
//两个字符串依次从首字符互相比较(比较ASCII码值)
String s2 = "abc";
//如果此字符串s1等于参数字符串s2返回值为0,如果此字符串小于参数字符串,则返回一个负数
sop(s1.compareTo(s2));
//如果此字符串大于参数字符串,则返回一个正数。
}
}
2.2 StringBuffer:
如字符串缓冲区,是一个容器。
1)长度是可变化的。
2)可以操作多个数据类型化。
3)最终会通过toString方法方法变成字符串。
1.存储:
StringBuffer append():将指定数据作为参数添加到已有数据结尾处。
StringBuffer insert(index,数据):可以将数据插入到指定index位置。
按顺序将 String 参数中的字符插入此序列中的指定位置,将该位置处原来的字符向后移,此序列将增加该参数的长度。
2.删除
StringBuffer delete(int start,int end):删除缓冲区中的数据,包含start,不包含end。
StringBuffer deleteCharAt(int index):删除指定位置的字符。
3.获取
charAt(int index)
indexOf(String str)
lastIndexOf(String str)
length()
substring(int start, int end)
4.修改
StringBuffer replace(int start, int end, String str):包含头不包含尾。
void setCharAt(int index, char ch)
不返回,返回值类型是void。
5.反转
StringBuffer reverse()
6.void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。(包含头不包含尾)
JDK 1.5版本以后出现了StringBuilder
StringBuilder是线程不同步的。(一般单线程使用)
StringBuffer是线程同步的。(一般多线程使用)
以后开发,建议使用StringBuilder。
java升级一般从以下三种考虑:
1.提高效率
2.简化书写
3.提高安全性
基本操作代码:
class StringBufferDemo
{
public static void main(String[] args)
{
//method_add();
//method_del();
//method_get();
//method_update();
method_rev();
}
public static void method_rev()
{
StringBuffer sb = new StringBuffer("abcde");
sb.reverse();//edcba
sop(sb.toString());
}
public static void method_update()
{
StringBuffer sb = new StringBuffer("abcde");
sb.replace(1,3,"wokao");
//包含头不包含尾,替换。
sb.setCharAt(3,'c');
//abcce
sop(sb.toString());
//awokaode
}
public static void method_get()
{
StringBuffer sb = new StringBuffer("abcde");
sop(sb.substring(1,3));
}
public static void method_del()
{
StringBuffer sb = new StringBuffer("abcdef");
//sb.delete(1,4);
//清空缓冲区
//sb.delete(0,sb.length());
sb.deleteCharAt(2);
//相当于sb.delete(2,3);
sop(sb.toString());
}
public static void method_add()
{
StringBuffer sb = new StringBuffer();
//创建一个字符串缓冲区对象
StringBuffer sb1=sb.append(34);
sop("sb==sb1:"+(sb==sb1));//true
sop(sb.toString());//都是字符串34
sop(sb1.toString());//都是字符串34
sb.append("asb").append(true).append(9898).insert(1,"qqqq");;
//可以接受任意类型的数据(基本数据类型除了short和byte,其他都包含.)
sop(sb.toString());//3qqqqq4asbtrue9898。
}
public static void sop(String str)
{
System.out.println(str);
}
}
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-