Variable type

Escape character

The original meaning of a character conversion, using \the conversion. Commonly used are:

  •  : Dedicated as escaped slash, having specific ambiguity can be coupled with the output
  • \ T: tab, the equivalent of a tab key, the default is 8 characters. In particular, where the context may be specific.
  • \ N: line breaks represent newline

Basic data types

Data type is the data type, can be divided into literal data types. And not only understand the data type of the variable data types, variables

classification

Basic data types

Simple type used in most

Four categories of eight type, type all eight key

  • Integer type (int)
    • byte, byte, 1 byte space, represents the range of -128 to +127
    • short, occupies 2 bytes and indicates the range of -32768 to +32767
    • int, is the default integer types in Java (that is, if you do not specify a constant or variable of type int, int range if more than we should add the suffix). 4 bytes. -21 billion to 2.1 billion +
    • long, 8 bytes for the count time is the most common.
  • Floating-point type (float)
    • Are approximate values ​​expressed in floating-point, is itself an approximation
    • float, single precision floating point type, 4 bytes, f represents the fractional need to add in the back, or F.
    • double, the Java floating point type default (if not specified that it is the double of the constant or variable, is specified later suffix), double precision floating point type, 8 bytes. When defining a float with a decimal point will default to double-type, with a float are not defined. This time want to use float values ​​defined later when we must again float plus F job. If there is no decimal point can not add, that does not involve floating point numbers.
  • Character type (char)
    • char, it represents a single character occupies 2 bytes, a Chinese character occupies 2 bytes, Chinese characters can be saved, not saved null character.
  • Boolean type (Boolean)
    • boolean, only two values, true, false
  • Note:
    • String string is not a basic type, but reference types
    • Size numeric type are sorted, byte-> short-> int-> long-> float-> double
    • Floating-point data representation is an approximation, not a specific value
    • float type accepts a decimal must write "f" or "F"
    • long type of data, if more than is necessary to add a range int "L" or "l"
    • Suffixes are generally recommended capital
  • pay attention:

    • Reference CSDN: This also say java is kind of a strongly typed language, you're doing the assignment operator when in fact等号两边的类型都是确定的 , the assignment can succeed or both sides of the same type, or types on both sides with (inherited or achieved) some kind of relationship, either by the compiler context can be determined, for example, 1, byte x = 1; 1 is an integer literal, is an int, the value in the type byte range, this time the compiler will help you do type conversion, leaving only an int 8 , leading zeroes are removed. 2, float type literal default type is double, but due to different types float and double types of binary representation in memory, not as the default conversion sometimes like plastic surgery, we must determine the type, take f.
    • The default is int int int scope than necessary to add a suffix to tell Java which is long type. So when the time assigned to the long int value exceeds the scope necessary to add Lthis to the right to continue additional.
    • When a literal byte int assignment because, Java detected automatically converted into byte size.
    • double to float assignment when we should add F, because Talia is not a model that tells it which is F-type.
    • byte and int are brothers, all literals excessive and should be reduced suffix table, but maybe not automatically detect the size of an extra plus.

      Reference data types

Interface, the whole thing's a
String string and adding any type of type String

Object-oriented and then go into detail learned

variable

Concept: within reason to keep changing data
variables must be initialized when defined, but the members of the (global) initialization of Java have default assignment 0, the error will not use. But local initialization no default value assigned to fend for themselves, or a use on the error. It can be said to be sure to initialize.

  • At the same time the definition of assignment

    • Data type variable name = value;
    • Data type = 1 the value of the variable 1, variable 2 values ​​= 2, the value of the variable 3 = 3;
  • Define, after the assignment

    • Variable name data type; variable name = value;
  • Note:

    • A code segment can not be the same in the redefined variable assignments may be repeated.
      • But if the same name is the variable of death, you can use a variable name.
      • This happens, variable scope of the dead. Scope field variables can be used.
    • To initialize variables to use. But the global variable is a global variable is the default value.
    • A byte, short time to do the four operations will be automatically converted to an int.

Operators

  • Operator Operation

    + - * / ++ -- %

    • int int type and type of operation when the floating point operation is not involved, only the received int integer, decimal, will wipe.

    • Two variables or constants result with integer arithmetic is integer, to decimal can only make data and fractional arithmetic, either with its own novel itself is a fiction, and either go to 1.0 this operand bit, no loss of data.

    • Two types of data operation, the result will be a large program that accuracy.

    • The division, the division between the integer precision loss will occur.

    • Modulus, modulus of symbols from symbols modulo.
      整除的结果是0
      结果的正负号取决于被模数的正负
    • addition:

      • Summing

      • String concatenation, and the string of data using any type of data "+" string will become the final splice.

        "5+5="+5+5 输出 5+5=55    错的
        "5+5="+(5+5) 输出 5+5=10  对的
        只要改变计算的优先级就可以解决,程序是顺序执行的
      • Adding an integer and character types subtraction, subtraction is to ASCII code. The final results are returned based on the return type. Will be converted into character data corresponding to a decimal integer, then added to an integer.
        Unicode code table, the former 128 and the same ASCII code table, code table and a collection of various countries and regions. multimeter.

    Unary operators++ --
    • ++ x is equivalent to x = x +1;
    • From left to right calculation
    • Pre- and post-symbols are possible, without affecting their individual operation results
    • This represents the first post-unary perform again (over again), and then again after his change in value, after the value has changed and the value previously executed does not matter. This change only the value of the equivalent value for the next use this preparation. Simple point is: first perform re-assigned (variable value).
    • Front on behalf of first assignment (variable value) and then perform other operations.
    • It can also be fractional, calculations.
  • Assignment Operators = += -= *= /= %=
    • =: Assignment
    • + =: I + = 3; etc. 价于 i = i +3;
    • Note:
      • Constants can not be assigned
  • Comparison operators == > < >= <= !=
    result of the comparison operation is a boolean value
    • ==: equality
    • ! =: To determine whether the two numbers ranging from
  • Logical operation & | ! && || ^
    result of the logic operation is a Boolean value
    • &: And determines whether or not the same on both sides of the formula is true, the same is true is true
    • |: Or, as long as one side is true is true
    • !: Africa, negated
    • ^: XOR, not simultaneously true
    • &&: & and the role of the judge as just the first left, when it is judged to be false when the left is not a judgment on the right. Return result. And & will judge both sides. And more work with
    • ||: as long as the judge left true when the direct returns true. The right not to judge. And more work with
  • Ternary operator
    result ternary operator expression must be an exact value
    • Format: Expression 1? Expression 2: Expression 3;
    • Logic: Expression 1 is true, execute the expression 2, expression 3 1 expression is false performed.
  • Bitwise
    only the time will be a constant bit operation to convert the decimal number into a binary number. The logic, or operation, and finally to decimal.

      或    System.out.println(3 | 4);   输出 7
      与    System.out.println(3 & 4);   输出 0
      或    System.out.println(3 | 10);  输出 11
  • The left

    System.out.println(3 << 1); 输出 6  
    将3向转换成二进制,然后向左移1位,变大了。
    其实就是 3*2^1
    比如 3 << 4 就是 3*2^4 等于 48

    Type Conversion

    Can operation between different data together? is allowed.

    Automatic type conversion (implicit)

  • Concept: calculating different data types, data types of small data range, will be automatically converted into a wide range of data types, without loss of precision, it is reasonable common way, small or large turn. But big data range of data can not be converted into small data range of data , regardless of the value is large or small, can only use casts. Because it is not the value of the magnitude, but the type of the size range. As long as the type of large range of revolutions than the type of range, turn to be strong. But strong move led to the loss of accuracy, not recommended. Variable types mentioned here are not only variable, but there is also a literal
  • 范围:byte->short->int->long->float->double
  • Question: No big turn a small, but why be short x = 8;so short assignment to an int can it? Yes it is indeed an int, but Java detects that it is consistent with the scope, it can be assigned. But if so int x = 8;byte s = x;this would not be, because Java does not know how much is the value of x is not so, as it happens so ide ide know ahead of time being given to you.
  • pay attention:
    • ** In java operation, when it comes to the range of less than int data type (e.g. byte, char, short), the data type is automatically elevated to int type, when they result to int lift can not be used when less than int the type of the received data, the worst should be strong turn, or else go wrong. For example, when two types of data byte operation is performed, the calculation result becomes int type, if the result is a byte, then it would turn into strong type byte, because the internal compiler java process them. For example: char * short is int. For example: char + float = float this is not the three internal operations. Is to be understood here, between three know their operation will increase as the int, int is greater than even other types of calculation will be upgraded to three outer int, char + float from the results can be seen for the figures. **
    • Increment decrement inherent cast, without error ++ -- += *= /=.
    bb = (byte)(bb + 1); 不报错 以int运算返回int,又转成byte
    bb = (byte)bb + (byte)1; 报错 最后又变成int进行运算,返回int

Casts (strong rpm)

  • The basic data types are not recommended data type conversion, also known as cast.
  • Action: in a specific format to convert the data type of a large range of data into small range of data types.
  • Format: small variable name = data type (type target) a wide range of data values;
  • Note: The basic daily life without the use of the basic data type conversion, cast, because most of the conversion would result in the loss of accuracy, do not allow it to deal with the error, no good.
  • The basic data types are not recommended for mandatory conversion.

    to sum up

  • You can turn small type range
  • No big turn a small range of types, stronger turn, will lose precision
  • three short byte char four operations will be transferred to each other because their range is too small int
  • + - + = strong automatically transferred etc.

Guess you like

Origin www.cnblogs.com/macht/p/11567474.html