Java Some Tips 一些小细节

更多 Java 基础知识方面的文章,请参见文集《Java 基础知识》


关于局部变量的默认值

局部变量没有默认值,无论是基本数据类型,还是引用类型。
例如,以下代码编译不通过:

public static void main(String[] args) throws Exception {
    // 编译不通过
    int i;
    System.out.println(i);
}

关于构造方法

  • 没有 return type
  • 有 return value
  • 不会被继承
  • 不能用 final 修饰

关于 main 方法

为什么 main 方法是 static:无需创建对象,无需额外分配内存
以下两种方式等价:
public static void main(String[] args)
static public void main(String[] args)

没有 main 方法可以执行程序么?YES,将代码放在 static 块中。(仅限 Java 7 之前)

Java 中只有值传递

例如如下代码:
函数 f1() 传入引用变量 s1,产生一个副本 temp,指向同一个对象。通过 temp 改变对象的内容 name,会影响到变量 s1
函数 f2() 传入引用变量 s2,产生一个副本 temp,指向同一个对象。通过 s = new Student("321"); 改变 temp 的指向,不会影响到变量 s2

public class PassByValue_Test {
    public static void f1(Student s) {
        s.name = "321";
    }

    public static void f2(Student s) {
        s = new Student("321");
    }

    public static void main(String[] args) {
        Student s1 = new Student("123");
        f1(s1);
        // 输出 321
        System.out.println(s1.name);

        Student s2 = new Student("123");
        f2(s2);
        // 输出 123
        System.out.println(s2.name);
    }
}

class Student {
    public String name;

    public Student(String name) {
        this.name = name;
    }
}

Java Math

  • Math.cell(1.2):不小于 1.2 的最小整数,结果为 2
  • Math.floor(1.2):不大于 1.2 的最大整数,结果为 1

Java Array 类

提供一系列静态方法来创建和操作数组。
示例:

public static void main(String[] args) {
    Object arr = Array.newInstance(String.class, 10);
    Array.set(arr, 0, "Tom");

    System.out.println(Array.get(arr, 0));
}

数据存储:大端 Big Endian VS 小端 Small Endian

例如 char c = 'a'; 字符 a 的 ASCII 码为97。

  • 大端 Big Endian:低地址位存放高有效字节。存储为:00097
  • 小端 Small Endian:低地址位存放低有效字节。存储为:97000
    Java 使用 Big Endian

示例如下:

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Endian_Test {
    public static void main(String[] args) {
        char c = 'a';

        ByteBuffer buffer1 = ByteBuffer.allocate(4);
        buffer1.order(ByteOrder.BIG_ENDIAN);
        buffer1.putInt(c);

        byte[] arr1 = buffer1.array();
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]); // 输出 00097
        }

        ByteBuffer buffer2 = ByteBuffer.allocate(4);
        buffer2.order(ByteOrder.LITTLE_ENDIAN);
        buffer2.putInt(c);

        byte[] arr2 = buffer2.array();
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i]); // 输出 97000
        }
    }
}

数字之间可以使用下划线

参见 Underscores in Numeric Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code. 提高代码可读性
For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

例如:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =      3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

Java 允许长度为 0 的数组

这样可以避免了对空指针的检测,例如:

public class ZeroLengthArrayDemo {
    public static void main(String[] args) {
        int[] is = new int[0];

        System.out.println(is.length); // 0
        
        for(int i : is) {
            System.out.println(i);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,874评论 18 399
  • 多线程、特别是NSOperation 和 GCD 的内部原理。运行时机制的原理和运用场景。SDWebImage的原...
    LZM轮回阅读 2,046评论 0 12
  • ———————————————回答好下面的足够了---------------------------------...
    恒爱DE问候阅读 1,777评论 0 4
  • iOS面试小贴士 ———————————————回答好下面的足够了------------------------...
    不言不爱阅读 2,055评论 0 7
  • 史上最全的iOS面试题及答案 iOS面试小贴士———————————————回答好下面的足够了----------...
    Style_伟阅读 2,480评论 0 35