java day02
DtaaType Data type
Java is a strongly typed language.
1. Variables
Variable: A quantity whose value can change during the running of a Java program.
Format for defining the definition of a statement that defines a variable:
// Data type Variable name = initialized value;
Caveats:
1、Variables have to be initialized and assigned before they can be used
2, in the same scope, the variable name can not be repeated, can be case different, strictly case sensitive.
3, java in the default integer is int type, to define a long type of variable, you need to add the end of the value of L or l, recommend the use of L
4, java in the decimal default is double type, to define a float type of variable, you need to add the value of the end of the F or f, recommended use of F
2. Data types
1. Basic data types:
/* Integer type: number of bytes occupied Range of representability
byte 1 -2^7 ~ 2^7-1
short 2 -2^15 ~ 2^15-1
int 4 -2^32 ~ 2^32-1
long 8 -2^63 ~ 2^63-1
Fractional type:
float 4
double 8
Boolean types:
boole 1
Character type:
char Depending on the specific encoding, the number of bytes a character takes up in java is different under different encodings
*/
2. Reference data types: [not learned for the time being]
3. Code examples
public class DataTypeDemo1{
public static void main(String[] args) {
//data type variable name = initial value
// Define a variable of type byte
byte b1 = 100;
(b1); // print this variable directly, actually printing the value stored in the variable
// Define a variable of type short
short s1 = 200;
(s1);
// Define a variable of type int
int i1 = 300; (i1); //define a variable of type int
(i1); //define a variable of type int
//int i2 = 30000000000000; outside the range of the int type
long i2 = 30000000000000L; //int i2 = 30000000000000L.
(i2).
// Define a float variable.
float f1 = 12.34F; (f1); //define a variable of type float
(f1).
// Define a variable of type double
double d1 = 33.45;
(d1).
// Define a boolean variable
boolean b2 = true;
(b2);
// Create a character variable
char c1 = 'a';
(c1);
}
}
3. Data type conversion
Automatic data type conversion
// In Java, when variables are involved in an operation, the data type is automatically elevated and will be converted to the largest data type in the computation
byte,short,char -> int -> long -> float -> double
Forced data type conversion
/* Statement definition format:
Target data type Variable name = (target data type) (value or expression to be transformed)
Note: Try not to use this in the future, you may lose precision.
*/
code example
public class DataTypeDemo2 {
public static void main(String[] args) {
// Which sentence is the one that fails to compile? Why?
// byte b1=3,b2=4,b; // not recommended to be defined this way
// When working, it is recommended that one variable definition takes up one line
byte b1=3.
byte b2=4.
byte b.
// b=b1+b2; // If a variable is involved in an operation, it will be automatically converted first, and after the operation is done, it will be judged whether it can be assigned or not.
// Forced type conversion
b=(byte)(b1+b2); // b=(byte)(b1+b2).
(b); // b=3+4; // b=3+4
// b=3+4; // If it's a constant, it will do the operation first, and then determine if the result is within the range, and if it is, assign it directly.
}
}
Forced Data Type Conversion Cases
public class DataTypeDemo3 {
public static void main(String[] args) {
byte b = (byte)130; // Can't be assigned directly because 130 exceeds the maximum range of a byte.
(b); // -126
}
}
// Calculation procedure
/*
Data in computers are computed using complementary codes
The complement of 130: 00000000 00000000 00000000 10000010
Do forced type conversion:
Complement: (00000000 00000000 00000000) 10000010
Complement: 10000010
Find the original code with the known complement:
Symbolic bits Numeric bits
Complement: 1 0000010
Inverse: 1 0000001
Original: 1 1111110
-----------------------------
64 + 32 + 16 + 8 + 4 + 2 = 126
Since the sign bit is 1, the final result is -126
*/
practice
Write the result of the following program
('a');
('a'+1);
("hello "+'a'+1); ('a'+1+"hello").
('a'+1+"hello"); ("5+5="5+5"); ("5+5="5+5")
("5+5 = "+5+5); ("5+5+"=5+5"); ("5+5+"+5+5")
(5+5+"=5+5");
/*
Notes:
1, + sign on both sides if there is no string, this is the most common arithmetic addition operation
2, if there are characters involved in arithmetic operations, the java virtual machine will be the character corresponding to the ASCII code table for the conversion of values
Remember the ASCII values corresponding to the three special characters:
'0' - 48
'A' - 65
'a' - 97
3、Symbols with the same level of precedence, operate from left to right
4, + sign on both sides if either side is a string, but to do the string of left and right splicing operations, the end of the splicing is a new string
*/
solution
public class DataTypeDemo4 {
public static void main(String[] args) {
// Code formatting shortcut: ctrl + alt + L
// ('a'); // a
// ('a' + 1); // 98
("hello" + 'a' + 1); // helloa1
('a' + 1 + "hello"); // 98hello
("5 + 5 = " + 5 + 5); // 5 + 5 = 55
(5 + 5 + "=5+5"); // 10=5+5
("5+5=" + (5+5)); // 5+5=10
}
}
4、Operation
1. Assignment operations
1. Assignment operators: =, +=, -=, *=, /=, %=.
code example
public class FuZhiDemo1 {
public static void main(String[] args) {
int a = 3;
// a += 4;
// (a);
a-=5;
(a);
}
}
/*
short s=1, s = s+1;
short s=1, s+=1;
*/
public class FuZhiDemo2 {
public static void main(String[] args) {
// short s = 1;
// s = (short) (s + 1);
// (s);
short s = 1;
s += 1; // s = (short) (s + 1);
(s);
}
}
2. Relational arithmetic
1, relational operators: >, <, >=, <=, ! =, ==
The result of a relational expression must be of type boolean
2、Code example
public class GuanXiDemo1 {
public static void main(String[] args) {
int a = 3;
int b = 4;
(a==b); // false
// (a=b); // 4
(a>b);
(a<b);
(a<=b);
(a>=b);
(a!=b);
}
}
3. Logical operations
1. Logical operators: & , | , ^ , ! , && , ||
logical operator, the type of the parameter involved in the operation is boolean, and the result is also a boolean of type
2、Code example
public class LuoJiDemo1 {
public static void main(String[] args) {
int a = 3;
int b = 4;
// & there arefalseimitatefalse
// ((a++>3)&(b++>4)); // false & false = false
// ("a: " + a + ", b: " + b);
// ((a++>3)&(b++==4)); // false & true = false
// ((a++==3)&(b++>4)); // true & false = false
// ((a++==3)&(b++==4));// true & true = true
// && Short Circuit and When the left side isfalsewhen,The right expression will not be executed,provefalse
// ((a++ > 3) && (b++ > 4)); // false && false = false
// ("a: " + a + ", b: " + b);
// ((a++ > 3) && (b++ == 4)); // false && true = false
// ((a++ == 3) && (b++ > 4)); // true && false = false
// ((a++ == 3) && (b++ == 4));// true && true = true
// there aretrueimitatetrue
// ((a++ > 3) | (b++ > 4)); // false | false = false
// ((a++ > 3) | (b++ == 4)); // false | true = true
// ((a++ == 3) | (b++ > 4)); // true | false = true
// ((a++ == 3) | (b++ == 4));// true | true = true
// ("a: " + a + ", b: " + b);
// // || Shorted or When the left side istruewhen,The right expression will not be executed,provetrue
// ((a++ > 3) || (b++ > 4)); // false || false = false
// ((a++ > 3) || (b++ == 4)); // false || true = true
// ((a++ == 3) || (b++ > 4)); // true || false = true
// ((a++ == 3) || (b++ == 4));// true || true = true
// ("a: " + a + ", b: " + b);
// (100>a>10) // javaSuccessive comparisons are not allowed in
(a>10 & a<100);
// ^ differentiation 相同imitatefalse,不同imitatetrue
// ((a++ > 3) ^ (b++ > 4)); // false ^ false = false
// ((a++ > 3) ^ (b++ == 4)); // false ^ true = true
// ((a++ == 3) ^ (b++ > 4)); // true ^ false = true
// ((a++ == 3) ^ (b++ == 4));// true ^ true = false
//! commander-in-chief (military)truechange intofalse, falsechange intotrue
// (!(a++ > 3));
}
}
4. Trinomial arithmetic
1. The trinomial operator [ternary operator]:
The statement defines the format:
(relational expression)? Expression 1:Expression 2.
Here expression 1 and expression 2 must be to have a result value. The relational expression is established to receive expression 1 and vice versa to receive expression 2
2、Code example
//demand (economics):Find the maximum of two numbers
public class SanMuDemo1 {
public static void main(String[] args) {
int a = 3;
int b = 8;
int x = (a > b) ? a : b;
(x);
(a > b) ? (a) : (b);
}
}
5. Arithmetic operations
1. Arithmetic operators: +, -, *, /, %, ++, --
In java, the data type of the final result of an arithmetic expression is determined by the type of the parameter with the largest participation in the operation.
2、Code example
public class SuanShuDemo1 {
public static void main(String[] args) {
int a = 3;
int b = 4;
// (a+b);
// (a-b);
// (a*b);
// (a/b); // divide exactly without remainder (in integer arithmetic),Retention of integer parts
// (a%b);
(a*1.0/b);
}
}
3. +1 vs.
/*++: self-add 1
++ comes later, first assignment and then self-add 1; ++ comes first, first self-add 1 and then assignment to participate in the operation
--: self-decrease by 1
After --, assign and then subtract 1; before --, subtract 1 and then assign.
*/
public class SuanShuDemo2 {
public static void main(String[] args) {
/**
* Standalone
*/
int a = 3;
int b = 4;
("a: " + a); ("b: " + b); int b = 4.
("b: " + b).
("-----------------------------");
// ++a.
// ++b; // a++; // b++; // ++a; // ++a; // ++b
/**
* Mixed use
*/
// int x = a++; // (x); // 3
// (x); // 3
int x2 = ++a; // (x2); // (x2); // 3
(x2).
}
}
6. Bitwise operations
1、
Bitwise operators: for numeric binary form calculations
& | ^ ~ >> << >>>.
Bitwise operators, where the value is a numeric value and the result is a numeric value.
Code Samples and Principles
public class WeiDemo1 {
public static void main(String[] args) {
int a = 3;
int b = 4;
// (a&b);
// (a|b);
// (a^b);
(~a);
}
}
/*
All operations in computers are performed using complementary codes
3complementary code of a function (math.):00000000 00000000 00000011
4complementary code of a function (math.):00000000 00000000 00000100
&&&:there are0imitate0
00000000 00000000 00000000 00000011
&
00000000 00000000 00000000 00000100
-------------------------------------
00000000 00000000 00000000 00000000
|:there are1imitate1
00000000 00000000 00000000 00000011
|
00000000 00000000 00000000 00000100
-------------------------------------
00000000 00000000 00000000 00000111
^:相同imitate0,不同imitate1
00000000 00000000 00000000 00000011
^
00000000 00000000 00000000 00000100
-------------------------------------
00000000 00000000 00000000 00000111
~:0change into1,1change into0
00000000 00000000 00000000 00000011
~
-------------------------------------
binary code with 0 and 1 interchanged 11111111 11111111 11111100
reverse code 11111111 11111111 11111011
source code 10000000 00000000 00000100
-----------------------------------------
-4
*/
2、
>>:Right shift, the overall complement is shifted to the right by a number of bits, the extra part of the right side is discarded, if the highest bit is 1, the left side will be filled with 1; vice versa, it will be filled with 0.
<<:Left shift, the overall complement is shifted to the left by a number of bits, the extra part on the left is discarded, and the right is completed with 0
>>>:Unsigned right shift, the overall complement is shifted to the right by a number of bits, the extra part of the right side is discarded, and the left side is filled with 0 regardless of whether the highest bit is 0 or 1.
Code Samples and Principles
public class WeiDemo2 {
public static void main(String[] args) {
(24>>2); // 24/2^2 = 6
(2<<4); // 2*2^4 = 32
(-24>>>>2); // 1073741818
}
}
/*
24complementary code of a function (math.):00000000 00000000 00000000 00011000
00000000 00000000 00000000 00011000
>>2: 00000000 00000000 00000000 00000110(00)
------------------------------------------------
2complementary code of a function (math.):00000000 00000000 00000000 00000010
00000000 00000000 00000000 00000010
<<4: (0000)00000000 00000000 00000000 00100000
-24
source code:10000000 00000000 00000000 00011000
reverse code:11111111 11111111 11111111 11100111
binary code with 0 and 1 interchanged:11111111 11111111 11111111 11101000
------------------------------------------
11111111 11111111 11111111 11101000
>>>>2: 0011111111 11111111 11111111 111010(00)
*/