java新手笔记32 jdk5新特性

1.for

package com.yfs.javase;

import java.awt.Color;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("all")
public class ForDemo { /**
* @param args
*/
public static void main(String[] args) {
//useArray();
//useSet();
boxTest();
//
// Calendar cal = Calendar.getInstance();
// int year = cal.get(Calendar.YEAR); }
@Deprecated
public static void boxTest() {
int a = 10;
Integer oInt = new Integer(a);
Integer o2 = a;//自动装箱
System.out.println("o2 = " + o2); int b = o2 + 1; // 自动拆箱
System.out.println("b = " + b);
}
public static void useSet() {
Set<String> set = new HashSet<String>();
set.add("eee");
set.add("ccc");
set.add("aaa");
set.add("bbb");
set.add("yyy");
for (Object s : set) {
System.out.print((String)s + "\t");
} }
//增强for
public static void useArray() {
int[] a = {45,23,11,68,28,65,9,2,18,97};
for(int i : a) {
System.out.print(i + "\t");
} }
@Override//元数据
public String toString() {
// TODO Auto-generated method stub
return super.toString();
} }

2.静态导入

package com.yfs.javase;
import static java.lang.Math.PI;//静态导入
import static java.lang.Math.sqrt;
public class StaticImport { /**
* @param args
*/
public static void main(String[] args) {
//staticTest();
paramTest("java",2,3);
// paramTest(2,3,4,5,6);
// paramTest(10,20,30,40);
// paramTest(new int[]{45,22,61,32,78,15,24,63,98});
}
//变长参数 1.只能有一个 2.类型没有限制 3.可以传其他参数 必须在变长参数之前
public static void paramTest(String name, int ... a /*, Object...obj*/) {
int sum = 0;
System.out.println("call paramTest(int ... a) ...");
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println("sum = " + sum); } public static void paramTest(int a,int b) {
int sum = a + b;
System.out.println("sum = " + sum); } public static void staticTest() {
double area = Math.PI * 2 *2;
area = PI* 4 * 4;
area = PI* 6 * 6;
double result = Math.sqrt(16);
result = sqrt(81);
} }

3.Thread1继承 Thread

package com.yfs.javase.thread;

public class Thread1 extends Thread {

	public void test() {
while( true ) {
System.out.println("bbbbbbbbbb");
}
} @Override
public void run() {//覆盖父类run方法
while( true ) {
System.out.println(Thread.currentThread().getName() + "bbbbbbbbbb");
}
} }

4.Thread2实现Runnable接口

package com.yfs.javase.thread;

public class Thread2 implements Runnable {

	@Override
public void run() {
while(true) {
System.out.println(Thread.currentThread().getId() + "ccccccccccc");
} } }

5.Thread1测试(单线程)

package com.yfs.javase.thread;

public class ThreadDemo1 {

	public static void main(String[] args) {

		Thread1 th1 = new Thread1();
th1.test(); for (int i = 0;; i++) {
System.out.println("aaaaaaaaaaa"); } //System.out.println("执行结束..."); } }

6.Thread2测试(多、线程启动)

package com.yfs.javase.thread;

public class ThreadDemo2 {

	public static void main(String[] args) {
//继承Thread类
Thread1 th1 = new Thread1();
th1.start();//调用父类start方法 //实现runnabel接口
Thread2 r2 = new Thread2();
Thread th2 = new Thread(r2);
th2.start(); for (int i = 0;; i++) {
System.out.println("aaaaaaaaaaa"); } //System.out.println("执行结束..."); } }

7.currentThread

package com.yfs.javase.thread;

public class ThreadDemo3 {
// 线程启动
public static void main(String[] args) throws Exception {
// 继承Thread类
Thread1 th1 = new Thread1();
//th1.run();//普通方法调用 线程的执行代码============?????注意
//启动
th1.setName("逗你玩 ");
th1.start(); //接口
Thread th2 = new Thread(new Thread2()); th2.start();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);===========
for (int i = 0;; i++) {
Thread.sleep(1);
System.out.println( Thread.currentThread().getPriority() + " aaaaaaaaaaa"); } } }
上一篇:centos mongodb cluster install 完全版


下一篇:UVa 1583 Digit Generator --- 水题+打表