Java Note 2-data type variables Java operator

Source: Internet
Author: User
Tags logical operators naming convention

We write software that is designed to efficiently operate (increase, delete, change, check) data.

Data type

1. Basic type (8 kinds)
byte byte type -128~127
Short shorter integer type -32768~32767
int integral type -2147483648~2147483647
Long integer type
Float single-precision floating-point number
Double dual-precision floating-point number
Boolean Boolean type True/false
char character type ' A ', ' I '

Attention:
A.java any write an integer by default is int type, any write a decimal number by default is double type
B.char character type with "' modifier, string with" "modifier,
Only one character can be written in a character type, and any number of characters may be written in a string


2. Object type
Built-in object type, scanner,string ...
Custom Object Types

--

Variable

How do I define a variable?

Data type variable name = value;

Attention:
The variable name must consist of a number, a letter, a _,$, but not the beginning of a number, it can be Chinese, but it is not recommended
The naming convention for variable names is the first letter lowercase, and the first letter of each word is capitalized.

--

Conversion of data types

Forcing type conversions
Conversion from a high-precision data type to a low-precision data type, where low-precision data types do not necessarily exist
data, so the accuracy may be lost.

No loss of precision
float f = (float) 3.14;//3.14

Loss Accuracy
int i = (int) 3.9;//3

Automatic (implicit) type conversion
Converting from low-precision data types to high-precision data types
Long L = 100;
Double d = 3;

Attention:
Float,double,long these three types of data have a simple forced type conversion method,
f/f,d/d,l/l

--

Java operators

+1. Math Plus
2. String connection

Note: If both sides of the + sign are numbers, it is an addition, and if a string appears on either side or both sides, it is the Join function


-Math minus

* Math Multiplication

/Math In addition

Attention:
The same data type is calculated in Java, the result data type is unchanged
9/4 = 2

Different data types in Java are computed, and the result data type is biased toward the larger side of the data type
9.0/4=2.25.
9/4.0=2.25.
9.0/4.0=2.25 ...

% take-over, modulus-finding

--

+=,-=,*=,/=,%=

int i = 3;
i = i + 1;==> i+=1;

--

++/--+/-minus (self-increment/auto-subtract)

Pre-gaga/post-gaga ++i/i++

Pre-decrement/post-decrement--i/i--

Conclusion:
Before + +, first operation, after assignment
After + +, first assignment, after operation

--

Java comparison operators

>,<,>=,<=,!=

= Assignment, int i = 3;//Assign 3 to variable i

= = equals, I = = 3;//Returns a Boolean type, determines whether I equals 3

--

Java logical operators

A && b logic with (and) is true, False is false, as long as one is false, the result is false

A | | b logic or (or) the truth is true, the same false as false, as long as one is true, the result is true

! A logical non-inverting


Note:&& and | | Also known as short-circuit logic and short-circuit logic or

Short-circuit logical operator, if the condition preceding the operator can determine the result of the entire expression, the second half is
Will not be executed, this will improve the efficiency of the program operation.

A && B, if A is false, then B does not execute
A | | b, if A is true, B does not execute

--

Java generates random numbers

Double r = math.random ();

The range of R is [0,1]


Modify Scope:
Data type variable name = (data type) (Math.random () * n + M)

--

Conditional judgment

Syntax One:
if (condition) {
Code
}
other code;


Syntax Two:
if (condition) {
Code 1;
}else{
Code 2;
}


Syntax Three:
if (condition 1) {

}else if (condition 2) {

}else if (condition 3) {

}else{

}


Attention:
A. The condition must be of type Boolean
B.if Else statements can be nested with each other

Java Note 2-data type variables Java operator

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.