Hello.java
11 public class Hello { 22 public static void main(String[] args) { 33 System.out.println("Hello, world!"); 44 } 55 } 6 7文件名必须和程序的类名完全一致。扩展名是.java,编译文件为.class 一个project对应一个目录,源码存放在src目录, 编译输出存放在bin目录, bin目录在eclipse中自动隐藏。计算机最小存储单元是字节,一个字节8个二进制数00000000~11111111(1B=8b).变量分为两种:基本类型和引用类型。基本类型(持有某个数值):
-
整数类型:byte(1B),short(2B),int(4B),long(8B)
-
浮点类型:float(4B),double(8B)
-
字符类型:char(2B)
-
布尔类型:boolean
1 final double PI = 3.1415; 2 byte b = 127; // -128 ~ +127 3 short s = 32767; // -32768 ~ +32767 4 int i = 2147483647; 5 int i2 = -2147483648; 6 int i3 = 2_000_000_000; // 加下划线更容易识别 7 int i4 = 0xff0000; // 表示16进制的数 8 int i5 = 0b100000;// 表示二进制数 9 long l = 90000000000L; // 结尾加L 10 float f = 3.14e4f; 11 double f2 = 3.14e4; 12 13 System.out.println(Integer.toHexString(11)); // 输出16进制表示的整形 14 System.out.println(Integer.toBinaryString(10)); // 输出二进制表示的整形
-
与运算特点:把某些位保留下来,其余位全部置0
-
或运算特点:把某些位保留下来,其余位全部置1
-
^异或运算:相同为0不同为1
1 double a=0.0/0; // NaN(Not a number) 2 double a1=1.0/0; // Infinity 3 double a2=-1.0/0; // -Infinity
java使用Unicode表示字符
字符串类型是引用类型(变量指向某个对象,而不是持有某个对象)
11 char c1='A'; 22 char c2='中'; 33 int n1=c1; // 65 44 int n2=c2; // 20013 55 char c3='\u0041'; // \ u加4位的16进制数也可表示字符 66 String s="中文123"; // 五个字符 77 String s1="12\n"; // 三个字符 88 String s2=null; // 输出null 99 String s3=""; // 和null不一样
数组是引用类型的变量
11 int[] ns1 = new int[5]; 22 int[] ns2 = new int[] { 1, 2, 3, 4, 5 }; 33 int[] ns3 = { 1, 2, 3, 4, 5 }; 44 int[] ns4=ns3; 55 ns3[3]=999; // ns[4]=999,它们指向同一个数组 66 System.out.printf("%1$s %3$s %2$s %1$s", "A", "B", "C"); // A C B A $可以调整参数位置 7 81 int[] ns = {1, 2, 3}; 92 System.out.println(Arrays.toString(ns)); 103 int[][] ns1= {{1}, {1, 2}, {1, 2, 3}}; 114 System.out.println(Arrays.deepToString(ns1));
命令行参数是String[], 由用户输入并传递给main方法,如何解析命令行参数由程序自己实现。
this作用:
-
表示类中的属性
-
可以使用this调用本类的构造方法
-
this表示当前对象,如下
1 public boolean compare(Person per) { 2 Person per1 = this; //表示当前调用方法的对象,为per1 3 Person per2 = per; 4 if(per1 == per2) 5 return true; 6 if(per1.name.equals(per2.name) && per1.age==per2.age) { 7 return true; 8 }else { 9 return false; 10 } 11 }
Java中主要存在4块内存空间:
- 栈内存空间:保存所有对象的名称(准确说保存了引用的堆内存地址)
- 堆内存空间:保存了每个对象的具体属性内容
- 全局数据区:保存static类型的属性
- 全局代码区:保存所有的方法定义
非static声明的方法可以去调用static声明的属性或方法, 但是static声明的方法是不能调用非static类型声明的属性或方法
抽象类就是比普通类多了一个抽象方法, 除了不能直接进行对象的实例化操作之外并没有任何的不同。
接口:一种特殊的类,明确由全局常量和公共的抽象方法组成,可简写。
11 public interface A { 22 public static final String NAME = "XiaoMing";// 全局变量,等价于Stirng NAME = "XiaoMing" 33 public abstract void print(); // 定义抽象方法 , 等价于void print(); 44 public abstract String getInfo(); // 定义抽象方法,等价于String getInfo(); 55 // 接口方法默认public, 不是default。 66 }
若一个子类既要实现接口又要继承抽象类,如下
11 class 子类 extends 抽象类 implements 接口A, 接口B,...{ 22 }
允许一个抽象类实现多个接口, 但一个接口不允许继承抽象类, 但是允许一个接口继承多个接口,如下
11 interface 子接口 extends 父接口A,父接口B,...{ 22 }
除了在抽象类中定义接口及在接口中定义抽象类外, 对于抽象类来说也可以在内部定义多个抽象类, 而一个接口也可以在内部定义多个接口。
对象在向下转型先前用instanceof判断是否是某的类的实例, true后在转型。
object可接收一切引用类型,包括数组和接口类型。
11 int x = 30; 22 Integer i = new Integer(x) 33 = Integer i = 30;//自动装箱 44 55 int temp = i.intValue; 66 = int temp=i;//自动拆箱 7 81 // Integer类(字符串转int型) 92 public static int parseInt(String s) throws NumberFormatException 103 // Float类(字符串转float型) 114 public static float parseFloat(String s) throws NumberFormatException(异常属于RuntimeException可以不使用Try...catch处理,有异常交给JVM处理) 125 以上两种操作,字符串必须由数字组成。(Integer.parseInt,Float.parseFloat这样调用)
如果一个程序导入多个包中有同名类, 调用时“包.类名称”。
Thread类中提供了public Thread(Runnable target)和public Thread(Runnable target, String name)两个构造方法,可以接受Runnable接口的子类实例对象,可以以此启动多线程,
Thread和Runnable的子类都同时实现了Runnable的接口,之后将Runnable的子类放到Thread类之中,类似代理设计。
Runnable接口可以资源共享:
1 1 public class Mythread extends Thread{ 2 2 3 3 private int ticket = 5; 4 4 public void run() { 5 5 for(int i=0;i<100;i++) { 6 6 if(ticket>0) { 7 7 System.out.println("买票:ticket="+ticket--); 8 8 } 9 9 } 1010 } 1111 } 1212 public class Main { 1313 1414 public static void main(String args[]) { 1515 Mythread my1 = new Mythread(); 1616 Mythread my2 = new Mythread(); 1717 Mythread my3 = new Mythread(); 1818 my1.start(); 1919 my2.start(); 2020 my3.start(); 2121 } 2222 }
终断线程:
1 1 public class Mythread implements Runnable{ 2 2 3 3 public void run() { 4 4 System.out.println("进入run方法"); 5 5 try { 6 6 Thread.sleep(5000); 7 7 System.out.println("休眠结束"); 8 8 }catch(Exception e) { 9 9 System.out.println("休眠被终止"); 1010 return; 1111 } 1212 System.out.println("休眠正常结束"); 1313 } 1414 } 1515 public class Main { 1616 1717 public static void main(String[] args) { 1818 1919 Thread t = new Thread(new Mythread()); 2020 t.start(); 2121 try { 2222 Thread.sleep(2000); 2323 }catch(Exception e) {}; 2424 t.interrupt(); 2525 } 2626 }
线程礼让:
1 1 public class Mythread implements Runnable{ 2 2 @Override 3 3 public void run() { 4 4 5 5 for(int i=1; i<=5; i++) { 6 6 System.out.println(Thread.currentThread().getName()+"运行"+i); 7 7 if(i==3) 8 8 { 9 9 System.out.println("线程礼让"); 1010 Thread.currentThread().yield(); 1111 } 1212 } 1313 } 1414 public class Main { 1515 1616 public static void main(String[] args) { 1717 1818 Mythread my = new Mythread(); 1919 Thread t1 = new Thread(my, "线程A"); 2020 Thread t2 = new Thread(my, "线程B"); 2121 t1.start(); 2222 t2.start(); 2323 } 2424 2525 }
线程操作实例:生产者与消费者(生产者一直生产,消费者不断取走)
存在两个问题:
- 在线程中生产者没有添加完信息,消费者就取走,信息联系错乱。( 解决:同步方法synchronized )
- 线程中生产者放了多次消费者不取或消费者取了多次已取过的信息。 ( 解决: 加flag标记,同时利用object类中的等待wait()与唤醒notify() )
代码:
1 1 public class Main { 2 2 3 3 public static void main(String[] args) { 4 4 5 5 Info info = new Info(); 6 6 Producer p = new Producer(info); 7 7 Consumer c = new Consumer(info); 8 8 new Thread(p).start(); 9 9 new Thread(c).start(); 1010 } 1111 } 1212 public class Info { 1313 1414 private boolean flag = false; 1515 private String name = "LiXinghua"; 1616 private String concent = "Java"; 1717 1818 public synchronized void set(String name, String concent) { 1919 if (!flag) { 2020 try { 2121 super.wait(); 2222 } catch (InterruptedException e) { 2323 e.printStackTrace(); 2424 } 2525 } 2626 this.name = name; 2727 try { 2828 Thread.sleep(100); 2929 } catch (InterruptedException e) { 3030 e.printStackTrace(); 3131 } 3232 this.concent = concent; 3333 flag = false; 3434 super.notify(); 3535 } 3636 3737 public synchronized void get() { 3838 if (flag) { 3939 try { 4040 super.wait(); 4141 } catch (InterruptedException e) { 4242 e.printStackTrace(); 4343 } 4444 } 4545 try { 4646 Thread.sleep(100); 4747 } catch (InterruptedException e) { 4848 e.printStackTrace(); 4949 } 5050 System.out.println(this.name + " " + this.concent); 5151 flag=true; 5252 super.notify(); 5353 5454 } 5555 } 5656 public class Producer implements Runnable { 5757 5858 boolean flag = false; 5959 private Info info = null; 6060 6161 public Producer(Info info) { 6262 this.info = info; 6363 } 6464 6565 @Override 6666 public void run() { 6767 for (int i = 0; i < 10; i++) { 6868 6969 if (flag) { 7070 this.info.set("LiXinghua", "Java"); 7171 System.out.println("修了几次"); 7272 flag = false; 7373 } else { 7474 this.info.set("mldn", "JavaMldn"); 7575 System.out.println("修了几次"); 7676 flag = true; 7777 } 7878 } 7979 } 8080 } 8181 public class Consumer implements Runnable{ 8282 8383 private Info info = null; 8484 public Consumer(Info info) { 8585 this.info = info; 8686 } 8787 @Override 8888 public void run() { 8989 9090 for(int i=0; i<10; i++) { 9191 this.info.get(); 9292 } 9393 9494 }
停止进程常用方法:在Mythread中调用stop()将flag设置为false这样run()方法就停止运行
代码:
1 1 public class Mythread implements Runnable { 2 2 3 3 private boolean flag = true; 4 4 5 5 @Override 6 6 public void run() { 7 7 8 8 int i = 0; 9 9 while (this.flag) { 1010 while (true) { 1111 System.out.println(i++); 1212 } 1313 } 1414 } 1515 public void stop() { 1616 this.flag = false; 1717 } 1818 public class Main { 1919 2020 public static void main(String[] args) { 2121 2222 Mythread my = new Mythread(); 2323 Thread t = new Thread(my, "线程啊"); 2424 t.start(); 2525 my.stop(); 2626 } 2727 2828 }
返回值类型 方法名称(Object...args)表示方法可以接收任意多个参数
1 1 public class Main { 2 2 3 3 public static void main(String args[]) { 4 4 5 5 String str[] = fun("lasd", "asd", "111"); 6 6 for(String t:str) { 7 7 System.out.print(t+"、"); 8 8 } 9 9 } 1010 public static <T> T[] fun(T... args) { 1111 return args; 1212 } 1313 }
finalize()可以让一个对象被回收前执行操作:
11 @Override 22 public String toString() { 33 return "姓名:" + this.name + ", " + "年龄:" + this.age; 44 } 55 public void finalize() throws Throwable{ 66 System.out.println(this); 77 }
格式化日期操作:
public SimpleDateFormat(String pattern) 构造类型 ,通过一个指定的模板构造对象
public Date parse(String sourse) throws ParseException 普通类型,将一个包含日期的字符串变为Date类型
public final String format(Date date) 普通类型, 将一个Date类型按照指定格式变为String类型
1 1 import java.util.Date; 2 2 import java.text.DateFormat; 3 3 import java.text.SimpleDateFormat; 4 4 public class Main { 5 5 6 6 public static void main(String args[]) { 7 7 8 8 Date d = null; 9 9 String strDate = "2018-8-26 17:11:30.214"; 1010 String pat1 = "yyyy-MM-dd hh:mm:ss.SSS"; 1111 String pat2 = "yyyy年MM月dd日, hh时mm分ss秒SSS毫秒啦!"; 1212 1313 SimpleDateFormat sdf1 = new SimpleDateFormat(pat1); 1414 SimpleDateFormat sdf2 = new SimpleDateFormat(pat2); 1515 try { 1616 d = sdf1.parse(strDate); 1717 } catch (Exception e) { 1818 e.printStackTrace(); 1919 } 2020 System.out.println(sdf2.format(d)); 2121 } 2222 }