java基础我爱编程

Lang-Java

2016-01-19  本文已影响348人  freenik

个人笔记,方便自己查阅使用

Contents

Java Lang

Assignment, Reference

Java引用变量赋值

Box b1 = new Box();

Box b2 = b1;

System.out.println(b1==b2);//true

你可能认为b1和b2引用的是不同的对象,但实际b1和b2引用同样的对象。将b1赋值给b2并没有分配任何内存或对原对象做任何部分的拷贝。它们指向同一个对象,因此对变量b2的改变也将影响b1。

尽管b1和b2都引用同一个对象,但是他们之间没有任何其他的关系。例如,接下来对b1的赋值仅仅使b1脱离初始对象,而没有影响对象或影响b2。

Box b1 = new Box();

Box b2 = new Box();

b2 = b1;

// ...

b1 = null;

这里,b1被设置为空,但是b2仍然指向原来的对象。

当你将一个对象引用赋值给另一个对象引用时,你并没有创建该对象的一个拷贝,而是仅仅对该对象的引用的一个拷贝。

Data types & Structures

Array (Arrays.)

(Arrays.)

ArrayList

Character

Collection: java.util.Collection

List

Set

Queue

Queue接口与List、Set同一级别,都是继承了Collection接口。

java中queue的使用

jenkov Java Collections - Queue

Since Queue is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Queue implementations in the Java Collections API:

java.util.LinkedList
java.util.PriorityQueue

LinkedList is a pretty standard queue implementation.

Queue queueA = new LinkedList();
Queue queueB = new PriorityQueue();

Map

Summary

String

String pool

StringBuilder

HashMap

HashMap详细介绍(源码解析)和使用示例

Queue

Blocking Queue

Priority Queue

TreeMap

TreeMap详细介绍(源码解析)和使用示例

Date & Locale

SimpleDateFormat

Garbage Colletion

File I/O

Read

Write

Functions

Sorting

Regex

Matching

java.util.regex包主要包括以下三个类:

Pattern类:

pattern对象是一个正则表达式的编译表示。Pattern类没有公共构造方法。要创建一个Pattern对象,你必须首先调用其公共静态编译方法,它返回一个Pattern对象。该方法接受一个正则表达式作为它的第一个参数。
Matcher类:

Matcher对象是对输入字符串进行解释和匹配操作的引擎。与Pattern类一样,Matcher也没有公共构造方法。你需要调用Pattern对象的matcher方法来获得一个Matcher对象。
PatternSyntaxException:

PatternSyntaxException是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

String.replaceAll()

  1. in java: whenever you need regex in a function/method, you would give it a string,
    // the regex would look like the string after string escaped.
    // e.g.: String s2 = s.replaceAll("\W",""); // correct, you want to use \W to denote all non-word chars
    // String s2 = s.replaceAll("\W",""); // incorrect, \W is wrong escaping
    // http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
    // http://www.runoob.com/java/java-regular-expressions.html
    //
  2. \W => [^A-Za-z0-9_] rather than wholy nonalphanumeric
  3. String does not have .reverse() while StringBuilder does
  4. String's replace(), replaceAll(), toLowerCase() and others do not change the original string

Structures-Lang

generic arguments

You can't use primitive types as generic arguments in Java. E.G:

Map<String, Integer> myMap = new HashMap<String, Integer>(); // correct
HashMap<String, int> // incorrect

Loop

Java 增强循环

for(声明语句 : 表达式)
{
//
}

for(int x : numbers )

OOP

Class

Nested Class

Static Class

Objects

Cloning/ Copy (Shallow/Deep)

A shallow copy just copies the values of the references in the class. A deep copy copies the values.

Scope

Topics ==> Libs/Frameworks

Installation & Deployment Issues

Mac

Compilation&Packaging&Distribution

javac -classpath hadoop-common-2.4.0-amzn-3.jar:hadoop-mapreduce-client-core-2.4.0-amzn-3.jar:hadoop-mapreduce-client-common-2.4.0-amzn-3.jar:hadoop-annotations-2.4.0.jar:commons-cli-1.2.jar:hbase-0.94.18.jar -d lm_classes LangModel.java

jar -cvf lm.jar -C lm_classes/ .

指定用以查找类或接口定义的源代码路径。与用户类路径一样,源路径项用分号 (;) 进行分隔,它们可以是目录、JAR 归档文件或 ZIP 归档文件。如果使用包,那么目录或归档文件中的本地路径名必须反映包名。

注意:通过类路径查找的类,如果找到了其源文件,则可能会自动被重新编译。

JIT (vs. conventional(AOT) compiler, interpreter)

Concurrency

volatile

Nonblocking & CAS

Java Concurrency Tutorial

Java Concurrency Tutorial

Synchronization (Synchronized)

Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

Synchronization can introduce CONTENTION.

The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements.

Guarded Blocks

Blocking Queue

ConcurrentHashMap

Lock

Database Access: JDBC

Concepts:

Connection Pools

c3p0

C3P0 is a library for augmenting traditional (DriverManager-based) JDBC drivers with JNDI-bindable DataSources, including DataSources that implement Connection and Statement Pooling, as described by the jdbc3 spec and jdbc2 std extension.

calls for mchange-commons-java

Usage

Graphics

Displaying graphics in swing

    import java.awt.*;  
    import javax.swing.JFrame;  
      
    public class DisplayGraphics extends Canvas{  
          
        public void paint(Graphics g) {  
            g.drawString("Hello",40,40);  
            setBackground(Color.WHITE);  
            g.fillRect(130, 30,100, 80);  
            g.drawOval(30,130,50, 60);  
            setForeground(Color.RED);  
            g.fillOval(130,130,50, 60);  
            g.drawArc(30, 200, 40,50,90,60);  
            g.fillArc(30, 130, 40,50,180,40);  
              
        }  
            public static void main(String[] args) {  
            DisplayGraphics m=new DisplayGraphics();  
            JFrame f=new JFrame();  
            f.add(m);  
            f.setSize(400,400);  
            //f.setLayout(null);  
            f.setVisible(true);  
        }  
      
    }  
image.png

graphics是Java提供的用于绘图和显示格式化文字的工具。绘图必须在一个窗口(容器)中进行

Container类是java.awt.Component类的子类,JComponent类又继承自Container类。因此,JComponent类是AWT和Swing的联系之一。
除了Swing顶层容器类(top level containers)以外,其余所有的Swing组件类都继承自JComponent类(例如JPanel),如前所述,JComponent类是Container类的子类,因此,所有的Swing组件都可作为容器使用。
Swing顶层容器类包括了JFrame、JDialog、JApplet、JWindow,它们为其他的Swing组件提供了绘制自身的场所。

graphics是一个抽象类,其实现大都是平台相关的,所以不容易自己创建一个graphics实例。一般graphics的实例会由依照你所在的桌面环境给出。Graphics类及其子类Graphics2D提供的只是一些基本绘图方法,比如画直线、曲线什么的。所以做一个图形组件的基本思路可以总结为以下过程:
选择适合的基本图形组件->继承它->重写paint等方法->在需要刷新图形的时候调用repaint等方法

Swing是一个高层的GUI系统,而不像AWT那样与运行平台技术更加靠近的系统。Swing的类继续关系比AWT要复杂的多,而且Swing类大多都经过了中间的转接类-JComponent。而我们常用的JFrame则另辟蹊径,从awt的window继续了下来。
这种结构关系决定了Swing的庞大与复杂性。很多初学者都难以理解Swing的模式和结构。
Swing 中的控件都是利用Java图形功能绘制出来的,而不是对应到平台的一个具体控件实现。我们所用的所有Swing控件都是直接或者间接用Graphics绘制出来的,这种实现方式最大的好处是很灵活,我们想要什么样的控件,就直接用Graphics绘制出来就是了。
Sun之所以用这种方式来实现,是为了在不牺牲移植性的基础上加入丰富的界面交互功能。

但是缺点也很明显:Swing的速度和效率是所有GUI(图形用户界面Graphical User Interface)系统中最慢的。

Graphics 类相当于一个画布,每个 Swing 组件都通过 Graphics 对象来绘制显示。

Hadoop

HBase

Manager: Maven

Tutorials

Refs

Notes

Net

Http

java.net.HttpUrlConnection

Threading

Executor

ExecutorService的execute和submit方法

Runnable Interface/Thread 继承

ThreadPoolExecutor & Executor Service

Executor service

ThreadPoolExecutor

几种排队的策略:

  1. 直接提交。缓冲队列采用 SynchronousQueue,它将任务直接交给线程处理而不保持它们。如果不存在可用于立即运行任务的线程(即线程池中的线程都在工作),则试图把任务加入缓冲队列将会失败,因此会构造一个新的线程来处理新添加的任务,并将其加入到线程池中。直接提交通常要求无界 maximumPoolSizes(Integer.MAX_VALUE) 以避免拒绝新提交的任务。newCachedThreadPool采用的便是这种策略。
  2. 无界队列。使用无界队列(典型的便是采用预定义容量的 LinkedBlockingQueue,理论上是该缓冲队列可以对无限多的任务排队)将导致在所有 corePoolSize 线程都工作的情况下将新任务加入到缓冲队列中。这样,创建的线程就不会超过 corePoolSize,也因此,maximumPoolSize 的值也就无效了。当每个任务完全独立于其他任务,即任务执行互不影响时,适合于使用无界队列。newFixedThreadPool采用的便是这种策略。
  3. 有界队列。当使用有限的 maximumPoolSizes 时,有界队列(一般缓冲队列使用ArrayBlockingQueue,并制定队列的最大长度)有助于防止资源耗尽,但是可能较难调整和控制,队列大小和最大池大小需要相互折衷,需要设定合理的参数。

Future & FutureTask

JRuby's threadpooling

Time

Get Time

Delay

Vert.x

Notice:

Concurrency of Vert.x

SQL

Web Server - Tomcat

Java EE

Servlet

Deployment: - Tomcat

上一篇 下一篇

猜你喜欢

热点阅读