variable concept

There are two types of variables in java: primitive primary data types and reference variables. Java is a type-conscious language. A variable must have a type, and it must have a name variable name.
Primitive main data type:
type bit value field
value Integer (with sign)
Byte 8bits (bit) -128~127
Short 16bits -32768~32767
Int 32bits -2147483648~2147483649
Long 64bits - very large ~ very large
Boolean and char
Boolean (determined by the java virtual machine) true or false

Char 16bits 0~65535
floating point
Float 32bits range variable
Double 64bits range variable The
definition float type is to add f after the value, if not, it will be treated as double by java Type data processing.
There are also some rules for defining the names of variables:
variable names must start with a letter, underscore (_), or $ and cannot start with a number. Except for the first letter, numbers can be used after them, and they must be named without java keywords.
In java, the "=" symbol is usually used for assignment, and the value after "=" is assigned to the previous variable.
Java is a strongly typed language. You can't pack the data of int into the byte type, like this
int a=23; byte b=a; this is not allowed, the virtual machine will report an error when compiling, even if you know that the value of 24 can be placed in the byte.

Reference variable: The reference variable is used to refer to the object on the heap. It does not store the object, but only the reference of the object. The object is stored on the heap, and the reference variable is on the stack. An object can have multiple references. A reference can only refer to one object at a time.
After talking about the above two variables, let's talk about array variables.
An array variable is a reference to an array object, so an array variable is still a reference variable after all.
When creating a new array, for example: int[] nums=new int[3];
First, create an array reference variable named nums, then create an array of size 3 and type int on the heap, and finally point the object to reference variable. It should be noted that although an array is an object on the heap, its elements can be defined as primitive primary data types and object types (first you need to define the type of elements stored in the array) For example:
Object[] obj = new Object[ 5];//The object reference variable type is stored, because Object is the parent class of all classes, and polymorphism is used. String[] num=new String[3];//The string type is stored.
There are many other types, too many to list.
It should be noted that after the array elements are defined, the elements in the array will have default values. The primitive types of short, byte, int and lang are 0, double and float are 0.0, char is the space symbol, boolean is false, and object references use The comparison of null

variables

For primitive primary data types, using "==" can be used to compare two variables, return true if they are equal, and return false if they are not equal.
For reference variables, the "==" symbol is used to compare the value of the reference variable instead of whether the referenced objects are the same.
Using "==" to compare two reference variables and return true means that the values ​​in the two reference variables are equal. , that is, the two reference variables refer to the same object; an equals method is defined in the parent class Object of all classes, and the equals method of Object is only defined to return a boolea value through "==", so in the javaAPI there are Many classes override the equals method of Object, and the specific method logic needs to be implemented in a specific class, that is to say, the code of the method of the class that overrides the equals method is different. But in general, if "==" returns true, it means that the byte combination of the reference variable is equal, indicating that the two reference variables are the same object referenced. The same combination is of course the same if it is compared using equals, even if it is The overridden equals method will also return true.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615034&siteId=291194637