• Java学习(九)


    Java的函数中存在不定长度参数(variable length arguments),用"..."来表示,实质相当于数组。

    1 public void sum(int...num)
    2     {
    3         int s=0;
    4         for(int temp:num)
    5             s+=temp;
    6         System.out.println(s);
    7     }
    1 int[] num={1,2,3,4};
    2         rs.sum(num);

    对于类中没有相应的构造函数但又需要赋值的情况,则可以使用双大括号来表示。前提是i、j不可为private修饰符,可以为默认、protected或public修饰符。

    Reference temp=new Reference(){{i=1;j=2;}};

    Java中不存在指针,取而代之的是引用。指针比较灵活,但使用不当会造成内存溢出等意外发生。

    在Java中,不能够利用函数交换两个数的值,但可以利用返回一个对象的两个值来达到这种效果。

    1 public Object[] swap(Object a,Object b)
    2     {
    3         Object[] value=new Object[2];
    4         value[0]=b;
    5         value[1]=a;
    6         return value;
    7     }
      Object a=2,b=3;
            Object[] value=rs.swap(a, b);
            System.out.println(value[0]+" "+value[1]);
        }

    关于构建链表,代码如下:

     1 public class Reference {
     2     int i;
     3     public Reference(int n) {
     4         // TODO Auto-generated constructor stub
     5         i=n;
     6     }
     7     public Reference(){}
     8     public void print()
     9     {
    10         System.out.println(i);
    11     }
    12     Reference next;
    13 }
     1     Reference ref1=new Reference(0);
     2         Reference ref2=null;
     3         for(int i=0;i<10;i++)
     4         {    
     5             ref2=ref1;        
     6             ref1.next=new Reference(i);    
     7             ref1=ref1.next;
     8             ref2.print();
     9         }
    10 结果为:
    11 0
    12 0
    13 1
    14 2
    15 3
    16 4
    17 5
    18 6
    19 7
    20 8

    判断相等:1.值类型是值相等;2.引用类型是引用相等。但也存在一些比较特殊的情况。如下所示:

     1     public void judge()
     2     {
     3         /*
     4          * 形式一,true
     5          */
     6         Object obj1=new Object();
     7         Object obj2=new Object();
     8         obj1=1;
     9         obj2=1;
    10         if(obj1==obj2)
    11             System.out.println("Obj1 == obj2: true");
    12         else
    13             System.out.println("Obj1 == obj2: false");
    14         
    15         /*
    16          * 形式二,因为为两个对象,false
    17          */
    18         Integer i1=new Integer(1);
    19         Integer i2=new Integer(1);
    20         if(i1==i2)
    21             System.out.println("i1 == i2: true");
    22         else
    23             System.out.println("i1 == i2: false");
    24         
    25         /*
    26          * 形式三,内部有一个字节的缓存,为-128~127,true
    27          */
    28         Integer i3=1;
    29         Integer i4=1;
    30         if(i3==i4)
    31             System.out.println("i3 == i4: true");
    32         else
    33             System.out.println("i3 == i4: false");
    34         
    35         /*
    36          * 形式四,超过内部缓存值,false
    37          */
    38         Integer i5=130;
    39         Integer i6=130;
    40         if(i5==i6)
    41             System.out.println("i5 == i6: true");
    42         else
    43             System.out.println("i5 == i6: false");
    44         
    45         /*
    46          * 形式五
    47          */
    48         String hello="Hello";
    49         String lo="lo";
    50         final String lof="lo";
    51         //true
    52         if(hello=="Hello")
    53             System.out.println("hello = 'Hello': true");
    54         else
    55             System.out.println("hello = 'Hello': false");
    56         
    57         //false
    58         if(("Hel"+lo)=="Hello")
    59             System.out.println("'Hel'+lo = 'Hello': true");
    60         else
    61             System.out.println("'Hel'+lo = 'Hello': false");
    62         
    63         //true
    64         if(("Hel"+lof)=="Hello")
    65             System.out.println("'Hel'+lof = 'Hello': true");
    66         else
    67             System.out.println("'Hel'+lof = 'Hello': false");
    68         
    69         //false
    70         if(hello==new String("Hello"))
    71             System.out.println("hello==new String('Hello'): true");
    72         else
    73             System.out.println("hello==new String('Hello'): false");
    74         
    75         //true
    76         if(hello==new String("Hello").intern())
    77             System.out.println("hello==new String('Hello').intern(): true");
    78         else
    79             System.out.println("hello==new String('Hello').intern(): false");    
    80     }

    如果要判断引用类型值是否相等,则需要重写hashCode的equals()方法.

    对于String类型数据,常量之间可以用==来表示是否相等,非常量可以用.intern()函数转化成常量进行比较。常量与非常量进行比较时会返回false。还有一种方法就是利用String的equals进行比较。

  • 相关阅读:
    vmware esxi 过期,激活
    mysql 导入csv文件
    定时登录下载sftp服务器上的某些有规则的文件
    修改tmp的临时目录的地址
    jetty权威指南
    将linux下的rm命令改造成mv到指定的目录下
    mysql 5.6 grant授权的时候出现问题
    Keras vs. PyTorch
    Visualizing LSTM Layer with t-sne in Neural Networks
    keras Lambda 层
  • 原文地址:https://www.cnblogs.com/2Bthebest1/p/8421401.html
Copyright © 2020-2023  润新知