JavaNote6

class Life{   
    public static void main(String[] args){
        int alive;
        while(alive)
        {
            eat();  
            sleep();
            code();
            repeat();
        }
    }
}

老人说,这不丢脸,也无损真正的骄傲.

Java常用类库
1.1 StringBuffer类(重点)
String是一个字符串,但是字符串的内容不可改变,改变的只是内存地址的指向,那么如果现在想要让字符串的内容可以修改,则必须使用StringBuffer类完成。

1.1.1 StringBuffer的基本使用

在String中使用“+”号进行字符串的连接,在StringBuffer中使用append()方法进行字符串的连接。在使用StringBuffer是不能像String那样可以直接使用字符串进行复制操作,必须通过关键字new开辟对象才可以使用,之后使用append()方法。

package com.java.stringBufferDemo;

public class StringBufferDemo01 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    stringBuffer.append("Hello");

    stringBuffer.append(" ").append("World").append(" ").append("!!!");

    System.out.println(stringBuffer);

}

}

在程序中就只有简单的将“+”替换成了append()方法而已,但是需要说明的是,String与StringBuffer没有直接的关系。所以如果想将StringBuffer类型的对象转换成String类型,就必须使用toString()方法完成。

package com.java.stringBufferDemo;

public class StringBufferDemo02 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    stringBuffer.append("Hello");

    stringBuffer.append(" ").append("World").append(" ").append("!!!");

    String string =   stringBuffer.toString();

    System.out.println(string);

}

}

实际上,一般情况下StringBuffer问问都用在频繁修改字符串的地方。

package com.java.stringBufferDemo;

public class StringBufferDemo02 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    fun(stringBuffer);

    for (int i = 1; i <= 1000;   i++) {

        if(i % 10 == 1){

            stringBuffer.append("\n");

        }

        stringBuffer.append(i   + "\t");

    }

    System.out.println(stringBuffer);

}

public static void fun(StringBuffer   stringBuffer) {

    stringBuffer.append("Hello").append("\n");

}

}

1.1.2 StringBuffer的常用方法

在StringBuffer中提供了大量的操作方法,有些方法就是雨String类似的关键是查看那些新增加的方法。

  1. 在指定位置添内容:
    ·默认情况下所有的内容都是在StringBuffer的最后添加的。
    ·使用public StringBuffer insert(int offset, char c)方法
    package com.java.stringBufferDemo;

public class StringBufferAPIDemo01 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    stringBuffer.append("Hello");

    stringBuffer.append(" ").append("World").append(" ").append("!!!");

    stringBuffer.insert(1,   "***");

    stringBuffer.insert(0,   "+++");

    System.out.println(stringBuffer);

}

}

  1. 删除指定范围的内容:
    ·使用public StringBuffer delete(int start, int end)方法
    package com.java.stringBufferDemo;

public class StringBufferAPIDemo02 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    stringBuffer.append("Hello");

    stringBuffer.append(" ").append("World").append(" ").append("!!!");

    stringBuffer.insert(1,   "***");

    stringBuffer.insert(0,   "+++");

    System.out.println(stringBuffer);

    System.out.println(stringBuffer.delete(0,   9));

}

}

  1. 反转字符串的内容:
    ·使用public StringBuffer reverse()方法
    package com.java.stringBufferDemo;

public class StringBufferAPIDemo02 {

public static void main(String[] args) {

    StringBuffer   stringBuffer = new StringBuffer();

    stringBuffer.append("Hello");

    stringBuffer.append(" ").append("World").append(" ").append("!!!");

    System.out.println(stringBuffer.reverse()); //  将内容翻转

}

}

1.2 Runtime类
每一个JVM启动的时候伺机上都对应了一个Runtime实例,表示运行时,但是此类的构造方法被私有化,则此类的内部肯定有一个方法可以取得本类的实例。

·静态方法:public static Runtime getRuntime()
1.2.1 Runtime与Process

实际上可以通过Runtime类直接运行本机程序。

·方法:public Process exec(String command) throws IOException
范例:运行本机上的计算器程序

package com.java.runtimeDemo;

public class RuntimeDemo01 {

public static void main(String[] args) throws Exception {

    Runtime runtime =   Runtime.getRuntime(); // 单例设计

    runtime.exec("calc"); // 执行程序

}

}

可以让指定的程序进行自动的关闭。exec()方法返回一个Process类的实例,表示一个进程的队形,如果想要关闭,则使用此类中的:public abstract void destroy()

package com.java.runtimeDemo;

public class RuntimeDemo02 {

public static void main(String[] args) throws Exception {

    Runtime runtime =   Runtime.getRuntime(); // 单例设计

    Process process =   runtime.exec("calc"); // 执行程序

    Thread.sleep(2000); //  程序运行2秒

    process.destroy();  //  关闭进程

}

}

1.2.2 Runtime取得系统信息

在Runtime类中可以通过maxMemory()、freeMemory()、totalMemory()取得系统的内存信息,之后也可以通过gc()方法强制性的程序释放掉所有的垃圾空间。

package com.java.runtimeDemo;

public class RuntimeDemo03 {

public static void main(String[] args) throws Exception {

    Runtime runtime =   Runtime.getRuntime(); // 单例设计

    System.out.println("1.最大可用内存:" + runtime.maxMemory());

    System.out.println("1.空闲内存空间:" +   runtime.freeMemory());

    System.out.println("1.总共内存空间:" +   runtime.totalMemory());

    String string = "";

    for(int i = 0; i <= 10000;   i++){

        string += i;    //  会产生大量的垃圾

    }

    System.out.println("2.空闲内存空间:" +   runtime.freeMemory());

    runtime.gc();   //  运行垃圾回收

    System.out.println("3.空闲内存空间:" +   runtime.freeMemory());

}

}

运行结果:

1.最大可用内存:933953536

1.空闲内存空间:62319360

1.总共内存空间:62980096

=========================

2.空闲内存空间:305506256

=========================

3.空闲内存空间:390997696

1.3 System类(了解)
System是一个系统类,是一个陪伴我们时间最长的类,例如:System.out.println()就是此类中提供的操作。

1.3.1 取得计算时间

使用此类可以取得计算的时间,例如,想知道一个程序执行时一共花费了多少时间。

·得到当前的时间:public static long currentTimeMillis()

package com.java.systemDemo;

public class SystemDemo01 {

public static void main(String[] args) {

    long begin = System.currentTimeMillis();

    String string = "";

    for (int i = 0; i <= 10000;   i++) {

        string += i; // 会产生大量的垃圾

    }

    long end = System.currentTimeMillis();

    System.out.println("程序执行的时间:" + (end - begin) + "ms");

}

}

1.3.2 System与垃圾回收

在System类中有一个gc()方法:public static void gc(),运行垃圾回收器。调用 System.gc() 实际上等效于调用: Runtime.getRuntime().gc()

如果一个对象不用的话,则就有可能进行垃圾的回收,但是如果一个对象在被回收之前需要做一些收尾的工作。又该怎么办呢???

在Object类中存在一个方法,此方法在对象被回收前调用:

·protected void finalize() throws Throwable

package com.java.systemDemo;

public class SystemDemo02 {

public static void main(String[] args) throws Throwable {

    Person person = new Person("Harry", 80);

    person.finalize();

    person = null;

    System.gc();

}

}

class Person {

private String name;

private int age;

public Person(String name, int age) {

    this.name = name;

    this.age = age;

}

public String toString() {

    return "name:" + this.name + ",age:" + this.age;

}

protected void finalize() throws Throwable {

    System.out.println("我被回收了。。。" + "(" + this + ")");

}

}

但是从一把开发的角度来讲,很少有专门的开发在对象的回收前进行操作的,所以此操作理解即可。

1.4 日期操作类(重点)
在Java中对日期的显示进行了很好的支持,使用Date、DateFormat、SimpleDateFormat、Calendar等类都可以取得日期。

1.4.1 Data类

java.util.Date是一个由系统提供的日期操作类,此类的操作非常简单,同时也是一个非常很总要的类,在日后的很多开发中,都会使用到此类。

package com.java.dateDemo;

import java.util.Date;

public class DateDemo {

public static void main(String[] args) {

    System.out.println(new Date());

}

}

日期:

Wed Aug 08 17:50:48 CST 2012

此时日期已经可以正常显示了,但是日期的显示格式并不符合国人的习惯。中国人的习惯更倾向于:

2012-08-08 17:50 星期三

1.4.2 Calendar类

通过此类可以讲时间精确到毫秒,Calendar类的定义:

public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable
此类是一个抽象类,则使用时必须依靠其子类:

package com.java.dateDemo;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class CalendarDemo {

public static void main(String[] args) {

    Calendar calendar =   new GregorianCalendar();

    System.out.println("年: " +   calendar.get(Calendar.YEAR));

    System.out.println("月: " +   (calendar.get(Calendar.MONTH) + 1));

    System.out.println("日: " +   calendar.get(Calendar.DAY_OF_MONTH));

    System.out.println("时: " +   calendar.get(Calendar.HOUR_OF_DAY));

    System.out.println("分: " +   calendar.get(Calendar.MINUTE));

    System.out.println("秒: " +   calendar.get(Calendar.SECOND));

    System.out.println("毫秒: " +   calendar.get(Calendar.MILLISECOND));

}

}

1.4.3 题目设计一个区的日期的类

在程序的开发中基本上都会大量的使用取出日期的操作,而如果每次都想之前那样取出的日期的话,则会很麻烦,所以最好可以设计一个类,那么此类可以直接返回日期,返回的日期包含以下几种形式:

·形式一:2012-08-08

·形式二:2012-08-08 18-40 123

·形式三:2012年08月08日

·形式四:2012年08月08日 16时40分123毫秒

package com.java.dateDemo;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class DateTime {

private Calendar calendar = new GregorianCalendar(); // 实例化Calendar对象

public String getDate() {// 2009-03-02

    StringBuffer buf = new StringBuffer();

    buf.append(calendar.get(Calendar.YEAR)).append("-");

    buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(

            "-");

    buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));

    return buf.toString();

}

public String getDateTime() {// 2009-03-02   16:19:34.123

    StringBuffer buf = new StringBuffer();

    buf.append(this.getDate()).append(" ");

    buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append(

            ":");

    buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append(":");

    buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append(".");

    buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3));

    return buf.toString();

}

public String   getDateComplete() {// 2009年03月02日

    StringBuffer buf = new StringBuffer();

    buf.append(calendar.get(Calendar.YEAR)).append("年");

    buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append("月");

    buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2)).append("日");

    return buf.toString();

}

public String   getDateTimeComplete() {// 2009年03月02日16时19分34秒123毫秒

    StringBuffer buf = new StringBuffer();

    buf.append(this.getDateComplete());

    buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append("时");

    buf.append(this.addZero(calendar.get(Calendar.MINUTE), 2)).append("分");

    buf.append(this.addZero(calendar.get(Calendar.SECOND), 2)).append("秒");

    buf.append(this.addZero(calendar.get(Calendar.MILLISECOND), 3)).append("毫秒");

    return buf.toString();

}

private String addZero(int temp, int len) {

    StringBuffer str = new StringBuffer();

    str.append(temp);// 加入数字

    while (str.length() < len)   {

        str.insert(0,0); //   在第一个位置加上字母0

    }

    return str.toString();

}

public static void main(String args[]) {

    System.out.println(new   DateTime().getDateTimeComplete());

}
}
CNcots  CN Caprice of the stone的简写, 译为石头的随想曲. 

博客中有网上摘抄的资料. 有个人的一些小心得 ,有什么不对的地方, 还请各位观客老爷们指出.

JavaNote系列博客, 纯属个人学习时间段整理的一些笔记, 会坚持每周至少一更,多多关照.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好色鬼 2006 V02.08 <br/><br/>--------------------------------------------------------------------------------<br/> <br/>『好色鬼』是一款采集屏幕颜色的软件,软件不大,但是功能强大,实用性强,易操作,对网页设计师、photoshop、corldraw等平面设计师处理颜色是一个非常好的帮手,特别是网页设计师有更大的帮助,『好色鬼』会自动生成网页十六进制代码,只要在网页代码内粘贴就可以把想要的颜色显示在网页上;『好色鬼』除了以上功能,还有以下主要功能:<br/>◆分离红、绿、蓝三颜色值;可以对三种颜色做不同的调整;<br/>◆四个窗口采集颜色,能更方便的比较和储存颜色;<br/>◆提供放大镜功能,对局部颜色进行放大采集,让您更精确采集颜色;<br/>◆具有收藏颜色的收藏夹功能;<br/>◆提供颜色反向功能;<br/>◆提供颜色灰度功能;<br/>◆换皮肤功能,提供8款不同皮肤,同时支持皮肤扩展,可以自己动手制作自己喜好的『好色鬼』皮肤,操作简单容易实现;<br/>◆自动伸缩功能,让您有更多屏幕空间显示其他内容;<br/>◆支持屏幕吸附功能,自动吸附到屏幕边缘;<br/>◆软件完全绿色,不写入任何数据到注册表;<br/> 『好色鬼』虽然具有这么多优秀功能,但是完全免费提供给大家使用;如果你觉得好用,多推荐给你的朋友使用,就是对软件最大的肯定和支持
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值