0 Basic Learning Java: variables (i)

Source: Internet
Author: User

First, the concept of variables:

(1) One storage area in memory

(2) The region has its own name (variable name) and type (data type)

(3) Each variable in Java must be declared first and then used

(4) Data in the region can vary from one type to the other

Use variables Note:

(1) Scope of the variable: valid between a pair of {}

(2) Initialization value

Define the format of the variable: data type variable name = Initialize value

Ii. Classification of variables--by data type

Specific data types are defined for each type of data, and memory spaces are allocated in memory in different sizes.

1 , Integer type: Byte, short, int, long

Java integer types have a fixed number of table ranges and field lengths that are not affected by the specific OS to ensure portability of Java programs.

The integer constants in Java are by default int, declaring long constants to be followed by ' l ' or ' l '

2 , floating-point type: float, double

(1) Similar to integer types, Java floating-point types also have a fixed number of table ranges and field lengths that are not affected by the specific OS

(2) The floating-point constants in Java default to double, declaring a float constant, which must be followed by ' F ' or ' F '.

(3) There are two types of expressions for floating-point constants:

Decimal number form: 5.12 512.0f. 512 (must have decimal points)

Scientific notation form: For example: 5.12e2 512E2 100E-2

3 , character type: Char

Char-type data is used to denote "characters" (2 bytes) in the usual sense

Three representations of character constants:

(1) Character constants are single characters enclosed in single quotation marks ("'), covering all written words in the world.   For example: char c1 = ' a '; char C2 = ' Medium '; Char c3= ' 9 ';

(2) The escape character ' \ ' is also allowed in Java to convert subsequent characters into special-character constants.  For example: char c3 = ' \ n '; ' \ n ' indicates a newline character.

(3) Use Unicode values directly to represent the character type constants: ' \uxxxx '. where xxxx represents a hexadecimal integer. such as: \u000a means \ n.

(4) The char type is available for operation. Because it's all about the Unicode code that should

4 , character encoding

(1) ASCII code

Inside the computer, all data is represented using binary notation. Each bits (bit) has 0 and 12 states, so 8 bits can combine 256 states, which is called a byte. A byte can be used to represent a total of 256 different states, each of which corresponds to a symbol, which is 256 symbols, from 0000000 to 11111111.

ASCII code: In the 60 's, the United States developed a set of character encodings, which made a uniform provision for the relationship between English characters and bits. This is known as ASCII code. The ASCII code specifies a total of 128 characters, such as a space "space" is 32 (binary 00100000), the uppercase letter A is 65 (binary 01000001). These 128 symbols (including 32 control symbols that cannot be printed out) take up only one byte of the latter 7 bits, and the first 1-bit uniform is 0.

(2) Unicode Coding

Garbled: There are many coding methods in the world, the same binary numbers can be interpreted as different symbols. Therefore, if you want to open a text file, you must know how to encode it, or the wrong encoding to interpret, there will be garbled

Unicode: An encoding that incorporates all the symbols in the world. Each symbol is given a unique encoding that uses Unicode without garbled problems.

Disadvantage of Unicode: Unicode is just a set of symbols that specifies only the binary code of the symbol, but does not specify how the binary code should be stored: Unicode and ASCII cannot be distinguished: the computer cannot distinguish between three bytes representing a symbol or three symbols respectively.

(3) UTF-8 :

UTF-8 is the most widely used form of Unicode implementation on the Internet.

UTF-8 is a variable-length coding method. It can use 1-6 bytes to represent a symbol, varying the length of the byte depending on the symbol.

Coding rules for UTF-8:

For single-byte UTF-8 encoding, the highest bit of the byte is 0, and the remaining 7 bits are used to encode characters (equivalent to ASCII code).

For multi-byte UTF-8 encoding, if the encoding contains n bytes, the first n bits are 1, the first byte of the n+1 bit is 0, and the remainder of the byte is used to encode the characters. All bytes after the first byte are the highest two bits "10" and the remaining 6 bits are used to encode the characters.

5 , Boolean type: Boolean

Boolean types are suitable for logical operations and are generally used for program flow control

If condition control statement

While loop control statement

Do-while loop control statement;

For loop control statements

The Boolean type data only allows values of true and false, no null.

Do not replace false and true with an integer of 0 or not 0, which differs from the C language

Case

Variables: base data type (8) vs Reference data type (class, interface, array)

The format of variable definitions in 1.java: Data type variable name = initialization value

class testveriable {

publicstaticvoid Main (string[] args) {

2. Variables must be defined first and then used

int myInt1 = 10;

double d = 12.3;

System.out.println (MYINT1);

SYSTEM.OUT.PRINTLN (myInt1 + D);

I1 is beyond the scope of its function and cannot be used.

System.out.println (I1);

3. Integral type: Byte ( -128~+127) short int (default type) long

byte b1 = 12;

byte b2 = 128;

Short S1 = 128;

int i1 = 12;

Defines a long variable with "L" or "L" at the end of the value

long l1 = 2134123351345325L;

System.out.println (L1);

4. Floating point type (numeric with decimal point): Float double (default type)

double D1 = 12.3;

Declare float data of type floats, with "F" or "F" at the end

float f1 = 12.3F;

System.out.println (F1);

5. Character type (= two bytes): Char can only represent one character (English, Chinese, punctuation, Japanese 、。。。 )

char c1 = ' a ';

char c2 = ' AB ';

String str = "AB";

Char c3 = ' Medium ';

String str1 = "China";

can represent escape characters

char c4 = ' \ t ';

char c5 = ' \ n ';

SYSTEM.OUT.PRINTLN ("ABC" + c5 + "def");

Know

char c6 = ' \u1234 ';

System.out.println (C6);

6. Boolean type: Boolean can only take a value of true or false. Cannot take value null

boolean bool1 = true;

if (BOOL1) {

System.out.println ("Today is Friday");

}Else{

System.out.println ("Today is Saturday!") ");

}

}

publicvoid method1 () {

int i1 = 10;

System.out.println (I1);

}

}

Still school let you 0 basic learning Java without worry, take you into it this magical world!

0 Basic Learning Java: variables (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.