1:封装类
所有的基本类型,如int,都有自己对应的类类型,int对应的类类型是Integer。对应的这种类类型,就叫做封装类。如下图所示
2:基本类型转封装类
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
Integer it = new Integer(i);//基本类型装换成封装类型
}
}
3:封装类型转换为基本类型
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
Integer it = new Integer(i);
int i2 = it.intValue();//封装类型转换为基本类型
}
}
4:自动装箱
不通过构造方法,直接通过=号来使基本类型转化为封装类类型
public class zhulei {
public static void main(String[] args) {
int i = 1;
Integer it = new Integer(i);//通过构造方法使基本类型转化为类类型
Integer i3 = i;//通过=号来使基本类型转化为类类型
}
}
5:自动拆箱
不通过.intValue(),通过=号将封装类类型转化为基本类型
public class zhulei {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
Integer i3 = i;
int i2 = i3.intValue();//通过.intValue()将封装类类型转化为基本类型
int i4 = i3;//通过=号将封装类类型转化为基本类型
}
}
6:通过Integer来获取int的最大值
public class zhulei {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
}
}
7:什么时候能自动拆箱装箱
只有对应类型才能进行自动装箱和拆箱
public class zhulei {
public static void main(String[] args) {
byte l = 1;
// byte o = 1;
// Integer o1 = o;//不同类型不能进行自动装箱
Integer a = new Integer(l);//不同类型可以进行手动装箱
byte a1 = a.byteValue();//不同类型可以进行手动拆箱
}
}
8:装箱有啥作用之将数字转化为字符串
public class zhulei {
public static void main(String[] args) {
int i = 1111;
String i1 = String.valueOf(i);//方法一:用String类中的静态方法valueOf()
Integer i2 = i;//方法二:将int型变量装箱为Integer类类型对象,再调用其中的toString()方法
String i3 = i2.toString();
}
}
9:装箱有啥用之将字符串转化为数字
public class zhulei {
public static void main(String[] args) {
String str1 = "11";
int i = Integer.parseInt(str1);//使用Integer类的静态方法.parseInt()
System.out.println(i);
}
}
10:math数学公式
Math.random()是取[0,1)之间的数
double精度大于float
定义float时要在数后面加上f
eg:float a = 3.13f;
public class zhulei {
public static void main(String[] args) {
float a1 = 3.14f;//
float a2 = 3.74f;
System.out.println(Math.round(a2));//取整
System.out.println(Math.random());//[0,1)随机数
System.out.println((int)(Math.random()*10));
System.out.println(Math.sqrt(7));//开方
System.out.println(Math.pow(3,2));//3的2次方
System.out.println(Math.PI);
System.out.println(Math.E);
}
}
public class zhulei {
public static void main(String[] args) {
double e ;
int n = Integer.MAX_VALUE;//n能取到得最大值
e = Math.pow((1+1d/n), n);//以前写1/n总是得0,是因为1/n默认int类型,现在1d/n是doble类型
System.out.println(e);
}
}
11:格式化输出
public class zhulei {
public static void main(String[] args) {
String name = "盖伦";
int kill = 4;
String sentence = "英雄"+name+"击杀了"+kill+"个敌人";//不用格式化输出就要使用字符串连接
System.out.println(sentence);
String formatsentence = "英雄%s击杀了%d个敌人";
System.out.printf(formatsentence,name,kill);
System.out.format(formatsentence,name,kill);//format和printf功能相同效果一样
//使用格式化输出要用printf
//println和print不能用于格式化输出,会报错
//println换行,print不换行
}
}
12:常用的格式化方式
public class zhulei {
public static void main(String[] args) {
int year = 100;
System.out.printf("%d%n",year);//数字
System.out.printf("%8d%n",year);//八位占空,右对齐
System.out.printf("%-8d%n",year);//左对齐
System.out.printf("%08d%n",year);//0补位
System.out.printf("%,d%n",year*10000000);//,千位分位
System.out.printf("%.4f",Math.PI);//小数点后四位有效数字
}
}
练习
public class zhulei {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入地名");
String palcename = scanner.nextLine();//nextline接收到回车算停止
//和next的区别是next须接受有效字符,即在没输入有效字符前输入的空格是不算数的
System.out.println("请输入公司名");
String companyname = scanner.nextLine();
System.out.println("老板名");
String bossname = scanner.nextLine();
System.out.println("请输入欠款金额");
float money = scanner.nextFloat();//nextfloat只接受数字,最后的回车是不接受的
//导致使nextfloat结束的回车直接被"带着谁跑了"给接收了,然后"带着谁跑了"结束了。
scanner.nextLine();
System.out.println("带着谁跑了");
String runname = scanner.nextLine();
String format = "%s最大皮革厂%s倒闭了,王八蛋老板%s吃喝嫖赌,欠下了%.1f个亿,带着%s跑了";
System.out.printf(format,palcename,companyname,bossname,money,runname);
}
}
13:char的用法
public class zhulei {
public static void main(String[] args) {
char c1 = '1';
char c2 = 'a';
char c3 = '中';
// char c4 = 'ab';//报错,只能放一个字符
}
}
14:char的拆箱和装箱
public class zhulei {
public static void main(String[] args) {
char c1 = '1';
Character a = c1;//自动装箱
c1 = a;//自动拆箱
}
}
15:character类中的常用方法
public class zhulei {
public static void main(String[] args) {
char c1 = 'a';
Character a = c1;//自动装箱
System.out.println(Character.toUpperCase('a'));//转换为大写
System.out.println(Character.isDigit('1'));//是数字吗
System.out.println(Character.isLetter('a'));//是字母吗
System.out.println(Character.isWhitespace(' '));//是空格吗
System.out.println(Character.toString('a'));//将字符转化为字符串
}
}
16:常见转义
public class zhulei {
public static void main(String[] args) {
System.out.println("abc ed");
System.out.println("a ed");
System.out.println("abcdd ed");
// abc ed
// a ed
// abcdd ed
System.out.println("abc\ted");
System.out.println("a\ted");
System.out.println("abcdd\ted");
// abc ed
// a ed
// abcdd ed
System.out.println("看我打出一个制表符来\\t");
System.out.println("一个\\t的长度是8");
System.out.println("看我打印出一个单引号来\'");//单引号用不用反斜杠都能打出来
System.out.println("打一个反斜杠出来\\");
System.out.println("看我打印出一个双引号来\"");//双引号必须打反斜杠
}
}
16:字符串
String是一个类,所有的字符串都是String类类型的对象
String类被final修饰,不能被继承
public class zhulei {
public static void main(String[] args) {
String str = "abc123F";
String a = new String("ad");//通过new生成字符串对象
System.out.println(a);
char[] b = new char[] {'我','是','你'};
String b1 = new String(b);
System.out.println(b1);
}
}
17:格式化输出
这个新在他用了String.format()
public class zhulei {
public static void main(String[] args) {
String name ="盖伦";
int kill = 8;
String title="超神";
String format = "%s杀了%d个人,已经%s超神了";
String formatsentence = String.format(format, name,kill,title);
System.out.printf(formatsentence);
}
}
18:ascii码的英文字母对照表
public class zhulei {
public static void main(String[] args) {
char a = 'a';
char A = 'A';
char Z = 'Z';
char z = 'z';
int a1 = (int) a;
int z1 = (int) z;
int A1 = (int) A;
int Z1 = (int) Z;
System.out.println(a1);//97
System.out.println(z1);//122
System.out.println(A1);//65
System.out.println(Z1);//90
}
}
19:练习生成5位随机字符包括大小写和数字
public class zhulei {
public static void main(String[] args) {
char[] cs = new char[5];
int min = '0';//字符赋给int型就转化为ascii码了
int max = 'z'+1;
for(int i = 0;i<cs.length;i++)
{
while(true) //因为0-122ascii码除了数字和字符还有别的
{
char a = (char)((Math.random())*(max-min)+min);//random()生成[0,1)
if(Character.isLetter(a)||Character.isDigit(a))
{
cs[i] = a;
break;
}
}
}
System.out.println(cs);
}
}
20:操纵字符串
charAt() 返回指定位置的字符
public class zhulei {
public static void main(String[] args) {
String a = "大家好,维欧氏阿城市1234";
System.out.println(a.charAt(3));
}
}
toCharArray()将字符串转化为字符数组
public class zhulei {
public static void main(String[] args) {
String sentence = "我是你爹";
char[] a = sentence.toCharArray();
System.out.println(a.length==sentence.length());
}
}
substring()字符串截取
public class zhulei {
public static void main(String[] args) {
String sentence = "我是你爹123541545,你是我爹";
System.out.println(sentence.substring(3));//从3到最后
System.out.println(sentence.substring(3, 7));//左闭右开
}
}
split()依据指定符号分割
public class zhulei {
public static void main(String[] args) {
String sentence = "我是你爹,123541545,你是我爹";
String[] subsentence = sentence.split(",");
for(String sub:subsentence)
{
System.out.println(sub);
}
}
}
trim()去掉前后的空格
public class zhulei {
public static void main(String[] args) {
String sentence = " 我是你爹,123541545,你是我爹 ";
System.out.println(sentence);
System.out.println(sentence.trim());
}
}
toUpper() toLower()全变成大写或者小写
public class zhulei {
public static void main(String[] args) {
String sentence = "fajhfrjagrNHILDulhfd";
System.out.println(sentence.toUpperCase());
System.out.println(sentence.toLowerCase());
}
}
定位
public class zhulei {
public static void main(String[] args) {
String sentence = "fajahfrjagrNHILDulhfd";
System.out.println(sentence.indexOf('a'));//第一次出现字符a的位置
System.out.println(sentence.indexOf("ja"));//第一次出现字符串ja的位置
System.out.println(sentence.lastIndexOf('a'));//最后一次出现字符a的位置
System.out.println(sentence.indexOf('a',2));//从第二个位置开始,第一次出现字符a的位置
System.out.println(sentence.contains("frj"));//字符串中是否包含frj字符串
}
}
字符串替换
public class zhulei {
public static void main(String[] args) {
String sentence = "超神超神红红火火恍恍惚惚";
String temp = sentence.replaceAll("超神", "超鬼");//全部超神替换为超鬼
String temp1 = sentence.replaceFirst("超神", "死鬼");//第一个超神替换为死鬼
System.out.println(sentence);
System.out.println(temp);
System.out.println(temp1);
}
}
练习
//给出一句英文句子: “let there be light”
//得到一个新的字符串,每个单词的首字母都转换为大写
public class zhulei {
public static void main(String[] args) {
String sentence = "let there be light";
String[] subsentence = sentence.split(" ");//每个数组是一个字符串
int i = 0;
for(String sub:subsentence)
{
subsentence[i++] = Character.toUpperCase(sub.charAt(0))+sub.substring(1);
}
System.out.println(subsentence);
}
}
练习
英文绕口令
peter piper picked a peck of pickled peppers
统计这段绕口令有多少个以p开头的单词
public class zhulei {
public static void main(String[] args) {
String a = "peter piper picked a peck of pickled peppers";
String[] suba = a.split(" ");
int i =0;
for(String sub :suba)//sub是字符串形式的
{
if(sub.charAt(0)=='p')
{
i++;
}
}
System.out.println(i);//6
}
}
练习
把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
public class zhulei {
public static void main(String[] args) {
String a = "lengendary";
char[] b = a.toCharArray();
for(int i=0;i<b.length;i++)
{
if((i%2==0)&&(Character.isLowerCase(b[i])))
{
b[i]=Character.toUpperCase(b[i]);
}
}
System.out.println(b);
}
}
练习
把 lengendary 最后一个字母变大写
字符串转化为字符数组
字符数组转化为字符串
public class zhulei {
public static void main(String[] args) {
String a = "lengendary";//字符串
char[] b = a.toCharArray();//字符串转换为字符数组
b[b.length-1] = Character.toUpperCase(b[b.length-1]);
String c = String.valueOf(b);//字符数组转化为字符串数组
System.out.println(c);
}
}
练习
把最后的two的t大写
public class zhulei {
public static void main(String[] args) {
String a = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";//字符串
char[] b = a.toCharArray();//将字符串转化为字符数组
b[a.lastIndexOf("two")] = Character.toUpperCase(b[a.lastIndexOf("two")]);
System.out.println(b);
}
}
是否是同一个字符串
==用来判断两个字符串对象是否是同一个
虽然str1和str2的内容一样,但是不是同一个对象
因为str1的内容是light了,所以在创建str3时发现已经存在str1的内容和其一样,就直接拿来用了,不创建新对象了
public class zhulei {
public static void main(String[] args) {
String str1 = "light";
String str3 = "light";
String str2 = new String(str1);
System.out.println(str1==str2);//false
System.out.println(str1==str3);//true
}
}
判断字符串的内容是否相等
//使用equals进行字符串内容的比较,必须大小写一致
//equalsIgnoreCase,忽略大小写判断内容是否一致
public class zhulei {
public static void main(String[] args) {
String str1 = "light";
String str2 = "light";
String str3 = "Light";
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写
}
}
字符串以什么开始,什么结束
//startsWith //以...开始
//endsWith //以...结束
public class zhulei {
public static void main(String[] args) {
String str1 = "light";
System.out.println(str1.startsWith("li"));
System.out.println(str1.endsWith("t"));
}
}
20:StringBuffer()
StringBuffer是可变长度的字符串
append添加 delete删除 insert插入 reverse反转
public class zhulei {
public static void main(String[] args) {
String str = "i am";
StringBuffer a = new StringBuffer(str);
a.append(" a student");
System.out.println(a);
a.delete(0, 2);
System.out.println(a);
a.insert(0, "i ");
System.out.println(a);
a.reverse();
System.out.println(a);
}
}
StringBuffer的长度和容量的区别
public class zhulei {
public static void main(String[] args) {
String str = "i am";
StringBuffer a = new StringBuffer(str);
System.out.println(a.length());//长度是3
System.out.println(a.capacity());//容量是20
//所以说StringBuffer是可变长度的字符串
}
}
使用StringBuffer和String链接的时间比对
结果StringBuffer比String性能强很多
public class zhulei {
public static void main(String[] args) {
String str1 = random.getString(10);
StringBuffer str2 = new StringBuffer(str1);
String str = "";
double timestart1 = System.currentTimeMillis();
for(int i = 0;i<10000;i++)
{
str += str1;
}
double timeend1 = System.currentTimeMillis();
System.out.println(timeend1-timestart1);
double timestart2 = System.currentTimeMillis();
for(int i = 0;i<100000;i++)
{
str2.append(str1);
}
double timeend2 = System.currentTimeMillis();
System.out.println(timeend2-timestart2);
}
}
public class random {
public static String getString(int length)
{
int min = '0';
int max = 'z';
char[] c = new char[length];
for(int i =0;i<length;i++)
{
while(true)
{
char test =(char)(Math.random()*(max-min)+min);
if(Character.isAlphabetic(test)||Character.isDigit(test))
{
c[i] =test;
break;
}
}
}
String a = String.valueOf(c);//字符数组转化为字符串
return a;
}
}
自定义的MyStringBuffer
public class MyStringBuffer implements IStringBuffer {
private String str ="";
public MyStringBuffer(String str) {
this.str = str;
}
public String toString() {
return str;//外部可以通过该公有方法访问到私有变量str
}
@Override
public void append(String str) {
// TODO Auto-generated method stub
this.str = this.str+str;
}
@Override
public void append(char c) {
// TODO Auto-generated method stub
//将字符转化为字符串的两种方法
// this.str = this.str +String.valueOf(c);
this.str = this.str +Character.toString(c);
}
@Override
public void insert(int pos, char b) {
// TODO Auto-generated method stub
this.str = this.str.substring(0, pos)+Character.toString(b)+this.str.substring(pos);
}
@Override
public void insert(int pos, String b) {
// TODO Auto-generated method stub
this.str = this.str.substring(0, pos)+b+this.str.substring(pos);
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
this.str = this.str.substring(0, start);
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
this.str = this.str.substring(0, start)+this.str.substring(end);
}
@Override
public void reverse() {
// TODO Auto-generated method stub
char[] temp = this.str.toCharArray();
for(int i = 0;i<temp.length/2;i++)
{
char temp1;
temp1 = temp[i];
temp[i] = temp[temp.length-i-1];
temp[temp.length-i-1] = temp1;
}
this.str = String.valueOf(temp);
}
@Override
public int length() {
// TODO Auto-generated method stub
char[] temp = this.str.toCharArray();
return temp.length;
}
}
将字符串转换为字节数组
用方法getBytes()
一般的我们获得字符数组不这么写
byte[] data = {'a','d','e'};
而是这么写
byte[] data = "你好UDP".getBytes();