Java basics review

Posted by ShootingBlanks on Fri, 21 Jan 2022 11:46:44 +0100

Java basics review

Java language features

  1. Easy to learn;
  2. Object oriented (encapsulation, inheritance, polymorphism);
  3. Platform independence (Java virtual machine realizes platform independence);
  4. Support multithreading (C + + language has no built-in multithreading mechanism, so it must call the multithreading function of the operating system for multithreading programming, while Java language provides multithreading support);
  5. Reliability;
  6. Safety;
  7. It supports network programming and is very convenient (the birth of Java language itself is designed to simplify network programming, so Java language not only supports network programming but also is very convenient);
  8. Compilation and interpretation coexist;
  9. It has an extremely strong ecological environment in enterprise level development

JVM

A Java virtual machine (JVM) is a virtual machine that runs Java bytecode. The JVM has specific implementations for different systems (Windows, Linux, macOS) to use the same bytecode, and they will all give the same results. (implement cross platform features).


The. Java file can be compiled into a Java C program class bytecode file, which is interpreted by the JVM as machine executable machine code. (JVM is only oriented to bytecode, and can be run as long as it conforms to JVM bytecode specification, whether bytecode is compiled from Java file or python file)

Java data type


Java has certain data types. Unlike other programming languages, it will change with the transformation of hardware architecture, which is one of the reasons why Java has better portability than other languages.

boolean has only two values: true and false. It can be stored in 1 bit, but the specific size is not specified. The JVM will convert boolean data to int at compile time, using 1 to indicate true and 0 to indicate false. The JVM supports boolean arrays, but it is implemented by reading and writing byte arrays.

Each basic type has a corresponding packaging type. The assignment between the basic type and its corresponding packaging type is completed by automatic packing and unpacking. Take Integer as an example

Integer x = 2;     // Boxing called integer valueOf(2)
int y = x;         // Unpacking called X.intValue()

Interview questions

Integer x = new Integer(124);
Integer y = new Integer(124);
System.out.println(x == y);    // false
Integer z = Integer.valueOf(123);
Integer w = Integer.valueOf(123);
System.out.println(z == w);		// true
Integer k = 123;
System.out.println(w == k);		// true

x. Y are two different objects, and they are not equal. It's understandable, so why z == w? This involves the cache pool technology in Java. Taking Integer as an example, Java caches objects within - 128 ~ 127 and calls Integer Valueof () will judge whether the incoming value is in the range. If it is in the range, it will directly return to the object in the cache pool

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
static final int low = -128;
static final int high;
static final Integer cache[];

static {
    // high value may be configured by property
    int h = 127;
    String integerCacheHighPropValue =
        sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    if (integerCacheHighPropValue != null) {
        try {
            int i = parseInt(integerCacheHighPropValue);
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
        } catch( NumberFormatException nfe) {
            // If the property cannot be parsed into an int, ignore it.
        }
    }
    high = h;

    cache = new Integer[(high - low) + 1];
    int j = low;
    for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++);

    // range [-128, 127] must be interned (JLS7 5.1.7)
    assert IntegerCache.high >= 127;
}


Among all the value class buffer pools in jdk 1.8, Integer buffer pool integercache is very special. The lower bound of this buffer pool is - 128 and the upper bound is 127 by default. However, this upper bound is adjustable. When starting the JVM, specify the size of this buffer pool through - XX:AutoBoxCacheMax =. This option will be set to java.net during JVM initialization lang.IntegerCache. High system attribute, and then the system attribute will be read during integercache initialization to determine the upper bound.

Why is there no cache pool for floating point data?
Can you tell how many floating-point data there are in 0 ~ 1?

Java parameter passing

Java parameter transfer is value transfer rather than reference transfer. Corresponding to the basic data type, Java will copy a value and then pass it to the formal parameter. For the reference data type, Java will pass the reference address to the formal parameter in the form of value.
Example:

public static void main(String[] args) {
    int num1 = 10;
    int num2 = 20;

    swap(num1, num2);

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
}

public static void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;

    System.out.println("a = " + a);
    System.out.println("b = " + b);
}
/*
*result:
*	num1 = 10;
*	num2 = 20;
*	a = 20;
*	b = 10;
*/

analysis

class PassByValueExample {
    public static void main(String[] args) {
        Dog dog = new Dog("A");
        func(dog);
        System.out.println(dog.getName());          // B
    }

    private static void func(Dog dog) {
        dog.setName("B");
    }
}
// Result: B

Topics: Java