JAVA zero-based learning (variable)

Base (basic length of unit data)

bit Byte KB MB GB TB PB EB EB YB BB DB CB XB

Brief introduction

Write java program to create a package, you create the class, and then write a program, the program in an idiom only one main function (a program is like a tree, and the main function of the program is the trunk.)

//类
public class Test1 {
    /**
     * 主函数的格式如下
     * 主函数一个类中只有一个
     * 主函数参数可以在执行时候添加数据
     *
    * */
    public static void main(String args[]){
        
    }
}

A. Variables

Concept:
a computer memory storage space, it is the basic unit for storing data.

Syntax:
1. declare, by assignment, then use.
Notice: Data type variable name
assignment: variable name = value; = (assignment operator) is the mean value of the left operand is assigned to the right hand side.
2. Statement and assignment (common)
data type variable name = value
3. At the same statement with a plurality of types of variable
data type: variable name 1, variable name 2, 3 variable name, variable name 4;

 		//先声明再赋值
        int a;
        a=10;
        //声明并赋值
        int b=10;
        //声明多个变量
        int c,d,e,f,g;

Data type:
1. Basic Data Type
1) integers:

name Byte count Two braking length Decimal length
byte 1 byte -27~27-1 -128~127
short 2 bytes -215~215-1 -32768~32767
int 4 bytes -231~231-1 -2147483648~2147483648
only 8 bytes -263~263-1 -922 Kyoto - 922 Kyoto

2) decimals: the approximation (1bit symbol 23bits 8bits mantissa exponent) the IEEE754 standard

name Byte count positive number negative number
float 4 bytes -3.4E + 38 ~ -1.4E-45 1.4E-45 3.4E + 38 ~
double 8 bytes -1.7E + 308 ~ -4.9E-324 4.9E-324 ~ 1.7E + 308

3) Character: Character
char 2 bytes 0 ~ 65535 ASCII / Unicode encoding
assignment mode: 'A' 65 '\ u0041 '
escape character: \ u '\ \ n \ t

4) Boolean:
Boolean 1bit. 1 byte (in) values: true / false state used for each

2. The reference data types:
I. string: String "literal"

Data type conversion

Automatic type conversions:
I. Two types compatible (with a number or character)
II type is greater than the target source type (length of the data is relatively increased).
Cast:
I. Two types are compatible. (With a character or number)
II. Type source is less than the target type. (Length of the data is relatively decreased)
cast Rule
I. integer suitable length, data integrity.
II. Discomfort integer length, data truncation.
III. Decimal integer, data truncation, lose precision (16-bit case below decimal)
IV. Character integer conversion, data integrity.
V. boolean value of true / false, non-convertible.

  double b=1.12;
        //强制类型转换
        int a=(int)b;
        //强制类型转换
        byte c=(byte)b;
        //强制类型转换
        long d=(long)b;

Note:
1, there will be time to cast data truncation might cause data errors
automatically type promotion:

  1. Two operands, there is a double, as a result of lifting double.
  2. If you do not double, there is a float, the result promoted to float.
  3. If you do not float, there is a long, the result promoted to long.
  4. If there are no long, there is a int, with a result promoted to int.
  5. If no int, are byte or short, the result is still an int.
  6. NOTE: Any type String and adding (+) when, in fact, stitching, as a result automatically promoted to a String.
  //自动类型提升
        int a1=12;
        //自动提升为double 所以接收错误。
        int b1=a1+1.2;
        //自动提升正确;
        double b3=a1+1.2;
        byte c3=1;
        //整形会自动变为int
        byte c4=c3+1;
        //接收正确
        int c5=c3+1;
        //接收错误,任何数拼接字符串都是字符串,前拼后拼一样。
        int c6=c3+"xxx";
        //接收正确
        String c7=c3+"xxx";

Expression:
variables or literal connection using operators, and finally get a result.
Console Input:

  1. import java.util.Scanner; // guide packet
  2. Scanner input = new Scanner (System.in); // declare a variable of type Scanner
  3. Receiving different types of data:
    .nextInt (); // integer
    .nextDouble (); // decimal
    .next (); // string of
    the first character string //; .next () charAt (0) .

Scope variables

Global variables
declared the whole class available in the class declaration role. Called class variables or instance variables.
Local variables:
a statement in the function, only in the starting position scope function.

Released two original articles · won praise 0 · Views 44

Guess you like

Origin blog.csdn.net/qq_41946564/article/details/103946649