Java review two basic data types and variables and constants

  1. Java basic data types
  2. Java variables and constants

Java basic data

Integer type

        For storing integer data type, it may be a positive integer, and may be a negative integer and 0, but not floating point. There are three forms of integer constants in Java: decimal, octal, hexadecimal

Decimal: Java variables normal form of expression, not 0 as the beginning, in addition to 0.

Octal: beginning with 0 as the most significant bit.

Hex: beginning with 0x highest position.

        Integer variable according to the size of the share itself can be divided into in-memory byte, shot, int, long, four kinds

type of data Memory space Ranges
byte 8th -128~127
short # 16 -32768~32767
int # 32 -2147483648~2147483647
long # 64 -922337036854775808~922337036854775807

 

         Description: adding L or l (lowercase L) identifies the end of the long type integer number, usually occur when an integer greater than the maximum range int. Because Java is the default integer type int.

Floating-point type

          Represents floating-point number with a decimal part can be divided into single-precision floating point, and double precision floating point decimal. Floating-point data types

type of data

Memory (8 bits equal 1 byte)

Ranges

float

# 32

1.4E-45 ~ 3.4028235E38

double

# 64

4.9E-324 ~ 1.7976931348623157E308

                  Java language using the double double-precision floating-point default type, just like int is the default integer type. If the real number is assigned to a variable of type float, you need to add f or F as a suffix at the end of a real number, otherwise it is considered to be a double value. When you declare a double type of data, you can use the suffix "d" or "D" to clearly indicate that this is a double data type. But did to "D" or "d" is not mandatory, you can add also can not add, but when you declare a float variable if not "F" or "f", the compiler will be considered a double and an error.

Character Types

          Character type (char) for storing a single character, occupies 16 bits (two bytes) of memory. When defining a variable character, to represent a single quote, for example, 's' represents a character. The "s" indicates a string, though it has only one character, but because of the use of double quotation marks, it still represents a string instead of the character.

           The escape character is a special character variable. Backslash escape character "\" followed by one or more characters. Escape character has a specific meaning, different from the original meaning of the character, so called "escape." For example, format string printf function used in "\ n" is an escape character, meaning "carriage return line." Java escape characters in the Table.

Escape character

meaning

\ddd

Characters 1-3 octal data represented as: \ 456

\dxxxx

4 hexadecimal characters represented, such as: \ 0052

 \'

Single-quote character

\\

Backslash character

\t

Vertical tab, move the cursor to the next tab position

\r

Enter

\n

Wrap

\b

backspace

\f

Feed

Boolean

         Also known as Boolean logic type, only two values ​​true and false, represent Boolean logic "true" and "false" Boolean value can not be converted to an integer type. Boolean type commonly used as a determination condition in the process control system.

          The definition of Boolean variables can be defined by the keyword boolean. It is defined as follows:

                       boolean b; // define a Boolean variable b

                       Boolean b1, b2; // define Boolean variable b1, b2

                   Note: Due to the Boolean data type exists only true value and false, so when assigned to a boolean variable, anything other than these two values ​​are illegal.

Java variables and constants

         Identifiers and keywords

                Identifier: only underscores and alphanumeric by $ constitutes, in which the first letter not a number, and keyword can not be used as identifiers. Identifier strictly case-sensitive.

               Keywords: Keywords are words in the Java language has been given a specific meaning. These words can not be used as an identifier. Int data type mentioned, boolean are all keywords, Java keywords Table:

       Java keywords

int

public

this

finally

boolean

abstract

continue

float

long

short

throw

throws

return

break

for

static

new

interface

if

goto

default

byte

do

case

strictfp

package

super

void

try

switch

else

catch

implements

private

final

class

extends

volatile

while

synchronized

instanceof

char

protecte

importd

transient

implements

dafaule

double

          Define the variable

                  Variables can be understood as a box, the box in which you can install some of the items and size of these items and boxes Who determines it? This is related to the previous point summary - data types. Java language is strongly typed language, variables must be declared before they can be used. When a variable declaration for the data type depending on its size specification box, and determine the desired article inside the box.

                   Use the box, first you have to know the name of the box - variable name. Which is the same identifier naming rules.

 

          Declare a constant 

                 final constant data type name [= value]

When defining the final variables are "member variables", then it must set its initial value at the time of definition.

          Effective range of variables

                 Since the variable is defined it, but temporarily stored in memory, program execution to wait until after a certain point, the variable will be freed, that variable has its life cycle. The effective range of the variable is the area code to access the program variable, if the variable is outside the area access error occurs at compile time. In the process, typically based on the effective range of the variable, the variable will be classified as "member variables" and "local variable."

                1. Member variables: variables defined class body member variable is referred to. Member variables are valid in the whole class, the member variable is also referred to global variables. Member variable is the effective range of the entire class of code segments, i.e. anywhere in the body type of the variable can be used. Class member variables can be divided into two kinds of static and instance variables.

                             Static variables:

                                   The valid range is the entire class static variable, and all instances of the class can be shared. Static variables can be accessed through the "class name Static variable name" approach. Static variable depending on the life cycle of the life cycle of the class when the class is loaded, to allocate memory space for the class static variable, when unloading the class, freeing up space occupied by static variables, static variables are destroyed. When the class is loaded, it is assigned a static variable memory space, then no matter how many instances of the class is created, will no longer allocate memory space for static variables, these examples will use the same static variable.

                             Examples of variables: instance variables and instance of the class corresponding to that instance of the entire effective range. Each instance of a class is created, the current instance will allocate memory for the instance variables. So the life cycle of an instance variable depending on the lifecycle instance, when instance is created, allocated memory space for the instance variables, when the instance is destroyed, the release of the memory space occupied by the instance variables.

                  2. Local variables: variables defined in the body of the method is a kind of local variables, local variables only in the current code block (i.e. flower in brackets) valid. Lifecycle local variable declaration code blocks depends on the location, outside the range block can not be used in local variables within the block.

Take the class method that declared in the method variables, including variable parameters are all local variables when the method is called, the method of local variables allocated memory space for the Java Virtual Machine, when the end of this method is invoked, it will release the method of memory space occupied by local variables, local variables will also be destroyed, then Java's garbage collection will clean up memory space at a time.

Note: Local variables can be the same as the name of the global variable at this time of global variables are hidden, but can be used to access the global variables prefixed "this.".

Guess you like

Origin www.cnblogs.com/gaochunhui/p/11022676.html