Summary of Java knowledge
- 2020-04-01 01:53:31
- OfStack
1. Basic data types
Plastic:
Byte 1 byte
Short 2 bytes
Int 4 bytes
Long 8 bytes
Character:
Char 2 bytes
Floating point Numbers:
Float 4 bytes
Double 8 bytes
Boolean:
Boolean 1 byte
2. Java 7 Add binary integer
It starts with 0b or 0b
3. The 16-bit Unicode encoding in Java is '\uXXXX', where XXXX represents a hexadecimal integer
4. Positive, negative, and zero are defined in Java
Positive infinity = A positive number divided by 0
Minus infinity is equal to a negative number divided by zero
Divided by 0.0 & 0.0 have spent Or you take the square root of a negative number & cake; I get a non-number
5. In Java, Boolean types can only be true and false
6. There are no multidimensional arrays in Java
It looks like a multidimensional array in C is not a real array, such as a[3][4], a[0] a[1] a[2]& PI; It's a real thing, it's an address, just like an array that's dynamically assigned to c
Int [] [] B = new Int [3] [4]
7. Compilation method with package in Java
Javac - d. Hello. Java A directory tree is generated in the current directory
Run Java Package name. Class name
8. There is no polymorphism in the field of the object in Java polymorphism, such as The parent class Object = New Subclass (), object. Field Is the parent class of the call, even if the field is overridden in the subclass.
9. The instanceof operator
Format: reference variable name Instanceof Class name (or interface) It is used to determine whether the preceding object is a class of the following object, whether the subclass, the instance of the implementation class, returns true or false
10. Conversion between basic data types and corresponding wrapper classes in Java
Int A = 1;
Integer A = New Integer (a);
A = Anderson ntValue ();
The same is true of other types.
11. Example of a singleton class
class Singleton
{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
public static void main(String[] args)
{
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1 == s2);
}
}
12. Final modifies the initialization of member variables
Class Field: the initial value must be specified in the static initial block or when the Field is declared
Instance Field: must be declared in a non-static initial block or in a constructor when the Field is declared
13.Final variables must be explicitly initialized, and the system does not implicitly initialize Final variables
14. Java USES a constant pool to manage previously used String direct constants, such as String a = "Java "; , the system put the constant String "Java" in the constant pool, when the execution of String b = "Java "; A = = b Is true
15. Final methods cannot be overridden, and final classes cannot be inherited
If you use the private method, it's the same thing as final private
If a method with final modification appears ina 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 is immutable after it is created. Java provides eight basic variables to wrap the class and string are immutable classes.
17. Cache instances of immutable classes
class CacheImmutale
{
private static int MAX_SIZE = 10;
private static CacheImmutale[] cache = new CacheImmutale[MAX_SIZE];
private static int pos = 0;
private final String name;
private CacheImmutale(String name)
{
this. name = name;
}
public String getName()
{
return name;
}
public static CacheImmutale valueOf(String name)
{
for(int i = 0; i < MAX_SIZE; ++i)
{
if(cache[i] != null && cache[i].getName().equals(name))
return cache[i];
}
if(pos == MAX_SIZE)
{
cache[0] = new CacheImmutale(name);
pos = 1;
}
else
{
cache[pos++] = new CacheImmutale(name);
}
return cache[pos -1];
}
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);
}
}
Using a cache instance depends on how often an object is used. If it is used repeatedly, the advantages outweigh the disadvantages. If it is not used often, the disadvantages outweigh the advantages
There is also java.lang.integer available in Java; Creates Numbers between -128-127 using a caching mechanism
The Integer in2 = Integer. The valueOf (6);
The Integer in3 = Integer. The 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 another parent class to implement multiple interfaces. The fields in the interface are public, static and final, and the methods are public abstract
20. Methods of non-static inner class access a variable, the search order is: first in the inner class method -> The inner class - > A compilation error occurs if none of the external classes are found
import java.util.*;
public class DiscernVariable
{
private String prop = " Instance variables for external classes ";
private class InClass
{
private String prop = " Instance variables of the inner class ";
public void info()
{
String prop = " A local variable ";
System.out.println(" Outside of the class field Value: " + DiscernVariable.this.prop);
System.out.println(" The inner class field Value: " + 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();
}
}
21. A non-static inner class cannot have a static method, a static Field, or a static initialization block
22. Access inner classes outside the outer classes
Access the non-static inner class: outclass.inclass varname = new outclass(). New Inclass ();
Access the static inner class: Outclass.inclass varname = new outclass.inclass ();
import java.util.*;
class Out
{
class In
{
public In()
{
System.out.println(" Non-static inner class constructor ");
}
}
}
public class CreatInnerInstance
{
public static void main(String[] args)
{
Out.In in = new Out().new In();
}
}
class SubClass extends Out.In
{
//Displays the constructor that defines the SubClass
public SubClass(Out out)
{
//Displays the constructor that invokes In through the passed Out object
out.super();
}
}
import java.util.*;
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();
}
}
class SubClass extends StaticOut.StaticIn
{
//There is no need to create an inner class instance
}