栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java中的常用类-1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java中的常用类-1

Java API

API(应用程序编程接口)

java语言中提供了很多的类和接口

但是一般所说的api是指api文档,是对java预定定义的类类或接口功能和函数功能的说明文档,目的是提供给开发人员进行使用帮助说明。

基本数据类型包装类

为什么要有基本数据类型包装类?

基本数据类型是通过八个关键字声明,结构简单。但是基本数据类型却是不面向对象的,这在实际使用中存在许多的不便。为解决这个问题,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这些基本数据类型对应的类统称为基本数据包装类。

包装类:这些类封装了一个对应的基本数据类型数值,并为其提供了一系列操作方法。

基本数据类型 对应的基本数据类型包装类

byte Byte

short Short

char Character

int Integer

long Long

float Float

double Double

boolean Boolean

包装类的用途主要有两种:

1.作为和基本数据类型相对应的类类型存在

2.包含每种基本数据类型的相关属性如最大值,最小值等,以及相关的操作方法。

基本数据类型分装类中的常用方法

java.lang.Interger

public class use1 {
    public static void main(String[] args) {
​
      Integer a =new Integer(10);
      Integer b =new Integer("100");
​
        System.out.println(Integer.MAX_VALUE);//打印int类型数值的最大值
        System.out.println(Integer.MIN_VALUE);//打印int类型数值的最小值
​
        System.out.println(Integer.SIZE);   //打印
        System.out.println(Integer.BYTES);  //打印
​
        System.out.println(Integer.max(50,20));//比较两个int类型数值的大小,并打印两者中较大的数的数值
        System.out.println(Integer.min(50,20));//比较两个int类型数值的大小,并打印两者中较小的数的数值
​
        System.out.println(Integer.toHexString(1000));//将传入的int类型的数值以16进制打印
        System.out.println(Integer.toOctalString(100));//将传入的int类型的数值以8进制打印
        System.out.println(Integer.toBinaryString(10));//将传入的int类型的数值以2进制打印
        System.out.println(a.compareTo(b));//比较两个对象中包含的基本数据类型值的大小,返回 -1,0,1。
        
        
​
    }
}

装箱和拆箱

装箱:

自动将基本数据类型转换为包装器类型

装箱的时候自动调用的是Integer的valueOf(int)方法

拆箱:

自动将包装器类型转换为基本数据类型

拆箱的时候自动调用的是Integer的intValue方法

public class use1 {
    public static void main(String[] args) {
​
      Integer a =new Integer(10);
      Integer b =new Integer("100");//将String类型包装为int
      Integer d =Integer.valueOf(40);
      int aa =a.intValue();
​
      int c =Integer.parseInt("20");
        
        Integer x =new Integer(10);
        int y =x;//自动拆箱,把包装类转换为类型x.intValue()
        System.out.println(x);
        
        Integer z =x;//自动装箱,把基本类型转为包装类型Integer.valueOf(y)
        
        
        int  a1 =10;
        Integer b1 =Integer.valueOf(a);//装箱
        
        int c =b1.intValue();//拆箱
    }
}
​

==与equal

public class use2 {
    public static void main(String[] args) {
    //  ==  比较基本类型时,比较 值 是否相等
        int x =128;
        int y =128;
        System.out.println(x==y);//true
​
​
        
       
        Integer a =128;//Integer.valueOf(127), 大于107,小于-128  就回new新的对象
        Integer b =128;
        System.out.println(a==b);//true
​
​
        Integer i =new Integer(10);
        Integer ii =new Integer(10);
        System.out.println(i==ii);//false
​
        System.out.println(a.equals(b));//比较的是对象中的值是否相等
​
​
​
    }
}

Object类

Object类 是所有类的父类,是多态实现的基础,

其中的方法 toString();将xx转为字符串形式 Arrays.toString(a)

public class use3 extends Object {
    public static void main(String[] args) {
​
        Car c = new Car("宝马", 300000);
       
        System.out.println(c);//com.ffyc.javaapi.obj.Car@1b6d3586
​
​
    }
}
​

所以,需要使用Object类中的方法时,如果忘记了重写方法,那会调用原来类中的方法。

@Override
    public boolean equals(Object obj) {
        if(obj instanceof Car){
            Car otherCar = (Car)obj;
            return this.name.equals(otherCar.name) && this.price==otherCar.price;
        }
        return false;
    }

        Car c = new Car("宝马", 30000);
        Car c1 = new Car("宝马", 30000);
        
        System.out.println(c.equals(c1));//true
//如果为重写之前就使用    false  原因是Car类中没有重写equals(),调用Object类中的方法
​

Arrays类

java.util.Arrays类

用来操作数组的类,里面定义了常见操作数组的方法。

public class use3 {
    public static void main(String[] args) {
        int []a =new int[]{1,2,3,4,5};
​
        int []b =new int[]{1,2,3,4,5};
        System.out.println(Arrays.equals(a,b));//equals(a,b),传入两个数组,比较两个数组内容是否相等
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));//toString(a),将传入的数组的内容以字符串的形式输出
​
        int []c =new int[]{3,2,4,1,5};
        Arrays.sort(c,2,4);//开始位置(包含),结尾位置(不包含)
        Arrays.sort(c);         //引用传递
        System.out.println(Arrays.toString(c));
​
        user1 s1 =new user1("alice",1);
        user1 s2 =new user1("bob",2);
        user1 s3 =new user1("charli",3);
        user1 s4 =new user1("dog",4);
        user1 s5 =new user1("easy",5);
​
        System.out.println(s1.toString());
        System.out.println(s2.toString());//需要在子类中重写父类Object中的toString方法
​
        user1 []a1 =new user1[5];
        a1[0]=s3;
        a1[1]=s4;
        a1[2]=s1;
        a1[3]=s2;
        a1[4]=s5;//想要输出user1类型的数组的内容,如果不重写toString方法的话,不能达到效果
        Arrays.sort(a1);//对于引用类,需要特殊处理。Error!,.ClassCastException,数据类型转换错误,需要重写方法
            
​
        System.out.println(Arrays.toString(a1));
    }
}

public class user1 implements Comparable{
    String name;
    int no;
​
    public user1(String name, int no) {
        this.name = name;
        this.no = no;
    }
    
    @Override
    public String toString() {
        return "user1{" +
                "name='" + name + ''' +
                ", no=" + no +
                '}';
    }
    @Override
    public int compareTo(user1 o) {
        return  this.no-o.no;
    }
}
​

二分查找

public class use4 {
    public static void main(String[] args) {

        int []a =new int[]{5,3,4,2,1,7,6};
        Arrays.sort(a);
        int index = Arrays.binarySearch(a,1);
        System.out.println(index);//index如果不是负数,说明找到了。
        
        //如果key在数组中,则返回搜索值的索引;否则返回-1或者”-“(插入点)
    }
}

数组扩容

public class use5 {
    public static void main(String[] args) {
        int []a =new int[]{1,2,3,4,5};
        int []b = Arrays.copyOf(a,7);//数组扩容
        
        
    }
}

String类

public class use6 {
    public static void main(String[] args) {

        
        String s ="abc";
        s += "de";              //第一个字符串对象
        System.out.println(s);  //第二个字符串对象
        
        String s1 ="acd";
        String a ="acd";
        System.out.println(a==s1);//true
        System.out.println(s1.equals(a));//true
        


        String s2 =new String();
        String b =new String("abcd");
        String s3 =new String("abcd");
        System.out.println(b==s3);//false

    }
}

		String s =new String();//""
        String s1 =new String("abcd");//

public class use7 {
    public static void main(String[] args) {

        String s =new String();
        String s1 =new String("abcd");
        String s2 =new String("abcd");

        System.out.println(s1.equals(s2));//比较两个字符串内容的值是否相等
        String s3 =new String("Abcd");

        System.out.println(s1.equalsIgnoreCase(s3));
        //比较两个字符串内容,并且对于大小写字母的区别进行忽略

        System.out.println(s3.contains("Ab"));//是否包含指定的子串
        
        System.out.println(s.isEmpty());//判断字符串是否为空
        System.out.println(s1.startsWith("ab"));//判断字符串是否以ab开头
        System.out.println(s1.endsWith("cd"));//判断字符串是否以cd结尾

    }
}

public class use8 {
    public static void main(String[] args) {

        String s ="abcdea";

        System.out.println(s.length());//返回字符串长度
        System.out.println(s.charAt(1));//返回指定位置上的字符串
        System.out.println(s.indexOf("c"));//获取指定字符串首次出现的位置 从后向前
        System.out.println(s.indexOf("c",3));
        //获取指定字符串首次出现的位置    从指定位置开始找
        System.out.println(s.lastIndexOf("c"));
        //从指定的位置开始截取一个新的子串到结尾
        System.out.println(s.substring(2));
        //从指定的位置开始截取一个到指定位置结尾的子串
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号