Javase Basic Knowledge

Source: Internet
Author: User
Tags comparable naming convention unpack

Javase Review

1. Java System

2. Java Core System

3. Environment configuration

4. Basic syntax

5. Object-oriented

6. Exceptions (Exception)

7. Arrays (Array)

8. Basic class (Common Class)

9. Input/output stream (I/O stream)

10. Container/generics (collection/generic)

11. Threads (thread)

12. Network Programming (TCP/UDP)

GUI.

Meta Data

15 Regular Expressions

1. Java System

2. Java Core System 2.1 Java Virtual Machine 2.2 java recycle mechanism 3. Environment Configuration 4. Base Syntax 4.1 Identifier 4.1.1 What is a 4.1.2 naming convention

(1) composition

(2) Opening

(3) Case sensitive, no length limit.

(4) The conventional: see name Spur, can not be duplicate with the keyword

4.2 keyword 4.3 data type 4.3.1 constant 4.3.2 variable

(1) Definition

is essentially a small area of memory that accesses the area with a command name.

So you have to declare (apply) and then assign (populate the content)

(2) Program execution process

(3) Local variables

(4) member variables

4.3.3 Enumeration types

(1) Only one of the specified values can be taken

(2) using the enum keyword

(3) is the Java.lang.Enum type

(4) Other uses

package com.cfl.util;import org.apache.commons.collections.map.HashedMap;import java.util.Map;public enum Resouce {_51JOB("51job", 1), ZHILIAN("zhilian", 2), DAJIE("dajie", 3),HIATOU("haitou", 4), LAGOU("lagou", 5), SHIXIZENG("shixiseng", 6);private String name ;private int index ;private Resouce(String name , int index ){    this.name = name ;    this.index = index ;}public String getName() {    return name;}public void setName(String name) {    this.name = name;}public int getIndex() {    return index;}public void setIndex(int index) {    this.index = index;}

}

Partitioning of 4.3.4 data types

Note: (1) Java basic data type length is unaffected by system (2) Float F = 12.3f must be plus f

4.3.5 conversion of the underlying data type

(1) Boolean cannot be converted to and from other types

(2) Large capacity is automatically converted into small capacity. The capacity of small conversion capacity is large, which must be strongly turned, which can cause overflow.

The Java language provides eight basic types. Six types of numbers (four integers (by default, int), two floating-point types (default is double), one character type, and one Boolean type.

Byte

byte数据类型是8位、有符号的,以二进制补码表示的整数;(256个数字),占1字节最小值是-128(-2^7);最大值是127(2^7-1);默认值是0;byte类型用在大型数组中节约空间,主要代替整数,因为byte变量占用的空间只有int类型的四分之一;例子:byte a = 100,byte b = -50。

Short

short数据类型是16位、有符号的以二进制补码表示的整数,占2字节最小值是-32768(-2^15);最大值是32767(2^15 - 1);Short数据类型也可以像byte那样节省空间。一个short变量是int型变量所占空间的二分之一;默认值是0;例子:short s = 1000,short r = -20000。

Int:

int数据类型是32位、有符号的以二进制补码表示的整数;占3字节最小值是-2,147,483,648(-2^31);最大值是2,147,485,647(2^31 - 1);一般地整型变量默认为int类型;默认值是0;例子:int a = 100000, int b = -200000。

Long

long数据类型是64位、有符号的以二进制补码表示的整数;占4字节最小值是-9,223,372,036,854,775,808(-2^63);最大值是9,223,372,036,854,775,807(2^63 -1);这种类型主要使用在需要比较大整数的系统上;默认值是0L;例子: long a = 100000L,int b = -200000L。long a=111111111111111111111111(错误,整数型变量默认是int型)long a=111111111111111111111111L(正确,强制转换)

Float

float数据类型是单精度、32位、符合IEEE 754标准的浮点数;占4字节    -3.4*E38- 3.4*E38。。。浮点数是有舍入误差的float在储存大型浮点数组的时候可节省内存空间;默认值是0.0f;浮点数不能用来表示精确的值,如货币;例子:float f1 = 234.5f。float f=6.26(错误  浮点数默认类型是double类型)float f=6.26F(转换正确,强制)double d=4.55(正确)

Double

double数据类型是双精度、64位、符合IEEE 754标准的浮点数;浮点数的默认类型为double类型;double类型同样不能表示精确的值,如货币;默认值是0.0d;例子:double d1 = 123.4。

Boolean

boolean数据类型表示一位的信息;只有两个取值:true和false;这种类型只作为一种标志来记录true/false情况;默认值是false;例子:boolean one = true。

Char

char类型是一个单一的16位Unicode字符;用 ‘’表示一个字符。。java 内部使用Unicode字符集。。他有一些转义字符  ,2字节最小值是’\u0000’(即为0);最大值是’\uffff’(即为65,535);可以当整数来用,它的每一个字符都对应一个数字

(3) Real number, default is double

(4) integer, default int

4.4 Operators and expressions

(1)

(2) Self-increment, self-reduction

(3) Logical operator---short circuit

(4) Priority level

(5) Three mesh operator

4.5 Branches

(1) If--else

(2) switch

 注意: java 6及以下只能探测int(或自动转为int,如:short)的值,java 7及以上可以探测swString的值
4.6 Cycles 4.6.1 For,while and Do--while

(1) Similarities and differences

for和while是先判断再执行do...while是先执行一次再判断继不继续所以for和while语句块可能一次都不执行,但是do...while至少会执行一次

(2) When the number of cycles is not sure, use while.

(3) Continue has a great influence on while and Do-while

4.6.2 Break and Continue

(1) Break out of the loop, notice that it's not jumping out if

(2) Continue jump out of this cycle, into the next cycle

4.7 Method 4.8 The scope of the variable 4.9 recursively calls 5. The development of object-oriented 5.1 programming language

5.2 Process-oriented design ideas 5.3 Object-oriented design concept 5.4 relationship between objects and class 5.5 classes

(1) Association

Often the parameters of a method in a class are related to another class

(2) Inheritance

(3) Aggregation and combination

(4) Realization of the relationship

5.6 Objects and references

(1) in Java, in addition to the basic types, are called reference types.

5.7 Definition of Java class

(1) Memory parsing

(2) Relationship of classes and objects

5.8 Constructors

(1) Create a new object using the new+ construction method

(2) same name as class, no return value

(3) Initialize class

(4) Execution order

Z

package constructor;public class Z {    Z(){        System.out.println("Z");    }}

A

package constructor;public class A extends Z{    A(){        System.out.println("A");    }        static{        System.out.println("static");    } }

Main

package constructor;public class Main {    public static void main(String[] args) {        A a = new A();     }}

Results

staticZA
5.9 creation and use of objects

(1) Memory parsing

(2) Method overloading

5.10 This keyword 5.11 Package and import

(1) Default reference Java.lang

5.12 Access Control

Inheritance of Class 5.13

(1) Java is a single inheritance

(2) Construction method

--the construction method of the base class must be called during the construction of the child class.

--Sub-class construction method can be used to display calls to the base class construction method, without tuning, the default call no parameter construction method

--The subclass constructor method can be used to display the constructor of the base class, which must be written in the first row of the subclass construction method.

--If the subclass does not explicitly call the constructor of the base class, and the base class does not have a parameterless construction method then an error

5.14 Rewriting of methods

(1) Override to consider access restrictions

5.14 Final Keyword 5.15 object class

(1) is the base class for all Java classes

(2) Equals method (the comparison points to a place)

5.16 Object Transformation

(1) A base class reference can point to its child class

(2) The base class cannot access the newly added members of the subclass

(3) You can use the object instanceof class name to determine whether the object to which the reference variable "points" belongs to the class, or its subclasses

(4) Subclass object when the base class object is used, called upward transformation

(5) When the parent class is used as a subclass object, it is called downward transformation. (cast)

Animal a = new Animal();a = new Dog();//if(a instanceof Dog ){    Dog g = (Dog) a;//强制转换,数据仍在}






5.17 polymorphic

5.18 Abstract class

(1) using the abstract keyword to modify a class, this class is called abstract class; using abstract to modify a method called abstract method

(2) Abstract methods must be declared as abstract classes, and abstract classes must be inherited.

(3) Non-abstract classes (inherited from abstract classes), abstract methods must be overridden, and abstract classes (inherited from abstract classes) can not be rewritten.

(4) Abstract classes cannot be instantiated

(5) Abstract methods only need to be declared, without implementing

5.19 Final keyword

(1) The final value cannot be changed

(2) Final method cannot be rewritten

(3) The final class cannot be inherited

Much more to write a class library

5.20 interface

(1) is a collection of definitions of abstract methods and constant values

This is to avoid duplication of multiple inherited parent class variables, set to static variables so that he does not belong to any class.

(2) A special abstract class that contains only the definitions of constants and methods, and no variables and methods are implemented.

6. Exceptions (Exception)

(1) root cause of all exception classes: Throwable

(2) Error: System exception, unable to process

(3) Exception: can handle catch error

(4) RuntimeException:

-Frequent occurrence, can not capture, such as: by 0 in addition, subscript overflow.

(5) Excetion that must be captured such as IOException

(6) A method with a return value, the return statement in try{}, must be executed finally and then returned.

package exception;public class Main {    public static void main(String[] args) {        int k = f();        System.out.println(k);    }public static int f(){int i = 2 ;int j = 1 ;try{int x = i/j ;return 0 ;}catch(Exception e){e.printStackTrace();}finally{System.out.println("finally");}return 0 ;}

}

Results

finally0

(7) Capturing small anomalies before capturing large

(8) Exceptions and overrides

7. Arrays (Array) 7.1 One-dimensional arrays

(1) Dynamic initialization

int a = new int [3];a[0] = 1 ;a[1] = 2 ;1[2] = 3;

(2) Static initialization

int a = {1,2,3};Date days []{    new Date(1,4,2018),    new Date(1,5,2018)}

(3) Default value

Date days[] = new Date[3];System.out.println(days[0]);

Result: null

7.2 Two-dimensional arrays

(1) Static initialization

int a[][] = new {{1,2,3},{3,4,5,6},{7,8,9}};

(2) The declaration of an array should be in high-to-low-dimensional order (left to right)

8. Base class (Common Class) 8.1 String and StringBuffer

(1) String common methods

(2) Common methods of StringBuffer

(3) Comparison of string and StringBuffer

--stirng is an immutable sequence of characters that is modified when the new is built and is constantly copy

The--stringbuffer is variable and can be modified directly

8.2 Wrapper class 8.3 math and file

(1) Math common methods

(2) File common methods

--Recursively list file tree structure

9. Input and output stream (I/O stream) 9.1 Java streaming input and output principle

9.2 Classification of Java stream classes

9.3 Input/output stream classes

--standing in the procedural angle

9.3.1 InputStream

(1) Organization relationship

(2) Basic methods

9.3.2 OutputStream

(1) Organization relationship

(2) Basic methods

9.3.3 Reader

(1) Organization relationship

(2) Basic methods

9.3.4 Writer

(1) Organization relationship

(2) Basic methods

9.4 Common node flows and processing flows

--The original pipeline, the node stream. On the other pipe flow, called the process flow

(1) Node flow

(2) Process flow

9.5 File Streams

--Example FileInputStream

package io;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IOMain {    public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos = null;try { fis = new FileInputStream("C:/Users/L/Desktop/test2.txt");  fos = new FileOutputStream("C:/Users/L/Desktop/x.txt");  int c ;  while((c=fis.read())!=-1){ fos.write(c); fos.flush(); } System.out.println("ok"); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

--filereader

package io;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;public class IOMain2 {    public static void main(String[] args) {FileReader fis = null;FileWriter fos = null;try { fis = new FileReader("C:/Users/L/Desktop/test2.txt");  fos = new FileWriter("C:/Users/L/Desktop/x.txt");  int c ;  while((c=fis.read())!=-1){ fos.write(c); System.out.println((char)c); fos.flush(); } System.out.println("ok"); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

--bufferedreader

package io;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class IOMain3 {    public static void main(String[] args) {BufferedReader fis = null;BufferedWriter fos = null;try { fis = new BufferedReader(new FileReader("C:/Users/L/Desktop/test2.txt"));  fos = new BufferedWriter(new FileWriter("C:/Users/L/Desktop/y.txt"));  String c ;  while((c=fis.readLine())!=null){ fos.write(c); System.out.println(c); fos.flush(); } System.out.println("ok"); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
9.6 Buffered stream 9.7 data stream 9.8 convert stream 9.9 Print Stream 9.9 Object Flow 10. Container/generics (collection/generic)

--1 1 3 6 1 graphs, 1 classes, 3 knowledge points, 6 interfaces

10.1 Organization diagram

10.2 A class

10.3 6 Interface 10.3.1 Collection interface

10.3.2 Set

--Data objects are not sequential and cannot be duplicated (equals) and can be echoed by the collection

--rewrite equals and hashcode to determine the criteria for judging duplicates

The container class for set in the--JDK API has HashSet, and TreeSet

10.3.3 List

--data objects are ordered and can be duplicated

The container class for list in--JDK API has Arraylist,linklist

10.3.4 Map

--Key value pair storage, key cannot be duplicated (overwrite)

The container class for set in the--JDK API has HashMap, and TreeMap

--Basic methods

--Package, unpack (CAST)

--hashmap principle of implementation (JDK7)

54946424

Similarities and differences of--JDK7 and JDK8 HashMap's realization principle

Http://www.importnew.com/23164.html

--implementation principle of various container implementation classes (also HashMap)

Http://wiki.jikexueyuan.com/project/java-collection/hashmap.html

10.3.5 Iterator

(1) Three methods

--hasnext (), Next (), remove ()

10.3.6 Comparable interface

--Pay attention to implement the comparable interface, by implementing the Comparato method, determine the ordering mode

10.4 Metrics (How to choose a data structure)

--array Read fast Change slow

--linked Read slow change fast

--hash between the two

10.5 3 Points of knowledge

--Enhanced for

--generlic (generic) container declaration when determining the type of an object

--pack, unpack

11. Threading (thread) 11.1 Thread Basic concepts

--thread is a different execution path for a program

(1) Call the Run method directly

No difference from normal method invocation

11.2 Creating and starting a thread

(1) Inherit thread

(2) Implement Runnable interface (recommended)

11.3 Thread scheduling and Priority 11.4 thread state Control 11.4.1 state transition diagram

Basic methods of 11.4.1 threading control

(1) The Sleep method is a static method

Thead.sleep() //当前正在执行的线程睡眠

(2) Join method

11.5 Synchronization of Threads

(1) Synchronized

synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。它包括两种用法:synchronized 方法和 synchronized 块。Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍可以访问该object中的非加锁代码块。

(2) Wait () needs to use synchronized to let the thread that accesses this object (the thread that gets the lock) wait, release the lock

(3) Notify () General and wait () pair use, wake up a thread in this object sleep.

--producerconsumer

package thread;public class ProducerConsumer {    public static void main(String[] args) {        Repository re = new Repository();        Producer p[] = new Producer[4];        Customer c[] = new Customer[2];for (int i = 0; i < p.length; i++) {p[i] = new Producer(re);new Thread(p[i]).start();}for (int i = 0; i < c.length; i++) {c[i] = new Customer(re);new Thread(c[i]).start();}}

}--book

class Book {    int id ;        public Book(int id) {        super();        this.id = id;    }        @Override    public String toString() {        return "book : " + id;    }

}

--repository

class Repository {int index = 0 ;Book[] book = new Book[5];public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public Book[] getBook() {return book;}public void setBook(Book[] book) {this.book = book;}public synchronized void push(Book b){while(index==book.length){try {System.out.println("仓库满了,等待");this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}this.notifyAll();book[index] =  b;index++;}  public synchronized Book pop(){Book b ;while(index == 0){try {System.out.println("  仓库没有书,等待");this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}this.notifyAll();System.out.println("index"+index);index--;b = book[index];return b;}

}

--producer

class Producer implements Runnable{Repository repository = null;public Producer(Repository repository) {this.repository = repository;}@Overridepublic void run() {for (int i = 0; i < 2000; i++) {Book b = new Book(i);repository.push(b);System.out.println("生产了"+b+"   仓库目前书的数量:" + repository.getIndex());try {Thread.sleep((long) (1000*Math.random()));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

--customer

class Customer implements Runnable{Repository repository = null;public Customer(Repository repository) {super();this.repository = repository;}@Overridepublic void run() {for (int i = 0; i < 2000; i++) {Book b = repository.pop();System.out.println("消费了"+b+"  仓库目前书的数量:" + repository.getIndex());try {Thread.sleep((long) (1000*Math.random()));} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

}

Attention:

while(index==book.length){try {System.out.println("仓库满了,等待");this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

To use while without if, or wake up, will not judge whether Index==book.lengt, but continue to execute.

12. Network Programming (TCP/UDP)

--Network programming! = website Programming

12.1 Network Foundation 12.2 TCP/IP Protocol 12.3 address 12.4 socket Communication

(1) tcp/udp

GUI14. Meta-DATA15 Regular expressions
Import java.io.*;import java.util.scanner;import java.util.regex.*;p ublic class Example {public static void main (Stri                        ng args []) {Scanner read = new Scanner (system.in);                        SYSTEM.OUT.PRINTLN ("Please input String:"); /********************** * x+ means x appears 1 or more times * x? Indicates that there are 0 or 1 times * \56 indicates that the decimal point * x{n} indicates exactly n times * x{n,} indicates X occurrences >=n * XY representation The suffix of X is y * x|  Y means x or y * \\p{alpha} denotes the letter * \\w denotes the character that can be used for identifiers * [A-z&&[def]] for any of the D E F            (cross) * [A-F&AMP;&AMP;[^BC]] represents ADEF's Difference * * \\d represents any number in 0-9 */  String s = "(http://|www) \56?\\w+\56{1}\\w+\56{1}\\p{alpha}+";   Filter URL//String s = "[ABC]"; [] denotes any one//String s = "[a-d]"; A-d represents a//example from a to D in the String s = "[0-9]+\56? [0-9]*"; Filter out the floating-point number in the string//string s = "1{3,4}"; Note Greedy match, input 1111 111 first time 1111 second time 111//String s = "a1*";                        Note Greedy match, input 1111 111 first time 1111 second time 111 String s = "p=\\{+.+\\}+";   Pattern pattern = Pattern.compile (s);  Match condition Matcher Matcher = Pattern.matcher (Read.nextline ());                The target string to match while (true) {if (Matcher.find ())                                        {System.out.printf ("%s can find!,%d--%d\n", Matcher.group (), Matcher.start (), Matcher.end ());                Break                    } else {break;        }}//System.out.println ("Number of Letters:" +n); }                      }

Javase Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.