String 和 StringBuffer 数组对象 MATH类 日期类 日期格式化:包装类

String 和 StringBuffer

相同点:两者都是用来存储字符串的。

String类型的赋值理解:

   int a = 3;

     a = 5;

这段代码的含义是:首先申请一个内存单元,命名为a,同时将3这个值存储到内存单元中,然后又将5这个值存入内存单元中,这个内存单元的之前的值在被赋值后就被修改了。

   String str = new String(“abc”);

Str= “123”;

对于引用而言,赋值并不是改变与原来引用对象的值,而只是调整了引用的关系,所以在使用“==”符号进行比较时,需要特别注意。

String string = new String("abc");
String string1 = new String("abc");
System.out.println(string==string1);//==比较的是两个对象的地址
System.out.println(string.equals(string1));//equals();比较的是两个对象的值

因string 和 string1引用的是不同的对象,所以string == string1为假

因string 和 string1引用的字符串内容是相同的所以string.equals(string1)为真。

 

String类常用的方法:

  equals,length,charAt(),format()(把字符串格式化),subString(切换字符串),indexOf(返回指定字符在此字符串中第一次出现处的索引), lastIndexOf, replace,sunString,trim, toUpperCase(),toLowerCase()

package String的常用方法;

public class CharAt {
    public static void main(String[] args) {
        String string = "abcdef";

//查找指定下标的字符
        System.out.println(string.charAt(2));
    }
}

 

package String的常用方法;

public class Format {
    public static void main(String[] args) {
        int a = 3;
        int b = 6;
        System.out.printf("%d+%d=%d\n",a,b,a+b);//格式化输出

       
String string = a + "+" + b + "=" + (a+b);
        System.out.println(string);

        String string1 = String.format("%d+%d=%d\n",a,b,a+b);//格式化字符串
       
System.out.println(string1);
    }
}

 

 

package 第1章_常用类.String的常用方法;

public class IndexOf {
    public static void main(String[] args) {
        String string = "HelloWorld";
        //返回指定字符在此字符串中第一次出现处的索引,把字符转换成ASCII码,所以参数类型为int
       
System.out.println(string.indexOf('o'));
        //从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。
       
System.out.println(string.indexOf('o',5));
        //返回第一次出现的指定子字符串在此字符串中的索引。
       
System.out.println(string.indexOf("World"));
        //从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
       
System.out.println(string.indexOf("d",5));
         //返回一个新的字符串,它是此字符串的一个子字符串。
       
System.out.println(string.substring(3));
        //返回一个新字符串,它是此字符串的一个子字符串。
       
System.out.println(string.substring(3,8));
        //返回字符串的副本,忽略前导空白和尾部空白。
       
System.out.println(string.trim());
    }
}

 

 

StringBuffer类常用的方法:

  Append,insert,deleteCharAt,delete,replace,setCharAt,reverse

 

public class Append {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("Append");
        //将布尔参数的字符串表示形式附加到序列中。
       
System.out.println(stringBuffer.append("2"));//Append2
   
}
}

 

public class Insert {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("Insert");
        //将布尔参数的字符串表示形式插入到该序列中。
       
System.out.println(stringBuffer.insert(3,4));//offset:偏移量
   
}
}

 

public class DeleteCharAt {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("DeleteCharAt");
        //删除指定字节
       
System.out.println(stringBuffer.deleteCharAt(3));
    }
}

 

public class Delete {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("Delete");
        //移除此序列的子字符串中的字符。
       
System.out.println(stringBuffer.delete(1,3));//从1-3删除,前包含后不包含
   
}
}

 

public class Replace {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("Replace");
        //用指定字符串中的字符替换该序列的子字符串中的字符。
       
System.out.println(stringBuffer.replace(1,3,"ggg"));
    }
}

 

public class SetCharAt {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("SetCharAt");
        //指定索引处的字符设置为CH
       
stringBuffer.setCharAt(1,'E');//setCharAt没有返回值,不能直接输出
       
System.out.println(stringBuffer);
    }
}

 

public class Reverse {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("Reverse");
        //使这个字符序列被序列的反向替换。
       
System.out.println(stringBuffer.reverse());
    }
}

 

1.2数组对象

    Arrays提供了数组的常见操作,比如查找,排序等。且他们的方法都是静态的,所以实际上他是一个工具类,提供方法,方便编程。

  fill,sort(升序排序)

 

   package Arrays的常用方法;

import java.util.Arrays;

public class fill {
    public static void main(String[] args) {
        int [] a = {0,1,2,3,4,5,6,7,8,9};
        //将指定的 int 值分配给指定 int 型数组的每个元素。
       
Arrays.fill(a,8);
        System.out.println(Arrays.toString(a));
    }
}

 

package Arrays的常用方法;

import java.util.Arrays;

public class Sort {
    public static void main(String[] args) {
        int [] a = {2,3,1,4,5,8,7,6,0,9};
        //对指定的 int 型数组按数字升序进行排序。
       
System.out.println("排序前:"+ Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("排序后:"+Arrays.toString(a));
    }
}

 

package Arrays的常用方法;

import java.util.Arrays;

public class BinarySearch {
    public static void main(String[] args) {
        int [] a = {3,7,5,7,8,3,2};
        Arrays.sort(a);
        //使用二进制搜索算法来搜索指定的 int 型数组,以获得指定的值。调用此方法要先排序
       
System.out.println(Arrays.binarySearch(a,5));
    }
}

 

1.3MATH类

 

   静态常量: e 和PI

max,min,floor(向下取整),ceil(向上取整),cos,sin,radom,round(四舍五入),pow……..

 

package Math;

public class 骰子 {
    public static void main(String[] args) {
        for (int i=0;i<6;i++){//摇六次
           
int num=(int)Math.ceil(Math.random()*6);
            System.out.println(num);
        }
    }
}

 

日期类

Canlendar类
package Date;

import java.util.Calendar;

public class calendar {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();//获取当前时间的所有信息
        System.out.println(calendar);

        System.out.println(calendar.get(Calendar.YEAR));//获取年份
        System.out.println(calendar.get(Calendar.MONTH)+1);//月,从0开始,月份加1
        System.out.println(calendar.get(Calendar.DATE));//日
        System.out.println(calendar.get(Calendar.HOUR));//时
        System.out.println(calendar.get(Calendar.MINUTE));//分
        System.out.println(calendar.get(Calendar.SECOND));//秒

        calendar.setLenient(false);//关闭默认模式,如果设置非法,会报错
        calendar.set(Calendar.YEAR,2222);//设置年份
        System.out.println(calendar.get(Calendar.YEAR));
        calendar.set(Calendar.MONDAY,12);//设置月份
        System.out.println(calendar.get(Calendar.MONTH));

    }
}

Date类

package Date;

import java.util.Date;

public class date {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date.toString());

        long dateTime = date.getTime();
        System.out.println(dateTime);
    }
}

 

日期格式化:

package 日期格式化;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        //参数为年月日的格式,HH,24进制,hh:12进制
       
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        System.out.println(date);
        System.out.println(simpleDateFormat.format(date));

    }
}

 

package 日期格式化;

import java.text.DateFormat;
import java.util.Date;

public class Test1 {
    public static void main(String[] args) {
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);//格式对象,参数为显示方式类型之一
       
Date date = new Date();//当前时间

       
System.out.println(dateFormat.format(date));

    }
}

 

包装类

为什么要用包装类?

   因为基础类型和其他类型处理是有不同的

   基础类型的赋值,是直接修改变量里面的值,准确的说,是修改了内存单元里面的内容。

   其他类型的赋值,是改变了引用的指向。

很多地方统一(尤其是泛型),系统就提供了我们基本类型所对应的包装类。

int——> Integer

char——>Character,其余的类型的包装类只需要把首字母大写


  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值