java vamei_Java快速教程--vamei 学习笔记(进阶篇)

感谢vamei,学习链接:http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html

Java进阶01 String类

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/08/3000914.html

字符串操作

--------------------------------------------------------------------------------------------------------

可以用+实现字符串的连接(concatenate),比如:

"abc" + s

字符串的操作大都通过字符串的相应方法实现,比如下面的方法:

方法                 效果

s.length()              返回s字符串长度

s.charAt(2)           返回s字符串中下标为2的字符

s.substring(0, 4)          返回s字符串中下标0到4的子字符串

s.indexOf("Hello")          返回子字符串"Hello"的下标

s.startsWith(" ")           判断s是否以空格开始

s.endsWith("oo")            判断s是否以"oo"结束

s.equals("Good World!")       判断s是否等于"Good World!"

==只能判断字符串是否保存在同一位置。需要使用equals()判断字符串的内容是否相同。

s.compareTo("Hello Nerd!")    比较s字符串与"Hello Nerd!"在词典中的顺序,

返回一个整数,如果<0,说明s在"Hello Nerd!"之前;

如果>0,说明s在"Hello Nerd!"之后;

如果==0,说明s与"Hello Nerd!"相等。

s.trim()              去掉s前后的空格字符串,并返回新的字符串

s.toUpperCase()           将s转换为大写字母,并返回新的字符串

s.toLowerCase()        将s转换为小写,并返回新的字符串

s.replace("World", "Universe")    将"World"替换为"Universe",并返回新的字符串

Java进阶02 异常处理

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/09/3000894.html

异常处理是在解决问题,同时也是在制造问题。大型项目中,过多、过细的异常处理往往会导致程序变得一团糟。异常处理的设计并不简单,并需要谨慎使用。

packagetest;public classTest

{public static voidmain(String[ ] args)

{

Battery aBattery= newBattery();

aBattery.chargeBattery(0.5);

aBattery.useBattery(-0.5);

}

}classBattery

{/*** increase battery*/

public void chargeBattery(doublep)

{//power <= 1

if (this.power + p < 1.)

{this.power = this.power +p;

}else{this.power = 1.;

}

}/*** consume Battery*/

public boolean useBattery(doublep)

{try{

test(p);

}catch(Exception e)

{

System.out.println("catch Exception");

System.out.println(e.getMessage());

p= 0.0;

}if(this.power >=p)

{this.power = this.power -p;return true;

}else{this.power = 0.0;return false;

}

}/*** test usage*/

private void test(double p) throws Exception //I just throw.don't handle

{if (p < 0)

{

Exception e= new Exception("p must be positive");throwe;

}

}private double power = 0.0; //percentage of battery

}

Java进阶03 IO基础

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/11/3000905.html

packagetest;import java.io.*;public classTest

{public static voidmain(String[] args)

{try{

BufferedReader br=

new BufferedReader(new FileReader("file.txt"));

String line=br.readLine();while (line != null) {

System.out.println(line);

line=br.readLine();

}

br.close();

}catch(IOException e) {

System.out.println("IO Problem");

}

}

}

Hello World!

Hello Nerd!

e454668a47b4e16a492098f22bc499b9.png

packagetest;import java.io.*;public classTest

{public static voidmain(String[] args)

{try{

String content= "Thank you for your fish.";

File file= new File("new.txt");//create the file if doesn't exists

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw= newFileWriter(file.getAbsoluteFile());

BufferedWriter bw= newBufferedWriter(fw);

bw.write(content);

bw.close();

}catch(IOException e) {

System.out.println("IO Problem");

}

}

}

38761e78d2ba557d5e1c1b48b4db810c.png

Java进阶04 RTTI

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/14/3013985.html

运行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制,在Java运行时,RTTI维护类的相关信息。

多态(polymorphism)是基于RTTI实现的。RTTI的功能主要是由Class类实现的。

packagetest;public classTest

{public static voidmain(String[] args)

{

Human aPerson= newHuman();

Class c1=aPerson.getClass();

System.out.println(c1.getName());

Human anotherPerson= newWoman();

Class c2=anotherPerson.getClass();

System.out.println(c2.getName());//Class c3 = Class.forName("Human");//System.out.println(c3.getName());

/** 这句有问题, Class c3 = Class.forName("Human");*/Class c4= Woman.class;

System.out.println(c4.getName());

}

}classHuman

{/*** accessor*/

public intgetHeight()

{return this.height;

}/*** mutator*/

public void growHeight(inth)

{this.height = this.height +h;

}private intheight;

}class Woman extendsHuman

{/*** new method*/

publicHuman giveBirth()

{

System.out.println("Give birth");return (newHuman());

}

}

Java进阶05 多线程

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html

多线程(multiple thread)是计算机实现多任务并行处理的一种方式。

在单线程情况下,计算机中存在一个控制权,并按照顺序依次执行指令。单线程好像是一个只有一个队长指挥的小队,整个小队同一个时间只能执行一个任务。

在多线程情境下,计算机中有多个控制权。多个控制权可以同时进行,每个控制权依次执行一系列的指令。多线程好像是一个小队中的成员同时执行不同的任务。

在多线程编程中,要尽力避免竞争条件(racing condition),即运行结果依赖于不同线程执行的先后。线程是并发执行的,无法确定线程的先后,所以我们的程序中不应该出现竞争条件。

创建线程

packagetest;public classTest

{public static voidmain(String[] args)

{

NewThread thread1= newNewThread();

NewThread thread2= newNewThread();

thread1.start();//start thread1

thread2.start(); //start thread2

}

}/*** create new thread by inheriting Thread*/

class NewThread extendsThread

{private static int threadID = 0; //shared by all

/*** constructor*/

publicNewThread()

{super("ID: " + (++threadID));

}/*** convert object to string*/

publicString toString()

{return super.getName();

}/*** what dors the thread do?*/

public voidrun()

{

System.out.println(this);

}

}

Runnable

packagetest;public classTest

{public static voidmain(String[] args)

{

Thread thread1= new Thread(new NewThread(), "first");

Thread thread2= new Thread(new NewThread(), "second");

thread1.start();//start thread1

thread2.start(); //start thread2

}

}/*** create new thread by implementing Runnable*/

class NewThread implementsRunnable

{/*** convert object to string*/

publicString toString()

{returnThread.currentThread().getName();

}/*** what dors the thread do?*/

public voidrun()

{

System.out.println(this);

}

}

售票程序

3个售票亭(Booth)共同售卖100张票(Reservoir)。每个售票亭要先判断是否有余票,然后再卖出一张票。如果只剩下一张票,在一个售票亭的判断和售出两个动作之间,另一个售票亭卖出该票,那么第一个售票亭(由于已经执行过判断)依然会齿形卖出,造成票的超卖。为了解决该问题,判断和售出两个动作之间不能有“空隙”。也就是说,在一个线程完成了这两个动作之后,才能有另一个线程执行。

在Java中,我们将共享的资源置于一个对象中,比如下面r(Reservoir)对象。它包含了总共的票数;将可能造成竞争条件的,针对共享资源的操作,放在synchronized(同步)方法中,比如下面的sellTicket()。synchronized是方法的修饰符。在Java中,同一对象的synchronized方法只能同时被一个线程调用。其他线程必须等待该线程调用结束,(余下的线程之一)才能运行。这样,我们就排除了竞争条件的可能。

在main()方法中,我们将共享的资源(r对象)传递给多个线程:

packagetest;public classTest

{public static voidmain(String[] args)

{

Reservoir r= new Reservoir(100);

Booth b1= newBooth(r);

Booth b2= newBooth(r);

Booth b3= newBooth(r);

}

}/*** contain shared resource*/

classReservoir

{private inttotal;public Reservoir(intt)

{this.total =t;

}/*** Thread safe method

* serialize access to Booth.total*/

public synchronized booleansellTicket()

{if(this.total > 0)

{this.total = this.total -1;return true; //successfully sell one ticket

}else{return false; //no more tickets

}

}

}/*** create new thread by inheriting Thread*/

class Booth extendsThread

{private static int threadID = 0; //owned by Class object

private Reservoir release; //sell this reservoir

private int count = 0; //owned by this thread object

/*** constructor*/

publicBooth(Reservoir r)

{super("ID: " + (++threadID));this.release = r; //all threads share the same reservoir

this.start();

}/*** convert object to string*/

publicString toString()

{return super.getName();

}/*** what does the thread do?*/

public voidrun()

{while(true)

{if(this.release.sellTicket())

{this.count = this.count + 1;

System.out.println(this.getName() + ": sell 1");try{

sleep((int) Math.random()*100); //random intervals

}catch(InterruptedException e)

{throw newRuntimeException(e);

}

}else{break;

}

}

System.out.println(this.getName() + "I sold: " +count);

}

}

Java进阶06 容器

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/15/3000913.html

packagetest;import java.util.*;public classTest

{public static voidmain(String[] args)

{//Human[] persons = new Human[2];//array size 2//persons[0] = new Human(160);//persons[1] = new Human(170);

int[] a = {1, 2, 3, 7, 9}; //array size 5

System.out.println(a[2]);

String[] names= {"Tom", "Jerry", "Luffy"}; //array size 3

System.out.println(names[0]);int[] aFrom = {1, 2, 3, 7, 9}; //array size 5

int[] aTo = new int[3];

System.arraycopy(aFrom,1, aTo, 0, 3);

System.out.println(aTo[1]);

System.out.println("表(List)");

List l1 = new ArrayList();

l1.add("good"); //good

l1.add("bad"); //good -> bad

l1.add("shit"); //good -> bad -> shit

l1.remove(0); //bad -> shit

System.out.println(l1.get(1));

System.out.println(l1.size());

System.out.println("集合(set)");

Set s1 = new HashSet();

s1.add(4); //[4]

s1.add(5); //[4,5]

s1.add(4); //[4,5]

s1.remove(5); //[4]

System.out.println(s1);

System.out.println(s1.size());

System.out.println("iterator()方法");

List l2 = new ArrayList();

l2.add(4);

l2.add(5);

l2.add(2);

Iterator i=l2.iterator();while(i.hasNext()) {

System.out.println(i.next());

}

System.out.println("Map是键值对的集合");

Map m1 = new HashMap();

m1.put("Vamei", 12);

m1.put("Jerry", 5);

m1.put("Tom", 18);

System.out.println(m1.get("Vamei"));/** Map还提供了下面的方法,来返回一个Collection:

* keySet() 将所有的键转换为Set

* values() 将所有的值转换为List*/}

}

0a0520437a3cddd9f71f3370331de73c.png

Java进阶07 嵌套类

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/16/3000896.html

内部类

packagetest;public classTest

{public static voidmain(String[] args)

{

Human me= new Human("Vamei");

Human him= new Human("Jerry");

Human.Cup myFirstCup= me.newCup();

Human.Cup mySecondCup= me.newCup();

Human.Cup hisCup= him.newCup();

System.out.println(myFirstCup.whosCup());

System.out.println(mySecondCup.whosCup());

System.out.println(hisCup.whosCup());

}

}classHuman

{/*** inner class*/

classCup

{publicString whosCup()

{return name; //access outer field

}

}/*** constructor*/

publicHuman(String n)

{this.name =n;

}public voidchangeName(String n)

{this.name =n;

}privateString name;

}

嵌套static类

packagetest;public classTest

{public static voidmain(String[] args)

{

Human.Mongolian him= newHuman.Mongolian();

him.Shout();

}

}classHuman

{/*** nested class*/

static classMongolian

{public voidShout()

{

System.out.println("Oh...Ho...");

}

}

}

Java进阶08 GUI

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/17/3000908.html

GUI(Graphical User Interface)提供了图形化的界面,允许用户以图形的方式与系统进行互动。

java布局方式:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

packagetest;import javax.swing.*;import java.awt.*;public classTest {private static voidcreateAndShowGUI() {

JFrame frame= new JFrame("HelloWorld");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Pane's layout

Container cp =frame.getContentPane();

cp.setLayout(newFlowLayout());//create button

JButton b1 = new JButton("click me");

JButton b2= new JButton("shit");//add buttons

cp.add(b1);

cp.add(b2);//show the window

frame.pack();

frame.setVisible(true);

}public static voidmain(String[] args) {

Runnable tr= newRunnable() {public voidrun() {

createAndShowGUI();

}

};

javax.swing.SwingUtilities.invokeLater(tr);

}

}

121e4b0b37bf7bc074aba65a918d4ca0.png

Java进阶09 事件响应

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/23/3000911.html

packagetest;import javax.swing.*;import java.awt.event.*;import java.awt.*;public classTest {private static voidcreateAndShowGUI() {

JFrame frame= new JFrame("Hello");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Pane's layout

Container cp =frame.getContentPane();

cp.setLayout(newFlowLayout());//add interactive panel to Content Pane

cp.add(newButtonPanel());//show the window

frame.pack();

frame.setVisible(true);

}public static voidmain(String[] args) {

Runnable tr= newRunnable() {public voidrun() {

createAndShowGUI();

}

};

javax.swing.SwingUtilities.invokeLater(tr);

}

}/*** JPanel with Event Handling*/

class ButtonPanel extendsJPanel

{publicButtonPanel()

{

JButton yellowButton= new JButton("Yellow");

JButton redButton= new JButton("Red");this.add(yellowButton);this.add(redButton);/*** register ActionListeners*/ColorAction yellowAction= newColorAction(Color.yellow);

ColorAction redAction= newColorAction(Color.red);

yellowButton.addActionListener(yellowAction);

redButton.addActionListener(redAction);

}/*** ActionListener as an inner class*/

private class ColorAction implementsActionListener

{publicColorAction(Color c)

{

backgroundColor=c;

}/*** Actions*/

public voidactionPerformed(ActionEvent event)

{

setBackground(backgroundColor);//outer object, JPanel method

repaint();

}privateColor backgroundColor;

}

}

5e03533d8ea00b58972e78f4bafb0a9d.png     

00f67791e0523f873481cd8f9da62076.png

Java进阶10 内存管理与垃圾回收

学习链接:http://www.cnblogs.com/vamei/archive/2013/04/28/3048353.html

JVM的垃圾回收是多种机制的混合。JVM会根据程序运行状况,自行决定采用哪种垃圾回收。

我们先来了解"mark and sweep"。这种机制下,每个对象将有标记信息,用于表示该对象是否可到达。当垃圾回收启动时,Java程序暂停运行。JVM从根出发,找到所有的可到达对象,并标记(mark)。随后,JVM需要扫描整个堆,找到剩余的对象,并清空这些对象所占据的内存。

另一种是"copy and sweep"。这种机制下,堆被分为两个区域。对象总存活于两个区域中的一个。当垃圾回收启动时,Java程序暂停运行。JVM从根出发,找到可到达对象,将可到达对象复制到空白区域中并紧密排列,修改由于对象移动所造成的引用地址的变化。最后,直接清空对象原先存活的整个区域,使其成为新的空白区域。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值