1. Basic data types
Plastic surgery:
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
character:
char 2 bytes
Floating point number:
float 4 bytes
double 8 bytes
Boolean:
boolean 1 byte
2.java 7 adds new binary integers
Start with 0b or 0B
3.Characters in java are 16-bit Unicode encoding, the format is '/uXXXX', where xxxx represents a hexadecimal integer
4. Java stipulates positive infinity, negative infinity and zero.
Positive infinity = a positive number divided by 0
Negative infinity = a negative number divided by zero
Divide 0.0 by 0.0 or take the square root of a negative number to get a not number
5. Boolean types in Java can only be true and false
6. There is no multidimensional array in java
It seems that the multi-dimensional arrays in C language are not real arrays. For example, a[3][4], a[0] a[1] a[2] are real, and they are filled with addresses, and are dynamic in C language. The same as the array allocated as
int [][] b = new int[3][4]
7. Compilation method with package in Java
javac -d . Hello.java will generate a directory tree in the current directory
Run java package name.class name
8. The field of objects in Java polymorphism does not have polymorphism, such as parent class object = new subclass (), object.field is called the parent class, even if the field is covered in the subclass.
9. instanceof operator
Format: Reference variable name instanceof class name (or interface) It is used to determine whether the previous object is an instance of the class, subclass, or implementation class of the following object. If so, it returns true, otherwise it returns false.
10. Conversion between basic data types and corresponding encapsulation classes in Java
int a = 1;
Integer A = new Integer(a);
a = A.intValue();
The same goes for other types.
11. Singleton class example
public static void main(String[] args)
{
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
}
Class Field: The initial value must be specified in a static initialization block or when declaring the FIeld
Instance Field: must be declared in a non-static initialization block or by specifying an initial value or constructor when declaring the FIeld.
13.Final variables must be explicitly initialized. The system will not implicitly initialize final variables.
14.java will use the constant pool to manage the direct string constants that have been used before, for example: String a = "java";, the system will save the constant string "java" in the constant pool, and when String b = "java"; is executed again, a == b is true
15.Final methods cannot be overridden, and final classes cannot be inherited.
If the private method is used, it is the same as final private.
If a method modified with final appears in a subclass, it is newly defined by the subclass and has nothing to do with the parent class.
16. Immutable class: The Field of this class cannot be changed after creation. Java provides eight basic variable wrapper classes and string classes are both immutable.
17. Immutable classes that cache instances
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj != null && obj.getClass() == CacheImmutale.class)
{
CacheImmutale ci = (CacheImmutale)obj;
return name.equals(ci.getName());
}
return false;
}
public int hashCode()
{
return name.hashCode();
}
}
public class CacheImmuteTest
{
public static void main(String[] args)
{
CacheImmutale c1 = CacheImmutale.valueOf("Hello");
CacheImmutale c2 = CacheImmutale.valueOf("Hello");
System.out.println(c1 == c2);
}
}
There is also java.lang.Integer provided by java, which uses a caching mechanism to create numbers between -128-127.
Integer in2 = Integer.valueOf(6);
Integer in3= Integer.valueOf(6);
in2 == in3 is true;
18. Static and abstract cannot modify a method at the same time. There is no class abstract method.
19. A class can have a parent class and implement multiple interfaces. In the interface, Filed is public, static, and final, and the method is public abstract.
20. When a method of a non-static inner class accesses a variable, the search order is: first within the method in the inner class -> inner class -> if not found in the outer class, a compilation error will occur.
public class DiscernVariable
{
private String prop = "Instance variable of external class";
private class InClass
{
private String prop = "Instance variable of inner class";
public void info()
{
String prop = "local variable";
System.out.println("Field value of external class: " + DiscernVariable.this.prop);
System.out.println("Field value of inner class: " + this.prop);
System.out.println("Value of local variable: " + prop);
}
}
public void test()
{
InClass in = new InClass();
in.info();
}
public static void main(String[] args)
{
new DiscernVariable().test();
}
}
22. Access inner classes outside outer classes
Access non-static inner classes: outclass.Inclass varname = new outclass().new InClass();
Access static inner classes: outclass.Inclass varname = new outclass.Inclass();
class Out
{
classIn
{
publicIn()
{
System.out.println("Non-static inner class constructor");
}
}
}
public classCreatInnerInstance
{
public static void main(String[] args)
{
Out.In in = new Out().new In();
/*
The above code can be written separately as:
Out.In in;
Out out = new Out();
in = out.new In();
*/
}
}
class SubClass extends Out.In
{
//Display the constructor that defines SubClass
public SubClass(Out out)
{
//Display the constructor calling In through the transferred Out object
out.super();
}
}
class StaticOut
{
static class StaticIn
{
public StaticIn()
{
System.out.println("static inner class constructor");
}
}
}
public class CreatStaticInnerInstance
{
public static void main(String[] args)
{
StaticOut.StaticIn in = new StaticOut.StaticIn();
/*
The above code can be written separately as:
StaticOut.StaticIn in;
in = new StaticOut.StaticIn();
*/
}
}
class SubClass extends StaticOut.StaticIn
{
//No need to create inner class instance
}