Java Notes (ii)

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

(data)

Is the core of the system, the purpose of our programming is to manipulate the data (increase, delete, change, check)

How do you define and store data in your code? You need to pass variables or constants and data types

Note: Java is a strongly typed language, which means that any data needs to have a type.
JavaScript, weakly typed programming language, no need to declare data types when defining variables

The Java language is divided into the basic type and the object type two kinds of data types (countless kinds)
1) Basic type (8 kinds):
Name capacity Range Default value
Byte byte type is 1 bytes =8bit-128~1270
Short shorter integer 2 bytes =16bit-32768~327670
int integer 4-byte =32bit-2147483648~21474836470
Long integer 8 bytes =64bit-2^63 ~ 2^63-10
Float single-precision floating-point number 4 bytes =32bit-3.4e+38 ~ 3.4e+380.0
Double dual precision floating-point number 8 bytes =64bit-1.797e+308 ~ 1.797e+3080.0
Char character type 2 bytes =16bit ' A ', ' B ', ' You ' \u0000
Boolean Boolean type 1 bytes =8bittrue/falsefalse

Note: All the numbers in 1.JAVA are signed
2. The character type and the string are different, the character type uses the single quotation mark, the string uses the double quotation mark
char c = ' you ';
String s = "Study hard, day up!"
3.java, you write an integer, the default is int type, write a decimal, the default is double type.
Float F = 3.4;//compilation error, 3.4 default is double type, cannot be assigned to a variable of type float

method of viewing Unicode encoding of Chinese characters in the 4.cmd command line
A. Open the command line
B. Enter NATIVE2ASCII carriage return
C. Enter the Chinese characters you want to query to get the corresponding Unicode

CTRL + C closes the current conversion process


2) Object type
A. Built-in object type (API)
Scanner,math,random,string ...

B. Custom object types
All of the classes we wrote when we programmed were custom object types
-------------------------------------------------------------------
Special symbols:
\ t tab indicates the distance between a tab
\ nyou Wrap to the beginning of the next line
\ r Enter the starting position of the current line
\b is equivalent to backspace on the keyboard
\ \ counter Slash
\ "Double quotation marks
\ ' Single quotation mark
-------------------------------------------------------------------

Variable

Grammar
Data type variable name [= initial value of the given variable];

Note:
The variable name and the class name are all made up of numbers, letters, _,$, and cannot be the beginning of a number
Variable names can be Chinese characters, but not recommended because garbled problems may occur
Variable names are as small as possible, with the first letter of each word capitalized (except constants)
such as: int num = 1;
int inputint = Sc.nextint ();

|. can be initialized directly at the time of declaration (assigning a value to a variable for the first time)
such as: int i = 100;
Double d = 3.14;
String str = "Hello!";

||. Declare the variable before use, initialize the
such as: int i;
.....
i = 100;

Literal: Refers to the data itself, it also belongs to the variable
"Hello!", ' A ', 3.14,100

Note: All programs are made up of variables!


How to assign values to variables:
A. Literal assignment
int i = 3;

B. Method assignment
public int Add (int i,int j) {
return i + j;
}

int i = Add (3,4);

C. Assignment of an expression
Expression: A valid statement made up of variables and operators
int i = Sc.nextint ();

int a = 1, b = 2;
int i = a + b;

----------------------------------------------------------
Constant

Add a keyword on the basis of the variable-final

Constants must be initialized, the value of which cannot be changed

Such as:
Final int row_each_page = 10;//defines a constant

Naming conventions for constants: using uppercase letters and separating them with underscores

Note: Constants carry more information than literals, which improves the readability of the Code
----------------------------------------------------------

Data type conversions:
1. Automatic type conversion (implicit type conversion)
Data types with small precision can be automatically converted into data types with high precision

Such as: long, int, short, byte
CHAR-int
boolean-int//error

2. Forcing type conversions
It is possible to lose precision when converting from a data type of high precision to a data type with small precision.
The compiler will make an error and must take the cast
Such as:
1) Add (type) before the value of the desired conversion
Note: The type here can be a basic type, or it can be an object type
int i = 220900;
char C = (char) i;

float f = (float) 3.14;

2) Only long,double,float can convert this
Add l/l,d/d,f/f after the value you want to convert
float F = 3.14F;

Attention:
In Java, when two data types are operating, the result is biased to the data type
The bigger side.

----------------------------------------------------------

1byte = 8bit
1 k = 1024byte
1MB = 1024K
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
1EB = 1024PB
1ZB = 1024EB
1YB = 1024ZB
1BB = 1024YB
1NB = 1024BB
1DB = 1024NB

NOTE: Mobile phone traffic is calculated as a limit of 1000
----------------------------------------------------------

In-process
Binary: only 0 and 1 are composed, each encounter 2 into 1

Binary decimal
01011*2^2 + 0*2^1 + 1*2^0 = 5
10101*2^3 + 1*2^1 = 10;
111115

Binary goto Decimal
First, from right to left, starting with 0, then adding the corresponding position number multiplied by the number of tokens in the binary

Decimal Turn binary
Divide by 2 consecutively and then rewind the remainder

Small exercise:
0010 0111

110010-50
-------------------------------------------------------------

0011--->56
0001 1100---> 28
0000 1110---> 14
0000 0111---> 7

Displacement operations-the most efficient of all operations
In binary, move one bit to the right, which is equivalent to dividing by 2, moving one bit to the left, or multiplying by 2


Octal: Made up of 0~7, every 8 in 1
0320*8^2 + 3*8^1 + 2*8^0 = 26

Hex: Consists of 0~9 and a~f, starting with 0x
int i = 0x0029;
2*16^1 + 9*16^0 = 41

Small exercise:
Find out the 0x0419 binary?
0x0419
Ask for decimal: 4*16^2 + 1*16^1 + 9*16^0 = 1049
Binary: 0100 0001 1001


System built-in conversion method (decimal to the corresponding string):
tobinarystring (int i) converts a binary string representation

tohexstring (int i) converts a hexadecimal string representation

tooctalstring (int i) converts octal string representation
------------------------------------------------------------------

Operator

|. Arithmetic operators
+1. Mathematical operations 2. Connection of strings
-Subtraction (minus sign)
* Multiplication
/Division 100/3 = 33100/3.0 = 33.33333333
% Residual 100%3 = 1

= denotes assignment int i = 3; Assign 3 to variable I of type int
= = equals return type: Boolean

+=-=*=/=%=
Such as:
b = B + 1; --B + = 1;//later this type of writing avoids the conversion problem

+ + self-increment
--Self-reduction
The front + + indicates that + + is written in front of the variable, and the value of the expression and the variable itself is added 1 (increment first, then assign value)
After + + indicates that + + is written at the back of the variable, the value of the expression is unchanged, the variable is added 1 (first assignment, then self-increment)

Displacement operations: Performance is the highest
<< left shift with symbols
>> Right Shift with symbols
>>> Right shift without symbols

Note: No left shift without symbol (<<<)

<<=>>=>>>=

Interview questions:
Would you please get 4 of the 3 times in the fastest and highest-performing way? 4<<3


An arithmetic operator is a binary operator that requires two parameters to participate in a
For arithmetic operators, operands can only be numeric types (integer, floating-point)
Special exceptions: Strings can be used with + operations (stitching)

Low precision and high precision when doing operations, the result is high precision
The same precision data, the result is its own data type


||. Comparison operator (return Boolean type)
< less than
> Greater than
<= less than or equal to
>= greater than or equal to
! = does not equal

|||. logical operators

&& Short Circuit logic and true, false as false, as long as there is a false, the result is false

|| The logic or the truth is true, the same false as false, as long as there is a true, the result is true

! Logical non-representation inversion

Such as:
Boolean B1 = true;
Boolean b2 = false;
Boolean B = B1 && B2;//false
b = B1 | | B2;//true
b =!b1;//false

Attention:
& 1. Non-short circuit logic and (generally not)
2. Bitwise AND

| 1. Non-short-circuit logic or (not normally)
2. Bitwise OR

The difference between short-circuit and non-short-circuit logical operators:

Short-circuiting operator, if the result of the left side of the operator can determine the result of the entire equation,
Then the right part will not be executed.
So, when we write code, we need to think about which conditions to put on the shipping
Operator Front
With logic and when, put the two conditions may be false probability of a larger one put in front
With logic or when, put the two conditions in the probability of a greater likelihood of a larger one put in front
This can improve the performance of the entire software.


||||. Bitwise operators
& Bitwise AND (non-short-circuit logic with) 0 is 0, the same 1 is 1, the difference is 0
| bitwise OR (non-short circuit logic or) the same 0 is 0, the same 1 is 1, the difference is 1
^ Bitwise XOR or Same as 0, different 1
Formula:
A number of consecutive XOR or the same number two times, the result is that it itself
5 ^ 7 ^ 7 = 5

Such as:
5 binary: 0101
7 binary: 0111

0101
& 0111
------
0101--5 & 7 = 5

0101
| 0111
------
0111--5 | 7 = 7

0101
^ 0111
------
0010--5 ^ 7 = 2
^ 0111
------
0101

~ Bitwise REVERSE
0 Change to 0

Negative numbers, which are stored in the form of a complement in the computer

Complement = original code inversion + 1
~x =-(x + 1)

How does a negative binary get?
2 Binary 00000000 00000000 00000000 00000010
Bitwise inversion 11111111 11111111 11111111 11111101
Re +111111111 11111111 11111111 11111110
-2 binary 11111111 11111111 11111111 11111110

-9 binary:
00000000 00000000 00000000 00001001
11111111 11111111 11111111 11110110
11111111 11111111 11111111 11110111

-10
11111111 11111111 11111111 11110110

00000000 00000000 00000000 00001010
11111111 11111111 11111111 11110110

--------------------------------------------------------------

Trinocular operator? :
function: Condition judgment

Condition? expression_1:expression_2;
Condition? expression 1: Expression 2;

When the condition is true, the expression 1 is executed, and when the condition is not true, an expression 2 is executed.

Equivalent
if (condition) {
Expression 1;
}else{
Expression 2;
}

Java Notes (ii)

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.