This is the 10th day of my participation in Gwen Challenge
Java error point 2
If there is any misunderstanding, please correct us!!
Data type conversion
Data type conversions are classified into explicit conversions (cast) and implicit conversions (automatic conversions).
Automatic conversion rules:
- Data type compatibility
- Byte →short→int→long→float→double 2, char→int
Value range:
- Byte: -2^7 to 2^7-1, that is, -128 to 127. 1 byte.
- Short :-2^15 ~ 2^15-1, i.e. -32768 ~ 32767. 2 bytes.
- Signed int: -2^31 ~ 2^31-1, that is, -2147483648 ~ 2147483647. 4 bytes.
- Unsigned int: 0~2^32-1.
- Long :-2^63 ~ 2^63-1, namely -9223372036854774808 ~ 9223372036854774807. 8 bytes.
- Float: 4 bytes.
- Double: 8 bytes
- Char: 2 bytes
- boole
There is no loss of accuracy in the conversion of the figure above
Example Code 1
package com.wangscaler;
/ * * *@author wangscaler
* @dateHe 2021.06.09 * /
public class TestType {
public static void main(String[] args) {
long a = 2147483648;// There is an error in this sentence
System.out.println(a);
long b = 2147483648L; System.out.println(b); }}Copy the code
Result 1
The execution results of the last two sentences
-2147483648
Copy the code
Answer a
In Java, int ranges from -2147483648 to 2147483647, while long a = 2147483648; The number to the right of the code is considered to be a literal of the consolidation type, but the value exceeds the integer value range, so an error is reported. However, the long type can be 2147483648, so how to assign? If L is added to the end, it is assumed to be a literal of type Long, and the assignment will not go wrong.
00000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000 —>long 2148483648
Strong conversion to int 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
1000 0000 0000 0000 0000 0000 0000 0000– > the value is -2147483648
That’s why integers range from -2147483648 to 2147483647 (a negative number has one more digit than a positive number), because 10000000 00000000 00000000 00000000 00000000 doesn’t mean -0, it means -n, so either +0 or -0 is 0 and there’s no need to waste space, So 0000 0000 0000 0000 0000 0000 is 0
Example Code 2
package com.wangscaler;
/ * * *@author wangscaler
* @dateHe 2021.06.09 * /
public class TestType {
public static void main(String[] args) {
int a = 1999999999;
float b = 1.0 f;
double c = 1.0 d;
System.out.println(a * b);
System.out.println(a * c);
short g = 222;
System.out.println(g / 7);
System.out.println(g / 7.0 f);
int h = 6789;
System.out.println(h / 30);
System.out.println(h / 30.);
int i = 2147483642;
i += 1.0 f; System.out.println(i); }}Copy the code
Result 2
2.0 e9
1.999999999 e9
31
31.714285
226
226.3
2147483647
Copy the code
Answer 2
A * B process: A will automatically change to float to participate in the calculation, and there will be a loss of accuracy.
A * C process: A will automatically convert to double to participate in the calculation, there will be no loss of accuracy.
G /7:g is automatically converted to an int, so the result is an integer of 31.
G /7.0 F: G is automatically converted to float to participate in the calculation, and the result is a floating point number, which can achieve the expected result.
H /30: Integer divided by integer is still integer, so 226.
H /30.:h is automatically converted to doublet to participate in the calculation, so the result is a double.
I +=1.0f: I will be converted to float to participate in the calculation. After the calculation, it will be converted to int, and there will be a loss of accuracy.
conclusion
In summary, implicit conversions of the cast should be avoided in the program.
The use of the class
-
Overloading: a class in which methods have the same name but different parameters. The return types can be the same or different.
Overload rules:
- Overloaded methods must change their argument lists (with different numbers or types of arguments);
- Overloaded methods can change the return type;
- Overloaded methods can change access modifiers;
- Overloaded methods can declare new or broader check exceptions;
- Methods can be overridden in the same class or in a subclass.
- Overloaded functions cannot be distinguished by return value types.
-
Override: A subclass rewrites the implementation of the parent class’s accessible methods, with the return value and parameters unchanged.
Method rewrite rule:
- The argument list must be exactly the same as the argument list of the method being overridden.
- The return type can be different from the return type of the overridden method, but it must be a derivative of the value returned by the parent class (Java5 and earlier must have the same return type, and Java7 and later can be different).
- Access cannot be lower than that of the overridden method in the parent class. For example, if a method of a parent class is declared public, overriding that method in a subclass cannot declare it protected.
- A member method of a parent class can only be overridden by its subclasses.
- Methods declared final cannot be overridden.
- Methods declared static cannot be overridden, but can be declared again.
- If a subclass is in the same package as its parent, it can override all methods of its parent except those declared private and final.
- If a subclass and its parent are not in the same package, a subclass can override only non-final methods declared public and protected by the parent class.
- An overridden method can throw any optional exception, whether or not the overridden method throws an exception. However, overridden methods cannot throw new mandatory exceptions, or more extensive exceptions than those declared by the overridden method, or vice versa.
- The constructor cannot be overridden.
- If you cannot inherit from a class, you cannot override the methods of that class.
-
Hide: Static methods and properties of a parent class that appear in a subclass are not overridden, but hidden. You should use the class name directly to avoid confusion.
-
Shadowing: When one declaration obscures another, the simple name refers to the entity in the shadowing declaration (no examples here)
-
Masking: Masking is the reuse of two names in different namespaces, including variables, packages, methods, or types.
The sample code
People object
package com.wangscaler;
public class People {
public String word = "people";
public static void eat(a) {
System.out.println("Eat");
}
public void sleep(a) {
System.out.println("Sleep"); }}Copy the code
Man
package com.wangscaler;
public class Man extends People {
public String word = "man";
@Override
public void sleep(a) {
System.out.println(snore);
}
public static void eat(a) {
System.out.println(Operation Compact Disc); }}Copy the code
The main function
package com.wangscaler;
/ * * *@author wangscaler
* @date2021.06.10 giving * /
public class TestClass {
public static void main(String[] args) {
People people = new People();
People pman = new Man();
Man man = newMan(); people.sleep(); pman.sleep(); man.sleep(); people.eat(); pman.eat(); Man.eat(); man.eat(); System.out.println(people.word); System.out.println(pman.word); System.out.println(man.word); }}Copy the code
The execution result
Sleep snore snore eat eat CD action CD actionCopy the code
conclusion
You can see that the @override annotation cannot be used on the man function eat because it is hidden, not overridden.
To avoid confusion, you should call directly with the class name, like man.eat () above;
The shadowing situation is as follows:
String System;
System.out.println("a");
Copy the code
In this case, System will become a string defined by ourselves, which will cause compilation failure, so we should avoid saving words and keywords when defining variables.
Keywords are as follows:
Reserved words | Package related | Access control | Variable reference | Error handling | Program control | Basic types of | Class method variable modifier |
---|---|---|---|---|---|---|---|
goto | package | protected | void | throws | break | boolean | abstract |
const | import | private | this | throw | continue | false | class1` |
public | super | try | return | float | extends | ||
catch | do | byte | final | ||||
finally | while | char | implements | ||||
if | double | interface | |||||
assertions | else | int | native | ||||
assert | for | short | new | ||||
instanceof | long | static | |||||
case | null | strictfpp | |||||
switch | true | synchronized | |||||
default | transient | ||||||
volatile |