Java data types, constants, variables

A data type

Java is a strongly typed language. This means you must declare a type for each variable . A kind of eight basic types, of which four kinds of shaping, two kinds of floating-point, one kind of type char character for representing Unicode characters units (see section discussed char type) and 1 to represent the true value the boolean type.

There can be a Java represents arbitrary precision arithmetic package , commonly called "big value" (big number.) Although a large numerical referred to, but it is not a new type of Java, but a Java object .


Integer

Integer. No fractional part.

  • a long occupies 8 bytes 8 bytes 64 may represent a power of two number -9223372036854775808 ~ 9223372036854775807
  • int 4 ~ byte range -2147483648 2147483647 most common
  • short 2 bytes -32768 to 32767  
  • byte 1 byte -128 to 127

In Java, integer range independent of the machine to run Java code. Whether running on that machine, the byte length occupied by shaping unchanged. C and C ++ programs need to choose the most efficient integer for different processors, so it may cause a good run C programs running on 32-bit processor integer overflow has occurred in the 16-bit system. Since the Java program must ensure that all the machines are able to get the same results of the operation, the various data types in the range must be fixed.

Suffix have a long integer value L or L (L is lowercase note, such as 40000000000000L), hexadecimal number or a prefix 0x 0X , binary prefix 0b or 0B , octal prefix 0, it is not recommended. From Java 7, also underlined that the digital literal, as represented by 1_000_000 one million. These underscore just to make people more readable. Java compiler will remove these underlined.


Floating-point type

He expressed the number of fractional part.

float 4 bytes about ± 3.402 823 47E + 38F (effective number of bits 6 to 7)

Yu. 8 double section about ± 1.797 693 134 862 315 70E + 308 (effective number of bits is 15) double precision is relatively high, generally used double

Few cases float type suitable for use, for example, require a single-precision data library, or need to store large amounts of data. It has a value of type float suffix F or f (e.g., 3.14F.)

No suffix F floating point value (e.g., 3.14) the default type double . Of course, the value may be D or D suffix (e.g., 3.14D) behind float.

In addition, the float is represented by a binary, decimal precision is not high. So, System.out.println (2.0-1.1) will print .8999999999999999, rather than people think 0.9. If not there is any rounding errors in the numerical calculation, BigDecimal class should be used.


char type

Two bytes.

char type character needed 'single quotation marks, using the character string "," double quotes. Such as 'A' and "A" represents a represents a character string. Type char may be represented as hexadecimal values, which range from \ u0000 to \ Uffff.

UTF-16 encoding using encoding different lengths represent all Unicode code points. Multilingual basic levels, each character is represented by 16, commonly referred to as unit codes (code unit); recommended not to char type in the program , unless absolutely need to handle UTF-16 code units. Preferably the processing string as abstract data types .


boolean type

Boolean (Boolean) type has two values: false and true, can not be used to determine conditions to convert between logical and Boolean integer values.


Second, variables and constants


constant

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

  PI = 3.14 Final

Constant not initialized after the assignment again at the definition, assignment again causes a compilation error. According to custom, the constant name is usually all uppercase.


variable

Java, declare a variable must specify the type:

int number ;

double salary;

By a variable name must be a letter or a numeral sequence beginning with the letter, case sensitive, almost unlimited length. Camel notation is recommended not to use java keyword. 

Variable initialization

After declaring a variable, the variable must be explicitly initialized with an assignment, do not use an uninitialized variable.

Java compiler think the following statement sequence is wrong:

int vacationDays;

System.out.println(vacationDays);// ERROR variable not initialized

You can be declared assignment:

int vacationDays;

vacationDays:12;

It may also declare the completion of the assignment in a row:

int vacationDays = 12;

In Java, declare variables as close as possible to use local variables for the first time, which is written in a style of a good program.

Guess you like

Origin www.cnblogs.com/wybslj/p/11226182.html