[JAVA学习笔记]常用类

 String类:

一、存放位置:

   字符串对象创建好后不能修改           

   String是引用数据类型,但是这里作为方法参数传递的时候,效果跟基本数据类型是一样的。也就是说在堆中创建出来的字符串”monkey”是不能被改变的,如果要修改,系统会在堆中重新开辟内存空间将”good”放入,然后将change方法中的s重新引用这个新的内存地址上的”good”。

第一个存在常量池里,第二个存在堆内存里

String s3=new String ("monkey") 先在堆内存存,再在常量池存【因为有双引号】

实际开发不用New 用下方方法

创建了3个对象

   二、字符串比较: 

字符串的比较:

==指地址相等

equals指内容相等


三、String方法:

char charAt(int index);获取index位置的字符

boolean contains(CharSequence s);判断字符串中是否包含某个字符串

boolean endsWith(String endStr);判断是否是以某个字符串结尾

boolean equalsIgnoreCase(String anotherString);忽略大小写比较两个字符串是否相等

byte[] getBytes();转换成byte数组

int indexOf(String str);取得指定字符在字符串的位置

int indexOf(String str, int fromIndex);从指定的下标开始取得指定字符在字符串的位置

int lastIndexOf(String str);从后面开始取得指定字符在字符串最后出现的的位置

int length();获取字符串的长度

String replaceAll(String s1,String s2);替换字符串中的内容

String[] split(String s);根据指定的表达式拆分字符串

boolean startsWith(String s);判断是否是以某个字符串开始

String substring(int begin);根据传入的索引位置截子串

String substring(int beginIndex, int endIndex);根据传入的起始和结束位置截子串

char[] toCharArray();将字符串转换为char数组

void toUpperCase();转换为大写

void toLowerCase();转换为小写

String trim();去除首尾空格

String valueOf(Object obj);将其他类型转换为字符串类型


四、StringBuffer和StringBuilder:字符串缓冲区

当频繁使用字符串拼接时,用StringBuffer,需要new

StringBuffer sb1=new StringBuffer(30)

StringBuffer进行字符串拼接 使用append

插入字符串用insert(index,“”)

删除指定位置用delete(index1,index2)

基本类型和包装类

基本数据类型 包装类

byte         Byte

short         Short

int         Integer

long         Long

float         Float

double         Double

char         Character

boolean         Boolean


自动装箱与自动拆箱

自动装箱

Integer i3 = 1024

自动拆箱

Integer i4 = 1024;

int i5 = i4


Integer、String、int的类型转换

Integer数据类型的引用

注意:Integer是引用数据类型,在判断两个Integer类型是否相等时,要使用equals方法,不能使用”==”,Integer已经重写了Object中的equals方法。


整型缓存池

如果Integer的值是在[-128~127]之间,这些值会从Integer中的“整型缓存池”获取。
观察下面程序打印的结果

Integer i5 = 127; 
Integer i6 = 127;

System.out.println(i5 == i6); //true

Date类型和Calender类型

Date类型

SimperDateFormat改变日期格式

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");

String strTime = sdf.format(nowTime);
System.out.println(strTime);

String类型变成Date类型

        String s = "2009-01-02";
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd");
        Date dd=new Date();
        try {
            dd=sdf1.parse(s);
        }catch (ParseException e){
            e.printStackTrace();
        }

        System.out.println(dd);

Calender类型

        Calendar c = Calendar.getInstance();

        //查看当前日期是星期几
        int i = c.get(Calendar.DAY_OF_WEEK);
        System.out.println(i); //外国人将周日看做是第一天
         System.out.println(c.get(Calendar.DAY_OF_MONTH));

Math类

public class MathTest {

    public static void main(String[] args) {
        //圆周率
        System.out.println(Math.PI);
        //取绝对值
        System.out.println(Math.abs(-10));              

        //ceil天花板,会向上取值,结果是double
        System.out.println(Math.ceil(12.3));
        System.out.println(Math.ceil(12.99));

        System.out.println("-----------");
        //floor地板,会向下取整,结果是double
        System.out.println(Math.floor(12.3));            
        System.out.println(Math.floor(12.99));

        //获取两个值中的最大值
        System.out.println(Math.max(20, 30));

        //前面的数是底数,后面的数是指数,即2的3次方
        System.out.println(Math.pow(2, 3));

        //生成0.0到1.0之间的随机小数,包括0.0,不包括1.0
        System.out.println(Math.random());

        //四舍五入
        System.out.println(Math.round(12.3f));
        System.out.println(Math.round(12.9f));

        //开平方
        System.out.println(Math.sqrt(16));
    }

}

BigInteger和BigDecimal类

BigDecimal类

 //开发时推荐通过传入字符串的方式
        BigDecimal bd3 = new BigDecimal("2.0");		
        BigDecimal bd4 = new BigDecimal("1.1");
        System.out.println(bd3.subtract(bd4));
        
        //这种方式在开发中也是推荐的
        BigDecimal bd5 = BigDecimal.valueOf(2.0);	
        BigDecimal bd6 = BigDecimal.valueOf(1.1);
        System.out.println(bd5.subtract(bd6));

BigInteger

        BigInteger bi1 = new BigInteger("100");
        BigInteger bi2 = new BigInteger("2");

        System.out.println(bi1.add(bi2));                 //+
        System.out.println(bi1.subtract(bi2));            //-
        System.out.println(bi1.multiply(bi2));             //*
        System.out.println(bi1.divide(bi2));            ///(除)

        BigInteger[] arr = bi1.divideAndRemainder(bi2);    //取除数和余数

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值