java基础

这里写目录标题

Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合

https://blog.csdn.net/mu_wind/article/details/109516995

菜鸟教程

1.StringBuffer

在这里插入图片描述

class Test
{
    public static void main(String[] args)
    {
        StringBuffer a = new StringBuffer("Runoob");
        StringBuffer b = new StringBuffer("Google");
        a.delete(1,3);
        a.append(b);
        System.out.println(a);
    }
}

2.Main 类中 main() 是一个静态函数, fun() 是一个非静态函数, Java 静态函数中不能调用非静态函数的方法。

在这里插入图片描述

class Main {
    public static void main(String args[]) { 
        System.out.println(fun());
    } 

    int fun()
    {
        return 20;
    }
}

3.Java 中参数通过值传递,所以 x 传到函数中不会影响原来的值。

在这里插入图片描述

public class Main { 
    public static void main(String args[]) { 
       String x = null; 
       giveMeAString(x); 
       System.out.println(x); 
    } 
    static void giveMeAString(String y) 
    { 
       y = "RUNOOB"; 
    } 
}

4.Java 函数参数通过值传递。

在这里插入图片描述

class Main {
public static void swap(Integer i, Integer j) {
      Integer temp = new Integer(i);
      i = j;
      j = temp;
   }
   public static void main(String[] args) {
      Integer i = new Integer(10);
      Integer j = new Integer(20);
      swap(i, j);
      System.out.println("i = " + i + ", j = " + j);
   }
}

5.在 Java 应用程序中永远不会传递对象,而只传递对象引用。因此是按引用传递对象。

在这里插入图片描述

class intWrap {
   int x;
} 
public class Main { 
    public static void main(String[] args) {
       intWrap i = new intWrap();
       i.x = 10;
       intWrap j = new intWrap();
       j.x = 20;
       swap(i, j);
       System.out.println("i.x = " + i.x + ", j.x = " + j.x);
    } 
    public static void swap(intWrap i, intWrap j) {
       int temp = i.x;
       i.x = j.x;
       j.x = temp;
    }
}

6.Java 函数不允许参数设置默认值。

在这里插入图片描述

class Main {
    public static void main(String args[]) {   
        System.out.println(fun());
    }   
    static int fun(int x = 0)
    {
        return x;
    }
}

7.String 类有一个内置的构造函数 String(character_array),它可以将字符数组初始化成一个字符串。split() 根据指定的规则或分隔符来分隔字符串,并返回数组。

在这里插入图片描述

class Test
{
    public void demo(String str)
    {
        String[] arr = str.split(";");
        for (String s : arr)
        {
            System.out.println(s);
        }
    }
 
    public static void main(String[] args)
    {
        char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ', 
                        'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};
        String str = new String(array);
        Test obj = new Test();
        obj.demo(str);
    }
}

8.delete(x, y) 函数删除字符串的 ‘x’(包含) 到 ‘y-1’(包含) 的位置元素。append() 函数用于连接字符串

在这里插入图片描述

class Main
{
    public static void main(String[] args)
    {
        StringBuffer a = new StringBuffer("runnob");
        StringBuffer b = new StringBuffer("com");
        a.delete(1,3);
        a.append(b);
        System.out.println(a);
    }
}

9.obj.hashCode() 函数返回对象的 32 位哈希值。 obj1.equals(obj2) 用于判断两个对象的值是否相等。 obj1 == obj2 在两个对象引用同一个对象时才会相等。

在这里插入图片描述

class Main
{
    public static void main(String[] args)
    {
        String obj1 = new String("runoob");
        String obj2 = new String("runoob");
 
        if(obj1.hashCode() == obj2.hashCode())
            System.out.println("object1 与 object2 哈希码相等");
 
        if(obj1 == obj2)
            System.out.println("object1 与 object2 内存地址一样");
 
        if(obj1.equals(obj2))
            System.out.println("object1 与 object2 值相等");
    }
}

10.clone( ) 方法调用时会生成多个对象的拷贝。 类只有在实现 Cloneable 接口才可以实现克隆。

在这里插入图片描述

class Test implements Cloneable
{
    int a;
 
    Test cloning()
    {
        try
        {
            return (Test) super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println("CloneNotSupportedException is caught");
            return this;
        }
    }
}
 
class Main
{
 
    public static void main(String args[])
    {
        Test obj1 = new Test();
        Test obj2;
        obj1.a = 10;
        obj2 = obj1.cloning();
        obj2.a = 20;
 
        System.out.println("obj1.a = " + obj1.a);
        System.out.println("obj2.a = " + obj2.a);
    }
}

11.str.toUpperCase() 将字符串小写字母转换为大写字母,但是它不会改变原始的字符串。 str.substring(x, y) 返回 ‘x’(包含) 到 ‘y’(不包含) 位置的字符串。 str.charAt(x) 返回 x 位置的字符。

在这里插入图片描述

class Main
{
    public static void main(String[] args)
    {
        String str = "runoob";
        str.toUpperCase();
        str += "wwwrunoobcom";
        String string = str.substring(2,13);
        string = string + str.charAt(4);;
        System.out.println(string);
    }

java开发手册

1.浮点数之间的等值判断

//方法1指定一个误差范围,两个浮点数的差值在此范围之内,则认为是相等的。
public class Test {

    public static void main(String[] args){
        float a = 1.0F - 0.9F;
        float b = 0.9F - 0.8F;
        float diff = 1e-6F;
        if (Math.abs(a - b) < diff) {
            System.out.println("true");
        }
    }
}
//方法2使用 BigDecimal 来定义值,再进行浮点数的运算操作。
public class Test {

    public static void main(String[] args){
        BigDecimal a = new BigDecimal("1.0");
        BigDecimal b = new BigDecimal("0.9");
        BigDecimal c = new BigDecimal("0.8");
        BigDecimal x = a.subtract(b);
        BigDecimal y = b.subtract(c);
        if (x.compareTo(y) == 0) {
            System.out.println("true");
        }
    }
}

2.使用索引访问用 String 的 split 方法得到的数组时,需做最后一个分隔符后有无内容的检查,否则会有抛 IndexOutOfBoundsException 的风险

public class Test {


    public static void main(String[] args){

        String str = "a,b,c,,";
        String[] ary = str.split(",");
// 预期大于 3,结果等于 3
        System.out.println(ary.length);

    }

}

3.add值数组和输出数组

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

        List<Number> list = new ArrayList<>();

        list.add(new BigDecimal("123"));
        list.add(new BigDecimal("2"));

        for (Number i :list ) {
            System.out.println("list = " + list);

        }
    }
}
//输出的值
//list = [123, 2]
//list = [123, 2]


单例模式

饿汉式

public class SingletonTest {

    public static void main(String[] args){

        happy h1 = happy.getInstance();
        happy h2 = happy.getInstance();
        System.out.println(h1 == h2);//true
    }
}
//this is 饿汉式~
class happy{
    //1.私有化类的构造器
    private happy(){

    }

    //2.内部创建类的对象
    //3.要求此对象也必须声明为静态的
    private static happy instance = new happy();

    //4.提供公共的静态的方法,返回类的对象
    public static happy getInstance(){
        return instance;
    }

}

在这里插入图片描述

懒汉式

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

            nice h1 = nice.getInstance();
            nice h2 = nice.getInstance();
            System.out.println(h1 == h2);//true
        }
    }
    //this is 懒汉式~
    class nice{
        //1.私有化类的构造器
        private nice(){

        }

        //2.声明当前类对象,没有初始化
        //3.要求此对象也必须声明为static的
        private static nice instance = null;

        //4.声明public,static的放回当前类对象的方法
        public static nice getInstance(){
            if(instance == null){
                instance = new nice();
            }
            return instance;
        }


}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值