Dark Horse programmer----Key words, identifiers, variables, data types and related questions of Java Foundation

Source: Internet
Author: User
Tags binary to decimal decimal to binary

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------


1: Keywords (master)
(1) Words that are given a specific meaning by the Java language
(2) Features:
All lowercase. such as: class, Static, etc.
(3) Precautions:
A:goto and const exist as reserved words.
B: High-level notebooks like notepad++ have special color tags on keywords


2: identifier (master)
(1) is to give classes, interfaces, methods, variables, such as the name of the sequence of characters, required to see the name of the known meaning.
(2) Composition rules:
A: English uppercase and lowercase letters
B: Digital
C:$ and _
(3) Precautions:
A: Cannot start with a number
B: cannot be a keyword in Java
C: Case Sensitive
(4) Common naming conventions (see Name and meaning)
A: Package All lowercase
Single-level package: lowercase
Example: itheima.com
Multi-level package: lowercase, and separated by.
Example: Cn.itcast,com.baidu
B: Class or interface
One word: Capitalize the first letter
Example: Student,demo
Multiple words: Capitalize the first letter of each word
Example: Helloworld,studentname
C: Method or Variable
One word: First letter lowercase
Example: Name,main
Multiple words: Start with the second word, capitalize the first letter of each word
Example: Studentage,showallnames ()
D: constant
All caps
One word: Uppercase
Example: PI
Multiple words: Uppercase and separated by _
Example: Student_max_age


3: note (master)
(1) The text that explains the program
(2) Classification:
A: single-line comment //
B: Multi-line Comment /**/
C: Documentation Comments (later)/** */
(3) wrote a annotated version of the HelloWorld case.
We're going to write a procedure in the back.
Demand:
Analysis:
Realize:
The code reflects:
(4) The role of annotations
A: Explain the procedure, improve the reading of the code.
B: Can help us debug the program.
Later, we will explain a more advanced debugging tool


4: Constant (Master)
(1) The amount of the value does not change during program execution
(2) Classification:
A: Literal constants
B: Custom Constants (later)
(3) Literal constants
A: string constant "Hello"
B: Integer Constants 12,23
C: Decimal constant 12.345
D: Character Constants ' A ', ' a ', ' 0 '
E: Boolean Constants True,false
F: Empty constant Null (said later)
(4) provides four representations of integer constants in Java
A: Binary Made up of 0, 1. Start with 0b.
B: Octal by 0, 1, ... 7 composition. Start with 0.
C: Decimal by 0, 1, ... 9 composition. Integers are decimal by default.
D: Hex by 0, 1, ... 9,a,b,c,d,e,f (both uppercase and lowercase) are composed. Start with 0x.


5: Binary conversion (Learn)
(1) Other binary to decimal
Coefficient: is the value on each bit
Cardinality: X-binary cardinality is X
Right: The data on each digit, from the right and starting from 0, the corresponding number is the right to the data.
Results: The sum of the coefficients * cardinality ^ power power.
Examples:
Binary to decimal: 0b10101=1*2^4+0*2^3+1*2^2+0+1*2^0=16+4+1=21
Octal decimal: 0123=3*8^0+2*8^1+1*8^2=83.


(2) Decimal to other binary
In addition to the base fetch, until the quotient is 0, the remainder is reversed:
Decimal to binary system in addition to 2, turn 8 into the system in addition to 8 to take the remainder, 16, except 16 until the quotient is zero.
Example: 20 converted into binary: 10100
20/2=10 0
10/2=5 0
5/2=2 1
2/2=1 0
1/2=0 1
(3) Fast conversion method of the binary conversion
A: Conversion between decimal and binary
8421 yards.
B: Binary to octal, hexadecimal conversion:
The first method: Use decimal as the bridge.
The second method: Split combinations, two to eight, 3-bit combinations, and two to 74-bit combinations.


6: Variable (master)
(1) The amount of the value that can change within a certain range during the execution of a program
A: it must be qualified. The data type is used to qualify.
B: The operation can not directly take the space to operate, the real operation is to use the value of the space, we give this space a representative name (variable name)
C: Even if there is a data type, there is a variable name, but the space is empty, there is no meaning, it needs to be assigned value. (Initialize value)
(2) Define the format of the variable:
A: Data type variable name = initialization value;
B: Data type variable name;
Variable name = initialization value;
(3) Issues to be aware of when using variables
A: Scope:
The variable is defined in the curly brace, it is valid within the curly brace, and a variable with the same name cannot be defined within the same curly brace.
B: Initialize Value
Variables that do not have an initialized value cannot be used directly, and the variable is assigned when it is recommended to define the variable.
C: It is recommended to define only one variable on the same line.


7: Data type (master)
(1) Java is a strongly typed language that provides the corresponding data types for each type of data.
(2) Classification:
A: Basic Data type: 4 classes of 8
B: Reference data type: Class, interface, array.
(3) Basic data types
A: Integer Number of bytes occupiedRange
Byte 1-128--127
Short 2-2^15--2^15-1
Int4-2^31--2^31-1
Long 8-2^63--2^63-1
B: Floating point
Float 4-3.403e38--3.403e38
Double 8 -1.798e308--1.798e308
C: Character
Char 2
D: Boolean
Boolean 1

Attention:
integers are type int by default, and floating-point numbers are double by default.

Long integers to add l or L, it is recommended to use L. Long j=100000000l
Single-precision floating-point number to add F or F, it is recommended to use F. Float h=12.345f


8: Data type conversion (mastering)
(1) Boolean type does not participate in conversions
(2) Default conversion
A: From small to large
B:byte,short,char--INT--long--float--double
C:byte,short,char do not convert each other, directly into the int type participates in the operation.
(3) Forced conversion
A: from big to small
B: There may be a loss of precision, generally not recommended for such use.
C: Format:
Target data type variable name = (target data type) (converted data);
Example: Byte a=3;    int b=4; int C=a+b (default conversion)
byte c= (Byte) (a+b) cast
9: Study Questions and interview questions:
(1) Are there any differences between the following two ways?
float f1 = 12.345f;
float F2 = (float) 12.345;
A: F1 is actually converted by a double type.
The F2 itself is a type of float. The second method is usually used.
(2) Is there a problem with the program below, and if so, where?
BYTE B1 = 3, b2 = 4, B;
B = B1 + b2;
b = 3 + 4;
A: There is a problem with the B=B1+B2, because the variables are added, the types are looked at first, and the results are evaluated when they are finally assigned. The result of the b1+b2 above is the int type. So it goes wrong.
and B = 3 + 4 is the constant addition, the first will do the addition, and then see whether the result is in the range of the assigned data type, if not, will be an error.
(3) What is the result of the following operation?
byte B = (byte) 130;
A; Because the range of byte is 128 to 127. And 130 is not within this range, so it will be an error. We can use forced type conversions. byte b= (byte) 130;
How much is the result?
Analysis Process: If we want to know what the result is, we should know how to calculate it. The computation of the data in the computer is in the complement. To get the complement, we first calculate the binary of the data.
A: Get 130 binary for this data
00000000 00000000 00000000 100000010
This is 130 of the original code, because it is a positive number, so it is also the anti-code, complement.
B: Because byte has only one byte, it needs to be intercepted.
100000010
The result is a complement that is truncated to byte.
C: Known complement, requires the original code
Sign bit value bit
Complement 1 0000010
Anti-code 1 0000001 (up borrow minus 1)
Original Code 1 1111110
The binary is converted to decimal by the original code 126, and because the first is 1, so is-126.


(4) Character participation operation
The addition of a character to an integer results in an integer
is to find the value inside the ASCII
A 97
A 65
' 0 ' 48

System.out.println (' a ');//a
System.out.println (' a ' + 1);//97+1=98


(5) String participation operation
String data and other data do +, the result is a string type, where the + is actually a string of connectors.

System.out.println ("Hello" + ' a ' + 1);//helloa1
System.out.println (' A ' +1+ "Hello");//98hello
System.out.println ("5+5=" +5+5);//5+5=55
System.out.println (5+5+ "=5+5");//10=5+5




Second: Supplementary knowledge points in the data type
1: When defining a long or float type variable, add either L or F.
integers are type int by default, and floating-point numbers are double by default.

Byte,short at the time of definition, they are actually receiving a value of type int.
This is self-made a data detection, if they are no longer within the scope of the error.

Problem with 2:byte value
BYTE B1 = 127;
byte b2 = (byte) 128; -128 128-(-128) =256
byte B3 = (byte) 129; -127 129-(-127) =256
byte b4 = (byte) 130; -126 130-(-126) =256
Byte ..... (byte) 256; 0 256-0=256
Byte ..... (byte) 300; 44 300-44=256
Equivalent to the data with strong turn-256;

Range of Byte: 128 ~ 127

128:10 million
-128:10000000 (1 Here is the sign bit, also the value bit)

3: Default conversion of data type conversions
Byte,short,char--INT--long--float--double

Long:8 bytes
Float:4 bytes

A: Their underlying storage structure is different.
The range of data represented by B:float is larger than the range of 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-1


4:java character Char in a language can store a Chinese character? Why?
OK. Because characters in the Java language occupy two of bytes.

The Java language uses Unicode encoding.


------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Dark Horse Programmer----Key words, identifiers, variables, data types, and related questions of Java Foundation

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.