Day0 know Java and variable types

Virtual machine bytecode

Java between compiled languages and interpreted languages . Compiled languages such as C, C ++, the code is compiled into machine code directly executed, but the different platforms (x86, ARM, etc.) CPU instruction set different, and therefore, needs to be compiled machine code corresponding to each of the platforms. Interpreted languages such as Python, Ruby does not have this problem, can be loaded directly from the source code interpreter and run, the cost of operating efficiency is too low. The Java code is compiled into a "byte code" , which is similar to abstract CPU instructions, then, written for different platforms, virtual machines , virtual machines on different platforms is responsible for loading bytecode and executed, thus achieving a "once preparation, running everywhere "effect.

Java bin folder executable file

java: This executable is actually a JVM , a Java program to run, is to start the JVM, then let the JVM perform the specified code compiler;
javac: This is a Java compiler , which is used to put Java source file (with the suffix .java end) compiled to Java byte code file (ending in .class suffix) ;
JAR: used to encapsulate a set of .class files into a .jar file for easy release;
Javadoc: Notes for automatically extracting and generating source code from Java documentation;
JDB: the Java debugger for debugging the development phase of the operation.

The first program

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Hello class name, case sensitive, as main method name, predetermined Java, a class definition public static void main (String [] args) is a method of fixing the inlet Java program, and therefore, the Java program always starts main method carried out.
When we save the code to a file, the file name must be Hello.java, and the file name should also pay attention to the case , and because we want to define the class name Hello entirely consistent . A Java source code can only define a public type of class

How to run Java programs

We've written Hello.java file, javac converts source files to bytecode file Hello.class, and then run on a virtual machine.

Variable Types

java There are two types of variables are: primitive types and reference types :
Reference types There are four: strong, soft, weak, empty, in turn weaken (see links in the article): https://www.cnblogs.com/liyutian/ p / 9690974.html
using reference type distinction, to reclaim memory and memory utilization,
1. strong references will not be recovered,
2. memory enough, soft reference object is not recycled, and only when the memory is insufficient, the system will recover soft reference object, still it does not have enough memory If the recovery after the soft reference object, an exception will be thrown out of memory
3. regardless of whether sufficient memory, as long as the JVM to start garbage collection, those who are weak reference objects are associated Be recycled.
4. The virtual reference always will be recycled, must be used with reference queue, whether the program may have been added to the virtual queue references to see if the referenced object is to be determined by reference to the garbage collector. If the program finds a dangling reference has been added to the reference queue, then it may take the memory object referenced by the necessary action before being recovered
the following basic types of
computer memory storage unit is a minimum of bytes (byte) ,
the FIG. is the number of bytes occupied by
char 2 bytes
short occupies 2 bytes
int occupies 4 bytes
long occupies 8 bytes
float occupies 4 bytes
double occupies 8 bytes

byte:-128 ~ 127
short: -32768 ~ 32767
int: -2147483648 ~ 2147483647
long: -9223372036854775808 ~ 9223372036854775807

Java char type can be represented in addition to standard ASCII, can also represent a Unicode character , we are able to output Chinese.
char zh = '中';

constant

final double PI = 3.14; // PI是一个常量

where

var sb = new StringBuilder();Equal StringBuilder sb = new StringBuilder();
compiler automatically modified at compile time

References role

Understood by reference to the C ++ pointer is a pointer to a memory area, such as string S is cited

public class Main {
    public static void main(String[] args) {
        String s = "hello";
        String t = s;
        s = "world";
        System.out.println(t); // t是"hello"还是"world"?
    }
}

Output is the original S "hello". Point S "hello" corresponding to the memory, and which also points t and s are directed to the new value of the "world", does not affect the t

Analyzing reference type == equal and equals ()

== said that "references are equivalent," or that point to the same object. Even if two variables of each application at the address stored in the same content, but not the same address, not equal.
To determine whether the contents of a reference type variable equal must use equals () method

string s1="hello";
string s2 ="HELLO".toLowCase();//如果直接赋予"hello",则s1==s2为真
s1.equals(s2);//为真

If compared to NULL, an error is reported NullPointerException, to avoid the need for error! = Null Analyzing

And output formatting placeholder

% d formatted output integer
% x formatted output hexadecimal integer
% f floating point output format
% e output format floating point number in scientific notation
% s format string

Array traversal

I ++ may be used as the for each may be

        int[] ns = { 1, 4, 9, 16, 25 };
        for (int n : ns) {
            System.out.println(n);
        }

Sorting an array

Sorting an array actually modify the array itself.
But if the string array sorting and off you have to modify the array itself, but each array element points to change the

Guess you like

Origin www.cnblogs.com/Jun10ng/p/12343804.html