Professional Documents
Culture Documents
Java 基础核心总结
Java 基础核心总结
Java
Java
Java
Java
Java
JDK
JRE
Java
Java
Java
if
if...else
if...else if
switch
while
do...while
for
break
continue
this super
static
final
Exception
Throwable
Exception
Exception Java
throws throw
try finally catch
Error
Iterable
ArrayList
Vector
LinkedList
Stack
HashSet
TreeSet
LinkedHashSet
PriorityQueue
HashMap
TreeMap
LinkedHashMap
Hashtable
IdentityHashMap
WeakHashMap
Collections
Class
Field
Method
ClassLoader
I/O
File
IO
InputStream
OutputStream
Reader
Writer
InputStream
OutputStream
Reader
Writer
null
null
null
Null-Safe
null
Java.IO
Java.lang
Java.math
Java.net
( Java Java666
)
Java
Java
Java Sun Microsystems 1995
Java
Java Java
Java
Java
(Object Oriented)
(Procedure
Oriented)
1 class student {
2 void student_wakeUp(){...}
3 void student_cloth(){...}
4 void student_wash(){...}
5 void student_eating(){...}
6 void student_gotoSchool(){...}
7 }
1 class student(){
2 void wakeUp(){...}
3 void cloth(){...}
4 void wash(){...}
5 void eating(){...}
6 void gotoSchool(){...}
7 }
Java C++
Java
Java
Java
1 public void foo() {
2 int x = 5;
3 boolean b = x;
4 }
Lisp,
Perl, Python Ruby JavaScript
Java
Java JVM
Java Java JVM Windows
Linux
Java
.class .jar
Java
Java Java
Java Java
Java
javac (bytecode) JVM
JVM Hotspot JVM
JIT(Just-In-Time) ︎ JIT ︎
Java
Java
Java
Java
JDK
Java jdk1.8
Description of Java Conceptual Diagram Java
Oracle Java JDK Java
JRE Java Runtime Environment Java JDK JRE
JRE
Java
Java
Java
float double
x
Java Hello hello Java
MyFirstClass
my.first.package
myFirstMethod()
Java
Java
= =
int a = 4
()
()
1 int a = 5;
2 b = ++a;
3 c = a++;
boolean true
false 6
true/false
AND 1 1 0
OR 1 1 0
XOR 0 1 1 0
NOT 0 1
if...else... 1 2
true 1 false 2
Java
if switch
if
if
1 int a = 10;
2 if(a > 10){
3 return true;
4 }
5 return false;
if...else
if else
1 int a = 10;
2 int b = 11;
3 if(a >= b){
4 System.out.println("a >= b");
5 }else{
6 System.out.println("a < b");
7 }
if...else if
if...else if...else if
1 int x = 40;
2 if(x > 60) {
3 System.out.println("x 60");
4 } else if (x > 30) {
5 System.out.println("x 30 60");
6 } else if (x > 0) {
7 System.out.println("x 0 30");
8 } else {
9 System.out.println("x 0");
10 }
switch
if...else if switch
1 switch (week) {
2 case 1:
3 System.out.println("Monday");
4 break;
5 case 2:
6 System.out.println("Tuesday");
7 break;
8 case 3:
9 System.out.println("Wednesday");
10 break;
11 case 4:
12 System.out.println("Thursday");
13 break;
14 case 5:
15 System.out.println("Friday");
16 break;
17 case 6:
18 System.out.println("Saturday");
19 break;
20 case 7:
21 System.out.println("Sunday");
22 break;
23 default:
24 System.out.println("No Else");
25 break;
26 }
while
while while
1 while( ){
2
3 }
( ) true false
1 int a = 10;
2 while(a > 5){
3 a--;
4 }
do...while
while do...while do...while false
while false while
do...while
1 int b = 10;
2 // do···while
3 do {
4 System.out.println("b == " + b);
5 b--;
6 } while(b == 1);
for
for
1 for( ; ; ){}
false for
Java for
for
for-each
break
continue
continue break
return
return
--- Java
C++ Java C#
Java
---
class
1 class ClassName {
2 // body;
3 }
// class
new
Camel-Case
CamelCase
Java
(reference)
1 Car carKey;
Java new
new new
1 class A{
2 int a;
3 Apple apple;
4 }
Java
1 public int getResult(){
2 // ...
3 return 1;
4 }
getResult () return
--- void
{}
Java Java
1 class Apple {
2
3 int sum;
4 String color;
5
6 public Apple(){}
7 public Apple(int sum){}
8 public Apple(String color){}
9 public Apple(int sum,String color){}
10
11 }
1 class createApple {
2
3 public static void main(String[] args) {
4 Apple apple1 = new Apple();
5 Apple apple2 = new Apple(1);
6 Apple apple3 = new Apple("red");
7 Apple apple4 = new Apple(2,"color");
8
9 }
10 }
Apple Apple
JVM
1 class Apple {
2
3 int sum;
4 String color;
5
6 }
7
8 class createApple {
9
10 public static void main(String[] args) {
11 Apple apple1 = new Apple();
12
13 }
14 }
Apple
JVM
JVM
Apple int
Java
Apple getApple
Java
1 class Fruit {
2
3 public void eat(){
4 System.out.printl('eat fruit');
5 }
6 }
7
8 class Apple extends Fruit{
9
10 @Override
11 public void eat(){
12 System.out.printl('eat apple');
13 }
14 }
Apple Fruit
, ,
@Override
Car new
1 class Car{
2 public Car(){}
3 }
JVM
Java
String null
1 int a = 11
a 0 11
i 11
static
static {}
static
{}
1 int[] a1;
2
3 //
4
5 int a1[];
Java
1 add(); //
2 add(1); //
3 add(2,1); //
4 add(new Integer[] {1, 3, 2}); //
Java Java
J (scope)
C C++ Java {}
1 {
2 int a = 11;
3 {
4 int b = 12;
5 }
6 }
a {} b {}
1 {
2 int x = 11;
3 {
4 int x = 12;
5 }
6 }
this super
this super Java
eatApple()
this eatApple return
this eatApple
this this
1 public class Apple {
2
3 private int num;
4
5 public Apple(int num){
6 this.num = num;
7 }
8
9 public static void main(String[] args) {
10 new Apple(10);
11 }
12 }
this
this, this( )
this()
this super super this
super.
super( )
this super
bug
1 class Father{}
2
3 class Son extends Father{}
1 class Father{
2
3 public void feature(){
4 System.out.println(" ");
5 }
6 }
7
8 class Son extends Father {
9 }
Son feature
feature feature
1 public class Fruit {
2
3 int num;
4
5 public void eat(){
6 System.out.println("eat Fruit");
7 }
8 }
9
10 public class Apple extends Fruit{
11
12 @Override
13 public void eat() {
14 super.num = 10;
15 System.out.println("eat " + num + " Apple");
16 }
17
18 public static void main(String[] args) {
19 Fruit fruit = new Apple();
20 fruit.eat();
21 }
22 }
static
static Java static static
/
static
static .
this
this
static
static
final
final
CxuanGoodJob cxuan
cxuan
interface
public default
implements
Java abstract
abstract
NullPointerException
ClassNotFoundException RuntimeException
Exception
Exception java.lang Throwable Exception
Throwable
Exception Throwable
Throwable
Throwable Java (errors) (exceptions)
Throwable Java @throw
Java
The unchecked exception classes are the run-time exception classes and the error classes.
The checked exception classes are all exception classes other than the unchecked exception
classes. That is, the checked exception classes are Throwable and all its subclasses other
than RuntimeException and its subclasses and Error and its subclasses.
RuntimeException error
checkedException
Throwable
Throwable Throwable
1 //
2 public string getMessage();
3 public string getLocalizedMessage();
4
5 //
6 public public String toString()
7
8 //
9 public void printStackTrace();
10 public void printStackTrace(PrintStream s);
11 public void printStackTrace(PrintWriter s)
12
13 //
14 public synchronized Throwable fillInStackTrace();
Exception
Exception Exception Throwable Exception
RuntimeException CheckedException
Java
RuntimeException
UncheckedException
Exception Java
Java Java throws throw try finally
catch
throws throw
Java
throws throw
throws throw
throw throws
throws throw
try...catch
1 static void cacheException() throws Exception{
2
3 try {
4 System.out.println("1");
5 }catch (Exception e){
6 e.printStackTrace();
7 }
8
9 }
try...finally finally
try...catch...finally finally
Error
Error
JVM Java
OutOfMemoryError StackOverflowError
Java JDK1.7
Java
OutOfMemoryError
Java
Native (Undefined)
StackOverflowError
OutOfMemoryError
Java
OutOfMemoryError
OutOfMemoryError
Java
IDEA class
class
1 public class OuterClass {
2 private String name ;
3 private int age;
4
5 class InnerClass{
6 public InnerClass(){
7 name = "cxuan";
8 age = 25;
9 }
10 }
11 }
InnerClass OuterClass
Java
( )
( )
( )
.class
$ OuterClass$InnerClass.class
Collection
- Iterator
Iterable
for-each for Java
for-each
1 Iterator<T> iterator();
fail-fast
1 for(Iterator it = coll.iterator(); it.hasNext(); ){
2 System.out.println(it.next());
3 }
Collection
ArrayList
ArrayList List ( )
ArrayList ArrayList
list Vector ArrayList
ArrayList List
ArrayList ArrayList
List
Collections.synchronizedList
Vector
ArrayList
LinkedList ( null )
LinkedList
Stack
LIFO Deque
HashSet
HashSet set
Collections.synchronizedSet()
fail-fast
TreeSet
fail-fast
LinkedHashSet
LinkedHashSet HashSet
LinkedHashSet HashSet
LinkedHashSet
LinkedHashSet LinkedHashSet
1 Collections.synchronizedSet
fail-fast
PriorityQueue
PriorityQueue AbstractQueue
Comparator PriorityQueue null
poll,remove,peek element
capacity
Collection Iterator
iterator()
Arrays.sort(pq.toArray())
PriorityQueue
PriorityBlockingQueue
HashMap
TreeMap
LinkedHashMap
LinkedHashMap(int,float,boolean) LinkedHashMap
removeEldestEntry(Map.Entry) map
map null
HashMap LinkedHashMap collection-views map.size
HashMap
LinkedHashMap
LinkedHashMap
map Map m =
Collections.synchronizedMap(new LinkedHashMap(...))
fail-fast
Hashtable
Hashtable
fail-fast
Hashtable
HashMap ConcurrentHashMap
IdentityHashMap
IdentityHashMap Map
WeakHashMap
map WeakHashMap
null null
fast-fail
WeakHashMap
Collections
Collections Java Collections
collections
UnsupportedOperationException
collection
collection
1 public static Collection unmodifiableCollection(Collection<? extends T> c);
2 public static Set unmodifiableSet(Set<? extends T> s);
3 public static List unmodifiableList(List<? extends T> list);
4 public static <K,V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m);
5 public static SortedSet unmodifiableSortedSet(SortedSet<? extends T> s);
6 public static <K,V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, ? extends
V> m);
7
Collections
Collections
https://mp.weixin.qq.com/s/ZflBpn2TBzTNv_-G-zZxNg
Jdk1.5
1 List arrayList = new ArrayList();
2 arrayList.add("cxuan");
3 arrayList.add(100);
4
5 for(int i = 0; i< arrayList.size();i++){
6 String item = (String)arrayList.get(i);
7 System.out.println("test === ", item);
8 }
Integer String
1 // T T E K V
2 public class GenericDemo<T>{
3 //value T,T
4 private T value;
5
6 public GenericDemo(T value) {
7 this.value = value;
8 }
9
10 public T getValue(){ // getKey T T
11 return value;
12 }
13
14 public void setValue(T value){
15 this.value = value
16 }
17 }
1 //
2 public interface Generator<T> {
3 public T next();
4 }
(generator)
Java
java
Java
Java
Class
Java java class Class
.class Class Class
Class Class
Class
toString()
java.lang.Boolean : boolean
java.lang.Character: char
java.lang.Byte: byte
java.lang.Short: short
java.lang.Integer: int
java.lang.Long: long
java.lang.Float: float
java.lang.Double: double
java.lang.Void: void
getName()
toGenericString()
forName()
Class
Class.forName(java.lang.Thread)
Thread.class
thread.getClass()
newInstance()
forName() newInstance
getClassLoader()
getTypeParameters()
getPackage()
getInterfaces()
Class
Cast
asSubclass(Class clazz)
getClasses()
getDeclaredClasses()
getSimpleName()
getFields()
getField(String name)
getDeclaredField(String name)
getDeclaredFields()
getAnnotation(Class annotationClass)
getAnnotations()
getDeclaredAnnotation(Class annotationClass)
getDeclaredAnnotations()
getConstructor(Class...<?> parameterTypes)
getConstructors()
getDeclaredConstructor(Class...<?> parameterTypes)
getDeclaredConstructors()
getMethods()
getDeclaredMethods()
Field
Field
API
equals(Object obj)
obj true
get(Object obj)
obj
obj
Method
invoke(Object obj, Object... args)
object
ClassLoader
ClassLoader (class) JVM
ClassLoader
ClassLoader
Java enum
enum Java C C++
Family 4
enum
enum .
? ennum static
Family
public static
final xxx
switch enum
1 enum Signal {
2 GREEN, YELLOW, RED
3 }
4
5 public class TrafficLight {
6 Signal color = Signal.RED;
7
8 public void change() {
9 switch (color) {
10 case RED:
11 color = Signal.GREEN;
12 break;
13 case YELLOW:
14 color = Signal.RED;
15 break;
16 case GREEN:
17 color = Signal.YELLOW;
18 break;
19 }
20 }
21 }
Java enum Enum
Enum java.lang
values values()
static
enum isEnum
EnumSet EnumMap
Java
I/O
I/O JDK
I/O
IO IO I/O Java
I/O I/O
IO
I/O
I/O
File
File
1 class FileDemo{
2 public static void main(String[] args) {
3 File file = new File("D:\\file.txt");
4 try{
5 f.createNewFile(); //
6
7 // File
8 // ( <windows ; linux >
9 System.out.println(File.pathSeparator); // ;
10 // <windows \ linux / >
11 System.out.println(File.separator); // \
12
13 //
14 /*
15 File file = new File(fileName);
16 if(f.exists()){
17 f.delete();
18 }else{
19 System.out.println(" ");
20 }
21 */
22
23
24 }catch (Exception e) {
25 e.printStackTrace();
26 }
27 }
28 }
1 class FileDemo{
2 public static void main(String[] args) {
3 String fileName = "D:"+ File.separator + "filepackage";
4 File file = new File(fileName);
5 f.mkdir();
6
7 //
8 /*
9 String[] str = file.list();
10 for (int i = 0; i < str.length; i++) {
11 System.out.println(str[i]);
12 }
13 */
14
15 // file.listFiles();
16
17 // file.isDirectory()
18 }
19 }
1 File(String directoryPath);
2 File(String directoryPath, String filename);
3 File(File dirObj, String filename);
File
IO
. IO InputStream OutputStream Reader Writer
read() write()
IO native C/C++
InputStream
InputStream Java
IOException
OutputStream
OutputStream void
IOException
Reader
Writer void
IOException
InputStream
FileInputStream FileInputStream InputStream
ByteArrayInputStream InputStream
PipedInputStream pipe
FilterOutputStream
ObjectInputStream ObjectOutputStream
DataInputStream : Java
PushbackInputStream pushback
Pushback
OutputStream
FileOutputStream
ByteArrayOutputStream byte
PipedOutputStream
ObjectOutputStream
FilterOutputStream
PrintStream PrintStream
DataOutputStream : Java
Reader
FileReader
CharArrayReader
BufferedReader BufferedReader
PushbackReader : PushbackReader
PipedReader
Writer
FileWriter FileWriter Writer
CharArrayWriter CharArrayWriter
PipedWriter
Java
InputStream InputStreamReader Reader
Writer Java
Java Annotation
JDK1.5 Java 7 3 java.lang 4
java.lang.annotation
@Override
@Deprecated
@SuppressWarnings
@Retention : class
@Documented : JavaDoc
@Target Annotation Annotation
packages types Annotation
catch
@Inherited
JDK1.7
Java NullPointerException
Java
eclipse Idea
null
null Java null Object
null int
0 boolean false
null
null null
null
null
null null
1 public static void main(String[] args) {
2 int i = 0;
3 Integer itr = null;
4 System.out.println(itr == i);
5 }
instanceof
null NullPointerException
Null-Safe
null-safe java
Integer , Double
null
== != null
SQL Java null == null true
1 public class CompareNull {
2
3 private static String str1;
4 private static String str2;
5
6 public static void main(String[] args) {
7 System.out.println("str1 == str2 ? " + str1 == str2);
8 System.out.println(null == null);
9 }
10 }
Java
Java.IO
Java.lang
Java.math
Java.net
cxuan