java variables and data types

Variables and variables of the same meaning javascript

In Java, variables are divided into two types: basic types of variables and reference type variable. (The same is true in JavaScript)

Basic data types

The basic data types are CPU type of the operation can be performed directly. Java defines the following basic data types:

  • Integer types: byte, short, int, long

  • Floating-point types: float, double

  • Character types: char

  • Boolean type: boolean

Java basic data types defined what difference does it make? To understand these differences, we must simply look at the basic structure of the computer's memory.

The minimum computer memory storage unit is a byte (byte), a byte is an 8-bit binary number, i.e., the eight bit. It binary representation range from 00000000~ 11111111, in terms of 0 to 255 in decimal, hexadecimal is converted into 00~ ff.

Memory cells are numbered from 0, referred to as a memory address. Each memory cell can be seen as one room, the memory address is the house number.

A byte is 1byte, 1024 bytes 1K, 1024K is 1M, 1024M is 1G, 1024G is 1T. A number that has 4T bytes of computer memory:

4T = 4 x 1024G
   = 4 x 1024 x 1024M
   = 4 x 1024 x 1024 x 1024K
   = 4 x 1024 x 1024 x 1024 x 1024
   = 4398046511104

Different data types are not the same number of bytes occupied. We look at the number of bytes occupied by the Java basic data types:

       ┌───┐
  byte │   │
       └───┘
       ┌───┬───┐
 short │   │   │
       └───┴───┘
       ┌───┬───┬───┬───┐
   int │   │   │   │   │
       └───┴───┴───┴───┘
       ┌───┬───┬───┬───┬───┬───┬───┬───┐
  long │   │   │   │   │   │   │   │   │
       └───┴───┴───┴───┴───┴───┴───┴───┘
       ┌───┬───┬───┬───┐
 float │   │   │   │   │
       └───┴───┴───┴───┘
       ┌───┬───┬───┬───┬───┬───┬───┬───┐
double │   │   │   │   │   │   │   │   │
       └───┴───┴───┴───┴───┴───┴───┴───┘
       ┌───┬───┐
  char │   │   │
       └───┴───┘

byteExactly is a byte, while longand doublerequires 8 bytes.

Integer

For integer type, Java defines only integer with a sign, and therefore, the most significant bit 'bit indicates the sign bit (0 for positive, 1 for negative). Various types of integers that can be expressed as the maximum range:

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

Let's look at an example of the definition of integer:

Define integer

public  class the Main {
     public  static  void main (String [] args) {
         int I = 2147483647 ;
         int I2 = -2147483648 ;
         int I3 = 2_000_000_000; // underlined easier to identify 
        int I4 = 0xff0000; // hexadecimal representation of 16.71168 million 
        int i5 = 0b1000000000; // 512 binary representation 
        Long L = 9000000000000000000L; // Long-type end need to add L 
    } 
}

Special attention: the same represent different binary number are identical, for example 15= 0xf=0b1111

Float

Is the number of decimal floating-point type, since when expressed in scientific notation of decimal, the decimal point can be "floating", such as may be expressed as 1234.5 12.345x102, can be expressed as 1.2345x103, so called float.

The following are examples of defined Float:

a float F1 = 3.14f ;
 a float F2 = 3.14e38f; // 3.14x10 ^ 38 is represented in scientific notation 
Double D = 1.79e308 ;
 Double D2 = -1.79e308 ;
 Double D3 = 4.9E-324; // scientific notation the 4.9x10 ^ -324

For floattype, we need to add fa suffix.

Range of floating-point numbers can represent very large floattype may represent the largest 3.4x1038, and doubletypes represent the largest 1.79x10308.

Boolean

Boolean type booleanonly trueand falsetwo values, Boolean operations are always relationship calculations:

Boolean B1 = to true ;
 Boolean B2 = false ;
 Boolean isGreater. 5 =>. 3; // evaluates to true 
int Age = 12 is ;
 Boolean isAdult = Age> = 18 is; // evaluates to false

ava language of Boolean type of storage does not make provision, since theoretically only need to store a Boolean 1 bit, but inside the JVM will usually booleanrepresented as a 4-byte integer.

Character Types

Character type charrepresents a character. Java's chartype can be represented in addition to standard ASCII, you can also represent a Unicode character:

public class Main {
    public static void main(String[] args) {
        char a = 'A';
        char zh = '中';
        System.out.println(a);
        System.out.println(zh);
    }
}

Note that charthe type of single quotes 'only one character, and to double quotes and "string type distinguished.

constant

Defined variable, if coupled with finala modifier, this variable becomes a constant:

Final  Double the PI = 3.14; // the PI is a constant 
Double R & lt = 5.0 ;
 Double Area = the PI * R & lt * R & lt; 
the PI = 300; // the compile error!

Constant not initialized after the assignment again at the definition, assignment again causes a compilation error.

Role constant is a meaningful variable names to avoid magic number (Magic number), for example, do not write everywhere in the code 3.14, but rather defines a constant. If in the future need to improve accuracy, we only need to modify the definition of the constants of, for example, to change 3.1416, without having to replace all the places 3.14.

According to custom, the constant name is usually all uppercase.

var keyword

In some cases, the type name is too long, too much trouble to write. E.g:

StringBuilder sb = new StringBuilder();

This time, if you want to omit the variable type, you can use varkeywords:

was sb = new String Builder ();

The compiler will automatically infer variable assignment statement according to sbthe type StringBuilder. Compiler, the statement:

was sb = new String Builder ();

In fact, it will automatically become:

StringBuilder sb = new StringBuilder();

Therefore, varthe definition of variables, just write a little variable type only.

Scope variables

In Java, a multi-line statements enclosed by {}. Many control statements, conditional and loop e.g., {} as to have their own range of, for example:

IF (...) { // IF starts 
    ...
     the while (...) { the while beginning 
        ... 
        IF (...) { // IF start 
            ... 
        } // IF End 
        ... 
    } / / the while ending 
    ... 
} // end if

As long as these properly nested {}, the compiler is able to identify the beginning and end of a block of statements. And the variable defined in the statement block, which has a scope that is defined from the beginning to the end of the block. Beyond the scope of these variables are referenced, the compiler will complain. for example:

{ 
    ... 
    int i = 0; // variable i defined starting here 
    ... 
    { 
        ... 
        int x =. 1; // variable x here defined starting 
        ... 
        { 
            ... 
            String S = "Hello" ; // variable s are defined starting from here 
            ... 
        } // variable s scope is over 
        ...
         // Note that this is a new variable s, and above it a variable of the same name,
         // but because the scope differ, they are two different variables: 
        String s = "Hi" ; 
        ... 
    } // variables x and s scope to this end 
    ... 
} // I variable scoping is over

When you define a variable scope should follow the principle of minimizing, as far as possible variables defined in the scope as small as possible, and do not reuse variable names.

summary

Java provides two types of variables: primitive types and reference types

The basic types include integer, floating point, boolean, character.

Variables can be reassigned, the equal sign is the assignment statement, not a mathematical sense of the equal sign.

Constants can not be re-assigned after initialization, use the constant to facilitate understanding of the program intent.

 

Guess you like

Origin www.cnblogs.com/fqh123/p/10962606.html