Java foundation - constants, variables, scopes

Long way to go ~ ~

1, Constant

1.1 Overview

Constant: is a Java program to run during the fixed data

1.2 Classification

Types of meaning Data example
Integer constant All integer 0,1,2332,-12
Decimal constant All decimals 0.0,-0.4,99.99
Character constant Single quotation marks, can only write a character, you must have content 'A', '', 'yellow Sister'
Boolean constants Only two values true,false
Constant air Only one value null

2, variable

  • Variable: Variable data is fixed, then the amount in the program can be called a variable number of

Mathematics, letters may be used instead of the digital calculation, e.g. x = 1+ 5 or 6 = x + 5

Program can use letters stored digitally calculates, increased computing power can solve more problems, such as x save 5, x 6 can also be saved, so x saved data can be changed, that is, we explain Variables

Java, requires a variable can only hold one data must be explicitly saved data types

Create a variable and use format:

数据类型 变量名称; // 创建了一个变量
变量名称 = 数据值; // 赋值,将右边的数据值,赋值交给左边的变量

一步到位的格式:
数据类型 变量名称 = 数据值; // 在创建一个变量的同时,立刻放入指定的数据值

3, Data Types

3.1 data type classification

Java data types are divided into two categories:

  • Basic data types (primitive type): comprising 整数, 浮点数, 字符,布尔
  • Reference data types (reference type): comprising , 数组,接口

3.2 Basic Data Types

Four categories of the eight basic data types:

type of data Keyword Memory footprint Ranges
Byte byte 1 byte -128 ~ 127
Short integer short 2 bytes -32768 ~ 32767
Integer int (default) 4 bytes -2^31^ ~ 2^31^-1
Long integer long 8 bytes -2^63^ ~ 2^63^-1
Single-precision floating-point float 4 bytes 1.4013E-45 ~ 3.4028E+38
Double-precision floating-point double (default) 8 bytes 4.9E-324 ~ 1.7977E + 308
Character char 2 bytes 0 ~ 65535
Boolean boolean 1 byte true、false

The default type in Java: integer type int, float typedouble

3.3 reference data types

Strings, arrays, classes, interfaces ......

Precautions
  • Strings are not primitive data types twenty-reference type

  • Float knowledge may be an approximation, not a precise value

  • The number of bytes of data does not necessarily correlate with the range of, for example, a wider range than the data float long, but float is 4 bytes, long 8 bytes

  • Float among the default type is double, if you must use a float, you need to add a suffix F

    If is an integer, the default type int, if we must use the long type, you need to add a suffix L. We recommend the use of capital letters suffix

4, type conversion

  • Because Java is a strongly typed language, all the time to perform some operation, you need to use a type conversion

  • In operation, to different types of data into the same type, and then computes

  • Automatic conversion : will 取值范围小的类型automatically promoted取值范围大的类型

  • Cast : The 取值范围大的类型cast to取值范围小的类型

    • Conversion format :

      数据类型 变量名 = (数据类型) 被转换数据值;
    • Features: format code requires special processing, not automatically

Precautions:

  1. Casts are generally not recommended because of the potential loss of precision occurs, data overflow
  2. byte / short / char three types can occur mathematical operations, such as addition "+"
  3. byte / short / char when these three types of operation, will be called the first lift type int, then calculate
  4. boolean type Data type conversion can not occur

5. Scope

Starting line defined variables, until the end all the way to direct your braces

Guess you like

Origin www.cnblogs.com/huangdajie/p/12409944.html