A variable
1. Concept: is a variable in the memory space, is the basic unit for storing data
2: data type, variable name, data
3. Define:
. A variable name declared data type; for example: int a;
. B = assignment data; for example: a = 4;
.. C otherwise: I and assignment statement: variable name = value data type; for example: int a = 4;
. Ii variable name data type, variable name = value, for example, the variable name: int a = 10, b = 20, c, d = 5;
public class testvar { public static void main (String [] args) {
// declare a variable
int A;
// variable assignment
A =. 4;
// while variable assignment statement
int. 3 = B;
// declare multiple variables , while the assignment
@ a statement three int type variables, no assignment where c, d and e are assigned
int c, d =. 5, e =. 6; } }
Second, the data type
1. The basic data types (short-answer type, original type)
a. integer type
b. floating point type (decimal)
c. Character Types
Keyword char
2B number of bytes
The assignment of char type variable manner: i char c = 'A'; quotes a single character
. Ii char c = 65; integer used to represent a character
. Iii char c = '\ u0041'; used to represent a character unicode
public class TestChar { public static void main (String [] args) { // single quotation marks a character char A = 'A'; // OK char B = 'I'; // OK char C = '65' ; // error !! // Note: because quotation marks can only write a single character, note the difference with the second statement of the way! ! // integer to represent a character char D = 65; // 65 here represents the character A // used to represent a character unicode char E = '\ u0041'; // NOTE: As used herein, single quotes, intermediate digital needs to start with \ u, it must be followed by four hexadecimal digits } }
Escape character:
\ T Horizontal tab
\ N newline
\' apostrophe
\" Double quotes
\\ backslash
d. Boolean keyword boolean
Boolean literals true and false only important: Java is a strongly typed language, declared data type and type of assignment must be consistent
2. The reference data types (object type, complex type)
in Java, remove eight basic types of data, other types of data are reference data types
, for example: String (String), arrays, interfaces, etc.
String (String) : in double quotation marks "" due to the content the string
string is a series or plurality of characters, but they are integrally string s = "hello";
public class the TestString { public static void main (String [] args) { // double quotation marks "" due to the content the string // single character enclosed in double quotes after it is no longer a char, but String String S1 = "I" ; // more than one character is the most common form of String String s2 = "I love you" ; // when using double quotes, 25105 no longer represents the "I" word, but simply the digital String s3 = "25105" ; // when using double quotes, no 1 + 1 results, but a simple text String S4 = "1 + 1" ; } }
Third, the data type conversion
1. automatic type lifting
a two variables are compatible with each other.
B assign variables to smaller ranges wider range of variables.
Rule: direct assignment
byte -> short -> int - > long -> float -> Double
char -> int -> Long -> float -> Double
Note: due to byte and short with a negative, while the number of char that there is no negative, it is not automatic type between them upgrade
public class TestAutoCast { public static void main (String [] args) { // assign variables to a lesser extent, a wider range of variable char A = 'I' ; int B = A; System.out.println (B) ; // 25105 int C = 123456 ; Double D = C; System.out.println (D); // 123,456.0 } }
2. casts
. A data between the two must be compatible with each other
b assign a larger range to a smaller range of variables variables
rules: variable name = target data type (data type target) data source variable name / Source data value;
casts can lead to changes in the data, which possibilities are as follows:
a conversion between integer and the integer: integer length matched to each other, direct assignment, data are complete.
B conversion between integer and integers. : integer length mismatch, data truncation, high discard
c conversion between integer and floating point: floating-point number to integer, data truncation, discarding decimals
d Boolean type does not participate in the conversion.
public class TestCast { public static void main (String [] args) { // conversion between integer and the integer: integer length match each other, direct assignment, data are complete int A = 127 ; byte B = ( byte ) A ; System.out.println (B); // 127 // converting between integer and the integer: integer length mismatch, data truncation, high discard int C = 129 ; byte D = ( byte ) C; the System.out .println (D); // -127 // conversion between integer and floating point: floating-point number to integer, data truncation, discarding decimals Double E = 12.5; int f = (int) e; System.out.println(f);//12 } }
Fourth, the expression
1. Concept: expression of variables, constants, data, consisting of operators, usually produces a result
2. When the mathematical operation of two different types of basic data types, data type rules are as follows:
a. when the double expression have found to be double
. when B is not double expression, but when the float, the float results
C. when no expression float and double, but a long time, the results of to Long
. D when the expression is not the above three cases, the result is the default type int int -> long -> float - > double order of
Fifth, operators
1. Arithmetic Operators
Including plus +, subtraction -, * multiplication, addition / I (mold)%
2. assignment operator
Included Included =, + =, - =, * =, / =,% =
3. The logical operators
&& result of short-circuiting two expressions are true, the result is true, if the result is false false
&-circuit with the results of the two expressions are true, the result is true, false if there is the result is false
result || short circuit or a two expressions is true then the result is true, are false if the result is false
| results-circuit or two expressions is true there is a the result is true, If the results are false, false
! The results indicate the expression of non-negated
the difference between short-circuit and non-short-circuit:
Short circuit: a current expression has been possible to determine the result, when the expression is not executed a
non-Short: No matter whether an expression can be determined before the results, after a expressions have to perform
4. unary operators also referred to increment decrement operators
including a ++, ++ a, a - , - a
difference between: a ++ is to use the values are then incremented,
++ A is then incremented to use value after the increment.
5. Relational operators
include == equal to, greater than> Less than <, greater than or equal to> = Less than or equal <=, is not equal to! =
6. The conditional operator
Syntax: boolean expression? Expression 1: Expression 2
Explanation: When the result of the Boolean expression is true when executing the expression 1, 2 as a result of executing an expression of false time