The second day of Basic Java learning-introduction to basic data types

Source: Internet
Author: User

The second day of Basic Java learning-introduction to basic data types

Document version Development Tools Test Platform Project name Date Author Remarks
V1.0       2016.02.21 Lutianfei None
Chapter 2 Basic Java syntaxContent of this Chapter
Keyword identifier comment constant, hexadecimal conversion variable data type and type conversion operator statement (1) KEYWORDSKeyword Overview
Granted by Java Specific meaningKeyword features
Composed of keywords Lowercase lettersKeywords
gotoAnd constAs Reserved WordsYes, it is not used currently (it may be promoted to a keyword in the new JDK version)


  <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMyBpZD0 = "Two identifiers"> (2) identifierIdentifier Overview
It is the Character Sequence Composition Rule used to name classes, interfaces, methods, variables, etc.
Uppercase/lowercase letters and numbers $And _Notes
Cannot begin Start with a numberIt cannot be a keyword in Java that is case sensitive. 1. common naming rulesBasic principles: see naming conventions
Package(Actually, folders are used to solve the problem of the same class name, all in lower case)
Single Stage: miaolu, liuyi multilevel (separated by.): cn. ali Class or interface: Each wordOf Uppercase letters, Which is case sensitive.
One word: uppercase For example: Times multiple words: uppercase for each word. Eg: StudentAge, ShowAllAge Methods and variables
One word: all lower case eg: main, age multiple words: Start from the second word, each word is capitalized. Eg: studentAge, showAllAge Constant name. Basic Data TypeConstant name usage All uppercaseLetter, _Words are separated by underscores. Object constants can be mixed in size.
One word: uppercase eg: TIME Multiple words: uppercase letters, separated. Eg: SIZE_TYPE (3) annotationsOverview
Text used to explain the Instruction Program

Annotation classification format in Java

Single line comment
Format: // comments text multi-line comments
Format:/* Comment text */document comment
Format:/** comment text */The document comment is a java-specific comment, where the comment content can be parsed by javadoc, a tool provided by JDK, generate a set of instructions for the program in the form of web files.

Example:

// Standard comment/** requirement: Prepare a Java program and put HelloWorld !!! This statement is output in the console * Analysis: * A: To write A Java program, class * B must be defined: data can be output, indicating that our program can run independently, to run the program independently, you must define the main method * C: to output data to the console, you must use the output statement * Implementation: * A: the java language provides A keyword: class is used to define the class, followed by the format of the class name * B: main method is fixed: * public static void main (String [] args) {} * C: the output statement format is fixed: System. out. println ("HelloWorld"); * // This Is A HelloWorld case class HelloWorld {/* to allow the program to run independently, defining the main method as the main method is the entry of the program, automatically called by jvm */public static void main (String [] args) {System. out. println ("HelloWorld ");}}

 

(4) ConstantsConstant Overview
The value of the program cannot be changed during execution.

Constant classification in Java

Nominal value constant custom constant (Object-Oriented)

Nominal value constant Classification

String constants Double quotation marksContents
"Helloworld" integer constant all Integers
12, 23 decimal constant all decimal places
12.34, 56.78 character constant used Single quotesContents
'A', 'A', and '0' (must be only one character) Boolean constants are special and only TrueAnd FalseNull constant (array)

Note: by defaultDecimalProcessing.

Binary: 0b100 octal: 0100 decimal: 100 hexadecimal: 0x100 Source code, reverse code, and supplementary code Signed Data RepresentationIn the computer, Number of symbolsThere are three types of representation: source code, reverse code, and complement code. All data operations are performed using Complement. Original code
It is a binary fixed-point representation, that is, the highest bit is the symbol bit, "0" indicates positive, "1" indicates negative, and the remaining bit indicates the value size. Reverse code
A positive number is the same as its original code. A negative number is used to reverse the original code, Except symbol bit.

Complement

The positive value is the same as the original value. Add 1 to the last position of the anti-code..

Symbolic Data exercises

If the original code of a certain number of X is 10110100B, try to obtain the complement and reverse code of X. If you know the completion code of a certain number of X, try to obtain the original code. (Complement-> except for the original code symbol bit, the original code is directly reversed with 1)

 

(5) variablesVariable Overview
In the process of program execution, the value of a specific range can be changed. Understanding: Just like the Definition Format of unknown variables in Mathematics
Data Type Variable name= Initialization valueNote: The format is fixed. Remember the format, which should not be changed.

Java is a strongly typed language. A specific data type is defined for each type of data, and different memory sizes are allocated in total.

Data Type Default Value Storage Format Data range
Short 0 2 bytes -32,768 to 32767
Int 0 4 bytes -2,147,483,648 to 2,147,483,647
Byte 0 1 byte -128 to 127
Char /U0000 2 bytes Unicode character range
Long 0L or 0l 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Float 0.0F or 0.0f 4 bytes 32-bit IEEEE 754 single precision range
Double 0.0 or 0.0D (d) 8 bytes 64-bit IEEE 754 dual precision range
Boolean False 1 digit True (1) or false (0)


Note:

Integer default: intDecimal default: doubleLong Integer suffix LOr l mark. L is recommended. Single-precision floating point FOr f flag. F is recommended.

Notes for using variables:

Scope
Variables are defined in BracketsWhich of the braces is the scope of the variable. Two variables with the same name cannot be defined in the same scope. Initialization value
You cannot use it directly without an initialization value. You only need to give the value before using it. It is not necessarily given immediately when defining it. Default conversion of data typesGenerally, the data types involved in the calculation must be consistent. booleanType Cannot be convertedDefault conversion for other data types (small data types are converted to large data types before calculation)
Byte, short, char-> Int-> Long-> float-> double byte, short, and char are not converted to each other. Converted to int type before Calculation Forced conversion of data types Target type Variable name= ( Target type) (Converted data );
Eg: byte c = (byte) (a + B); // int a, byte B; Several minor issues in Data Types1. In the definition LongOr FloatAdd LOr F. 2, byte, shortWhen defined, they actually receive Int type valueIf the value exceeds the range Error. 3. For the byte type -1282-digit 10000000Here 1 is both Symbol bitAgain Numeric bit4. byte, short, char-> int-> long-> float-> double
Long 8 byte, float: 4 byte; Why is long converted to float?
1. The underlying storage structure is different. 2. The data range indicated by float is larger than that indicated by long.
Long: 2 ^ 63-1 float: 3.4*10 ^ 38> 2*10 ^ 38> 2*8 ^ 38 = 2 \ 2 ^ 3 ^ 38 = 2*2 ^ 114> 2 ^ 63

5. Characters in JavacharOneChinese charactersBecause the character variables in the java language occupyTwo bytes.

6. the Java language usesUnicode encoding.

Interview question 1Byte b1 = 3, b2 = 4;
B = b1 + b2; B = 3 + 4; which statement fails to be compiled? Why?
Because b1 and b2 are variables, the type problem is considered first when adding the variables, and the type problem is also considered when assigning values to the results. After b1 + b2, to ensure no overflow, it will be converted to the int type for storage by default.. Both 3 and 4 are constants. constants are added first to check whether the result exceeds the byte range. If no value is available, the value can be assigned normally. Interview question 2Byte B = 130; is there any problem? What can I do if I want to make the assignment correct? What is the result?
In this case, an error is reported because the byte range is-128 ~ + 127,130 overflows. Byte B = (byte) 130; no error is reported during compilation, and the output result is-126. Analysis process:
A: When 130 is converted to binary: 00000000 00000000 00000000 10000010 (this is the original code of 130, anti-code, and re-code) B: intercept the operation to byte: 10000010 (this result is A re-code) c: The display is in the form of the original code to the original code (except for the symbol bit, the reverse is + 1) 1111110 Exercise Question 1Evaluate the result printed on the console of byte B = (byte) 300;
According to the above analysis process: 300-> 0000000000000101100-> 00101100-> 44 Exercise Question 2: Write the following program resultsSystem. out. println ('A'); Result:

System. out. println ('A' + 1); Result: 97

System. out. println ("hello" + 'A' + 1 );

System. out. println ('A' + 1 + "hello"); System. out. println ("5 + 5 =" + 5 + 5 );

System. out. println (5 + 5 + "= 5 + 5 ");

Note:
1. String data and other data+The result is of the string type. However, pay attention to the operation sequence.

Answer:
Helloa1 98 hello 5 + 5 = 55 10 = 5 + 5 Supplement the float Storage Format

Float Type numbers are used in computers4 bytesStorage. FollowIEEE-754Standard Format:
* A floating point number consists of two parts:Base mAndIndex e

The base part uses the binary value to indicate that the actual value index of the floating point is occupied. 8bitBinary Number, which can indicate that the value range is 0-255

HoweverIndexPositive and NegativeTherefore, IEEE requires that the power calculated here must beMinus 127Is the real index.
So,Float indexes can range from-126 to 128.

The base part is actually a 24-bit value, but the highest bit is always 1. Therefore, the highest bit is not stored, and the storage occupies 23bit.

Scientific notation format:

S_EEE EEEE E_MMM MMMM
S indicates the base number of M of the binary data after the floating point plus the positive and negative E index plus 127.

Example: 17.625 storage in memory

First, we need to convert 17.625 to binary: 10001.101 shifted 10001.101 to the right, until there is only one digit before the decimal point: 1.0001101*2 ^ 4 because the four digits are shifted to the right: because the decimal point must be 1, IEEE requires that only the decimal point should be recorded. Therefore, the base number here is: 0001101 index: Actually 4, must be added with 127 (when the transfer, minus 127), so it is 131. That is, 10000011
The symbol is an integer, so it is 0.
To sum up, the storage format of 17.625 in memory is:
01000001 10001101 00000000 00000000

Related Article

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.