Introduction to the

Java variables are classified into class variables, instance variables, and local variables. In the Java language, all variables must be declared before they can be used. Defining a variable tells the compiler its data type, so the compiler knows how much space to allocate to it and what kind of data it can hold. As the value of the space changes during program execution, the memory space becomes the variable. For ease of operation, give this variable a name, called the variable name.

Variable names must follow the naming rules for identifiers.

More video learning >>>www.bilibili.com/video/BV1oy…

Naming rules for identifiers

The Java identifier must start with a letter, underscore (_), or dollar character and be followed by a letter, underscore (_), dollar character, or digit. The Java identifier is case-sensitive and has no limit on length

  1. Java identifiers contain 26 letters (uppercase and lowercase), digits, underscores (_), and the dollar sign ($), but cannot start with a number.
  2. Class names must start with a capital letter. If multiple words are used, each word must start with a capital letter.
  3. Method names usually start with a lowercase letter (constructors are an exception) and consist of multiple words followed by uppercase letters.
  4. Variable naming rules same method name name.

You cannot use Java keywords as identifiers

Class variables (static variables)

  1. Class variables, also known as static variables, are declared with the static keyword in a class, but must be outside the method constructor and statement block

  2. No matter how many objects a class creates, a class owns only one copy of the class variable

  3. Static variables are rarely used except when they are declared as constants. Constants are variables declared as public/private, final, and static. Constants cannot be changed after initialization

  4. Static variables are stored in static storage. Is often declared as a constant, and it is rare to declare a variable static alone

  5. Static variables are created at the beginning of the program and destroyed at the end

  6. Similar visibility to instance variables. However, most static variables are declared to be of type public for visibility to users of the class

  7. Default values are similar to instance variables. The default value for numeric variables is 0, the default value for Boolean is false, and the default value for reference types is null. The value of a variable can be specified at declaration time or in the constructor. In addition, static variables can be initialized in a static statement block

  8. Static variables can be accessed with: classname.variablename

  9. When a class variable is declared as a public static final type, the class variable name must use uppercase letters. If static variables are not public or final, they are named the same way as instance variables and local variables

The instance variables

  1. Instance variables are declared in a class, but outside methods, constructors, and statement blocks

  2. When an object is instantiated, the value of each instance variable is determined

  3. Instance variables are created when the object is created and destroyed when the object is destroyed

  4. The value of an instance variable should be referenced by at least one method, constructor, or statement block so that outsiders can retrieve instance variable information in these ways

  5. Instance variables are visible to methods, constructors, or statement blocks in a class. In general, you should make instance variables private. Instance variables can be made visible to subclasses by using access modifiers

  6. Instance variables have default values. The default value is 0 for numeric variables, false for Boolean variables, and null for reference variables. The value of a variable can be specified at declaration time or in the constructor

  7. Instance variables can be accessed directly by variable names. But in static methods and other classes, object reference access should be used

  8. A local variable

  9. Local variables are declared in a method, constructor, or statement block

  10. Local variables are created when a method, constructor, or block is executed and destroyed when they are completed

  11. Access modifiers cannot be used for local variables

  12. A local variable is visible only in the method, constructor, or statement block that declares it

  13. Local variables are allocated on the stack

  14. Local variables do not have default values, so local variables must be initialized before they can be used

Variable data type

In Java, variables are divided into two types: primitives and reference types.

Of these, eight basic data types are embedded in the Java language and have the same large properties in any operating system, while reference data types are variable types defined by the programmers themselves in Jawa programs.

1. Variable integer type

Integer type variables are used to store integer values, that is, values without fractional parts. In Java, there are four different types of integers: byte, short, int, and long, in order to allocate proper storage space for integers of different sizes

Byte 8 bits (signed) -2^7 to 2^7-1 0 short 16 bits (signed) -2^15 to 2^15-1 0 int 32 bits (signed) -2^31 to 2^31-1 0 long 64-bit (signed) -2^63 to 2^63-1 0L or 0L

The smallest unit of memory in a computer is a byte. A byte is an 8-bit binary number, or 8 bits. Its binary representations range from 00000000 — 11111111, which is 0 — 255 in decimal and 00 — ff in hexadecimal.

Variable floating point type

Floating-point type variables are used to store decimal values. In Java, floating point types fall into two categories: single-precision floating point (float) and double. Floating-point numbers are represented more accurately by double than by float, as shown in the figure below.

Type Bit Range Default value Float 32 bits (compliant with IEEE 754) 1.4E-45 to 3.4028235E38 0.0f Double 64 bits (compliant with IEEE 754) 4.9E-324 to 1.7976931348623157E308 In Java, a decimal is used as a double by default, so when assigning a value to a float, always follow the letter F(or lowercase F). For a double, always follow the character D(or lowercase D). Or you don’t have to.

3. Boolean type of variable

Boolean has only two values true and false, the default is false

Boolean has nothing to do with whether it is 0 or not, but can be converted according to the desired logic

4. Variable reference types

There are many types of reference data, including classes, interface types, array types, enumerations, annotation types, strings, and simply put, all non-basic data types are reference data types

Any reference to a datatype variable is stored in the heap, and the stack is the memory address of its specific content; Non-global basic datatype variables defined in a method are passed numerically as parameters when the method is called, whereas reference datatype variables are passed by reference as parameters when the method is called

Conversions between primitive types

Boolean types cannot be converted to all seven other types.

In Java, any of the other seven numeric types can be converted between them, but there may be a loss of accuracy or other changes. Conversions are divided into automatic conversions and casts. Automatic conversions (implicit) require no operation, while casts require explicit conversions, using the conversion operator (type).

The values are in descending order as follows:

byte short=char int long float double

Automatic conversion:

A type with a small value range can be automatically converted to a type with a large value range.

Because larger types (such as int) hold smaller types (such as byte), memory is always sufficient and no casts are required.

Int can be assigned directly to data types such as byte, short, char, and so on, without casting, as long as it does not exceed the representation range of that type.

In automatic type conversions, there is no loss of precision except for the following situations that may result in loss of precision.

int–> float long–> float long–> double float –>double

Aside from the possible loss of accuracy, the automatic conversion does not incur any runtime exceptions.

Cast:

A cast must be performed when a type with a large value range is converted to a type with a small value range

Casts take the conversion operator (type). Technically, converting byte to char is not a cast, because the process from byte to char is actually byte–>int–>char, so it has both. In addition to the possible loss of accuracy, the cast may also change the overall magnitude.

In the case of over-range strong transformation, the result needs to be obtained from the range of the target type.

Truncation occurs when assigning a floating point type to an integer type. So you take out the decimal part, and you leave the integer part. If the integer exceeds the range of the target type, the remainder of the range of the target type will be used as well.

Basic data type conversion rules:

Boolean and all seven other types cannot be converted. Integers can be directly assigned to byte, short, or char, but cannot exceed the value range. Byte, short, and CHAR data are all converted to int before operation.

That’s all for today! Don’t forget to like it