2.java Learning _ Basic knowledge (identifiers, constants, variables, operators)

Source: Internet
Author: User
Tags arithmetic arithmetic operators binary to decimal bitwise bitwise operators logical operators modulus java keywords

Identifier definition

Some of the names that we customize in the program
In the process of programming, it is often necessary to define some symbols in the program to mark some names,
These symbols are called identifiers, such as package name, class name, method name, parameter name, and variable name.

constituent elements

English uppercase and lowercase letters

Digital 0~9

Underline _ and dollar sign $

Rules


Can not begin with a number, such as: 4student, X
Java keywords cannot be used, such as class cannot be used
Strictly case-sensitive, such as XXX and XXX, are two variables

Principle

See the name of the known, hump named

Specification

Master
Package Name
When multiple words are composed, all the words are lowercase.
Example: xxxyyyzzz
Class name and Interface name
When multiple words are composed, the first letter of all words is capitalized.
Example: xxxyyyzzz
Variable name and function name

When more than one word is composed, the first letter of a word is lowercase,
The first letter of the following words is capitalized. Example: xxxyyyzzz
Constant name
All letters are capitalized, and each word is connected with an underscore when you have multiple words.
Example: xxx_yyy_zzz
A total of 64, this is a piece of poker ah
Key words
Keywords: words that are given a specific meaning by the Java language.
Features: The letters that make up the key words are all lowercase.
Attention:
A:goto and const are reserved words
B: Advanced notebooks like notepad++ and editplus have special color tags for keywords.

Constant

Defined
Fixed numeric value in Java.
Classification
Literal constants
Integer constants
Binary binary: Begin with 0b
Made up of 0, 1. Start with 0b. New features after JDK7.
Octal octal: Begin with 0
Made up of 0-7. Start with 0.
Decimal decimals
Made up of 0-9. The default is decimal.
Hex Hex: Begin with 0x
Consists of 0-9,a-f (case insensitive), starting with 0x.
Decimal constants
Single-precision floating-point number float: End with F
Double-precision floating-point number double: End with D or omit
String constants
Identifies one or more characters in double quotation marks ("") to be declared,
"" String for empty content, which is a string constant of length 0
Character constants
Use single quotation marks (') for a number, letter, or symbol, Chinese character
Identity declarations, and special characters represented by escape sequences.
Only a single character ' a '; ' 4 '; ' + '; ' i '; ' \ R '; ' \u0000 ';
' \u0000 ' denotes a whitespace character, which is a single quotation mark without any characters.
A char is two bytes in size,
And a Chinese is also two bytes.
Boolean Constants
More unique, only two values: TRUE | False, is the Java keyword
Null constants
Only one value is NULL, which indicates that the reference to the object is empty.
Custom Constants
Precautions
Constants can be output directly.
Escape character
Defined
In a character constant, a backslash \ is a special character, called an escape character.
Role
Used to escape one of the following characters
Common escape characters
\ r: Represents a carriage return, positions the cursor at the beginning of the current line, and does not jump to the next line return
\ n: Represents a newline character, and the cursor wraps to the next line, beginning next
\ t: Represents a tab and moves the cursor to the next tab tab position
\b: Represents a backspace symbol, just like the backspace key on a keyboard
Escape Special Symbols
\ ': denotes single quote character
\ ": represents the double quote character
\ \: Indicates backslash character
Binary features and conversions
Origin of the binary system
Law
The larger the system, the shorter the representation
Classification
Binary
A sequence of numbers consisting of 0 and 1, preceded by 0b or 0B.
Octal
A sequence of numbers beginning with 0 and followed by integers of the 0~7 range
Decimal
A sequence of numbers consisting of integers of the number 0~9 range
Hexadecimal
A sequence of numbers beginning with 0x or 0X and subsequently composed of 0~9, a~f
Conversion between binary and binary
Other binary to Decimal
Conclusion: the coefficients * (the power of the radix) can be added.
Coefficient: is the data on each one.
Cardinality: X-Binary, cardinality is x.
Right: On the right, numbering starts at 0, and the number on the corresponding bit is the right of that bit.
Decimal to another binary
In addition to the base fetch, the remainder is reversed until the quotient is 0.
Fast Conversion
8421 yards: Binary and decimal transfers
Binary--Eight binary (3-bit combination)
Binary--16 binary (4-bit combination)
Split Group Legal
Variable
Defined
is to store uncertain data, that is, to open up a space in memory.
The opening up of memory space is done by explicit data types, variable names, numeric values.
Data type variable name = value;
Characteristics
1. One storage area in memory
2. The region has its own name (variable) and type (data type)
3. Data (values) of the region can be continuously transformed within the same type range
Role
Used to store constant constants of the same type and can be reused.
Precautions

1. Scope of the variable (scope):

Valid between a pair of {}
Also, a variable with the same name cannot be defined within the same curly brace.
2. Initialize values:
Special attention:
Be sure to initialize the variable before using it.
You must have an initial value to participate in the operation without a default initialization value.
A variable without an initialized value cannot be used directly.
You just have to give the value before you use it, and you don't have to give the value immediately when you define it.
It is recommended to give values at the time of definition.
3. Recommend defining only one variable on a single line
Multiple can be defined, but not recommended

Define multiple variables on a single line
int a = 10; int B = 20; int c = 30;
The above notation is possible, but not recommended.
int a = 10;
int B = 20;
int c = 30;
The following definitions allow:
int x=3,y;
y=x+3;
int x=y=z=5;//This is wrong.
int x=y=z;//This is wrong.

int x, y, Z;
x=y=z=5;
Defining formats

A: Data type variable name = initialization value;
Initialize directly when declaring a variable
B: Data type variable name;
Variable name = initialization value;
Declare the variable before you use the variable to complete the initialization
Note the problem of defining variables:

When to define a variable:
When you know that this is a change in the content of the time.
Scope of the general definition:
The first is in a small area, if not reasonable, in the consideration of a wide range ...
The data type of the variable
Basic data types
Four categories of 8
Numeric type
Integer type
BYTE 1 byte-128 ( -2^7) ~127 (2^7-1)
Short 2 byte-32768 ( -2^15) ~32767 (2^15-1)
int 4 bytes (default type)
Long 8 bytes
End With L
Original code
Anti-code
Complement
2, binary decimal negative number with sign bit 1
I believe you already know how to turn the law, but here is the explanation.
Complement 1111-1111
① remove the sign bit minus 1; anti-code 1111-1110
② Remove the sign bit, the bitwise negation; The result is a negative source; original Code 1000-0001
③ the original code into the corresponding decimal. Decimal:-1
Floating-point types
Float 4 bytes
End With F
float f=2.3f;
(default) Double 8 bytes
Base e is 10
Character Char
Character Char occupies two bytes in memory, range of values 0~65535
Boolean-Type Boolean
Only two values True|false, in Java operations, do not participate in the type conversion, can be considered a byte
Reference data type
Classes Class
Interface interface
Array []
Attention

Integers by default are of type int
Floating-point numbers are the double type by default.

Long integer suffixes are marked with L or L. It is recommended to use L.
Single-precision floating-point numbers are marked with F or F. It is recommended to use F.
Data type conversions

Automatic data type Promotion
(Implicit conversion)

Two types of compatibility
or the target type value is greater than the source type

byte B = 6;
int i =b;
Data type coercion
(cast)
Two types of incompatible
or the target type value is less than the source type
Show declared strong-turn format: target type variable name = (target type) value;
Note: You can also promote automatically when using the assignment operator (= except), no need to display a declaration
Do not arbitrarily use the cast because it implies a loss of precision.
Precautions
Range of values
(small → Large)
Byte,short,char < int < Long < float < double
Byte,short,char do not convert to each other, but their participation in the operation is first converted to the INT type
The Boolean type does not participate in the conversion.
Char can be assigned to int--long--float--double
A numeric overflow must declare a strong turn
If the expression to the right of the assignment operator is a literal constant operation,
Then the first operation, if the value range of the left definition variable is exceeded
Then the need for a strong turn, if not exceeded then can receive. No error.
Example: Byte b4 = 3 + 4; Compiled by.

If the expression on the right side of the equal sign has an int variable to participate in the operation (because of automatic elevation), it still needs to be forced type conversion (as long as a variable participates, it will be automatically promoted)
Example:
int i = 3;
byte B = i + 4;//Compile error,
byte B = (byte) (i + 4);
Case
Read more
Study Questions

Study Questions 1: Excuse me, is there a problem here?
Double d = 12.345;
float F = d;
--------------------------
To assign a double to float, you need to force the type conversion
Double d = 12.345;
float f = (float) D;

Study Questions 2: Look at the following two definitions there is no difference?
Float F1 = (float) 12.345;
float F2 = 12.345f;
————————————————————
F1 is actually converted by a double type.
The F2 itself is a type of float.
byte B = 130; Is there a problem? If I want to make the assignment right,
What can be done? How much is the result? -126
Exercise: Byte B = (byte) 300;
The arithmetic of the data in the computer is in the complement.
A: Get 130 binary for this data.
0000-0000 0000-0000 0000-0000 1000-0010
This is 130 of the original code, is also anti-code, or complement.
B: Intercept operation, cut into byte type.
10000010
The result is a complement.
C: Known complement to seek the original code.
Sign bit value bit
Complement: 1 000-0010

Anti-code: 1 000-0001

Original code: 1 111-1110
1 1 1-1 1 1 0
64 32 16 8 4 2 0 = 126
See how the program writes results
By adding a character and an integer, we give a table: the ASCII code table.
After reading this table, we have to remember three values:
' A ' 97
' A ' 65
' 0 ' 48
Direct output of one character
System.out.println (' a '); A
Output a character and an integer to do the addition
System.out.println (' a ' + 1); 98
String data and other data do +, and the result is a string type.
The + is not an addition operation, but a string connector.


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
Attention:
If the string is at the front, then the + sign is the connector;
If the preceding is an expression that can be operated on,
Then the + number is the arithmetic operator, the first operation and then the connection
Attention:
The upward transformation with the class (auto lift),
Downward Transformation (CAST)
to differentiate
Operator
Master
Arithmetic operators
11: + Plus,-minus, + add,-minus, * multiply,
/except: dividing integers, resulting in integers, ignoring fractional parts (not rounded)
If a decimal is involved in the operation, the result is a decimal
% modulus (that is, redundancy): The sign is only related to the left by the modulus
+ + self-increment (before and after): the first increment after the operation, after the first operation after the self-increment
--Self-reduction (before and after): Before the first self-reduction operation, after the first operation after the self-reduction

+ String Addition:
(Is it related to the order of the string???
Verified to have a relationship, his priority only works on String + data behind it)
String data and any data using + are connected and eventually become strings.
System.out.println ("5+5=" + (5+5)); The result is "5+5=10"
System.out.println ("5+5=" +5+5); The result is "5+5=55"
/: Divide integers, the result can only be integers.
If you want a decimal, then the one that participates in the operation
The data must have a floating-point number type.
%: Obtained remainder
The data on the left is greater than the right and the result is the remainder.
The data on the left is less than the right and the result is left.
The data on the left equals to the right and the result is 0.
The symbol is consistent with the left.
+ +,--:
Single use: Consistent effect, implied forced type conversion function ""
Participate in the operation using: inconsistent,
Before, would first ++/--, after, was after ++/--

Molefe said a little bit: about this
Post + + has a relationship with whether to introduce a second variable:
No, print input, is not self-increment value;
Yes, printout, is self-increment value
Assignment operators
6: = assignment, + = plus equals, = minus equals, *= multiply equals,/= equals,%= modulo equals
int x=y=z=5;//This is wrong.
int x=y=z;//This is wrong.
__________________
int x, y, Z;
x=y=z=5;
+ = This operator, which contains the coercion type conversion function.
Short S = 2;
s+=2;
Equivalent to
s = (short) (s+2);
Comparison operators
The result of the operation is a Boolean type
6: = = equals,! = Not equal to,< less than,> greater than, <= less than equals, >= greater than or equal to
Feature: The result of a relational operator is a Boolean type.
Please be careful: = = do not write =
logical operators
Operation of data with Boolean type result, still Boolean
6:& with, | or,! Non, ^ xor or,&& short circuit with, | | Short Circuit or
&: False if False
|: True is true
!:true Change False,false to True
^: same false, different true
______________________
&&: False if False
| |: True if True
____________________
The difference between && and & is: If there is false on the left, the right side will no longer execute.
|| The difference between | and | is: If there is true on the left, the right side will no longer execute.
Commonly used in development:
&&, | |,! (s+3);
Bitwise operators

Symbols for each operation of the binary number
7:& Bitwise AND, | bitwise OR, ~ reversed, ^ bitwise XOR,
<< left shift,>> right shift,>>> unsigned right shift
<< left Shift
The original value is multiplied by the number of bits of the 2 move, and the value shifts to the left.
The left side is abandoned, and the part on the right is 0.
>> Right Shift
The original value is divided by the number of bits moved by 2, and the value right shifts to a smaller
If the highest bit is 0, the left-hand space is filled with 0;
If the highest bit is 1, the left-hand-shifted bit is filled in 1
>>> Unsigned Right Shift
No matter what the high is, the right shift is 0.
^ Bitwise XOR OR
A number XOR or another number two times, and the result is that number
Move left or right,
The fact is to increase or decrease the data by 2 of the specified power.
Ternary operators
Trinocular operator

Format:
(conditional expression)? Expression 1: Expression 2;
Execution process
If the conditional expression returns true, the expression 1 is used as the result.
If the conditional expression returns false, the expression 2 is used as the result.
Nested three-dimensional expressions
int a = 20;
int B = 40;
int c = 30;

Mode 1:
int d = a>b?a:b;
int e = d>c?d:c;
Mode 2:
int f = (a>b)? (A>C?A:C):(b>c?b:c);
Attention:

The conditional expression indicates that the future can only be an expression with a result of true or false.
The operation of the ternary operator can be improved by using the IF statement, which is not true

When will it not be established?
When the statement body controlled by the IF statement is an output statement,
Will not be established. Because the ternary operator is an operator,
must require a result to be returned.
The output statement cannot be returned as a result.
Supplemental knowledge
An expression
Expression: A formula connected with an operator (variable, constant).
A+b Arithmetic expressions
5>4 Comparison expressions
Writing specifications
and precautions

Comparison operator: The result of the comparison operator is a Boolean type,
That is, either true or false.
==,!=,>,>=,<,<=
Separator:
A semicolon; represents the end of a statement. You can write multiple statements on one line,
However, it is recommended that you write only one statement on a line.
Space:
Must have a space before and after the keyword;
operator, it is optional at both ends of the operator. Suggested to have.
Attention:
Never put a place with equal numbers,
Write "= =", otherwise it may not be the result you want.
Precautions:
A = a + 20;
A + = 20;
The result is equivalent and the understanding is not equivalent.

The force type conversion function is included because the + = operator.
Like what:
Short S = 2;
s+=2;
Equivalent to
s = (short) (s+2);
Interview questions
Interview Question 1:
Write the Formula 2 times 8 with the highest efficiency.
2*8--2<<3
Interview Question 2:
Please exchange two of data.
Example:
int a = 10;
int B = 20;
Results:
A = 20;
b = 10;

Development uses third-party variables in the form of "mastering".
The interview uses the bit ^ operator.
Memory: Assumed variable is a, b
Left: A,b,a
to the right; A^b.

2.java Learning _ Basic knowledge (identifiers, constants, variables, operators)

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.