Java概述
第一代 打孔机
第二代 汇编
第三代 高级
Java的特点
- 面向对象
- 和平台无关
3.健壮语言
核心机制
Java虚拟机
垃圾收集机制
Java 解释型语言 拿一行解释一行
jre 运行环境
环境变量
系统默认到path里面找敲的命令
添加到bin目录
Java-version 看版本
切换盘d:回车
java 运行程序是对Java虚拟机说的 区分大小写
dir 显示当前目录的列表
clathpath 配置能让机器找到类
每一个类对应了一个class 文件
Java关键字
不同的数据类型分配不同的长度
Java用Unicode编码每个字符2个字节16进制
long a = 888888888888L
i++先取值再自己+1
三目运算符
x?y:z
for( ; ; ){} while(){} do{}while()
break; 强行退出 continue; 下一次
public class deda {
public static void main(String args[]){
for(int i = 101;i <= 200;i++,i++){
for(int a = 2;a < i;a++){
if(i%a == 0){
System.out.printf(i+" ");
break;
}
}
}
}
}
public static void main(String args[]){
int a = 0; int c = 0;
for(int i=1;i<=100;i++){
if(i%3 == 0){
System.out.printf(i+" ");
c++;
}
if(c == 5){
break;
}
}
}
}
面向对象编程
成员变量默认为0 局部变量要初始化
构造方法
new +构造方法 创建一个新的对象
构造函数和类名相同没有返回值例如
pubulic class Person{
int id,int age;
Person (int n,int b){
id=n;
age=b;
}
}
系统默认有个无参的构造方法
方法执行完成之后局部变量消失
类名首字母大写
变量名小写 有驼峰标识
方法的重载
一个类中有相同的名字相同的返回值 但是参数不同的多个方法
this关键字
一般出现在方法声明里
指的是对哪个对象调用方法指的就是谁 即为当前对象的引用
在内存里在堆内存里的一个变量
static 关键字
在类中 用static修饰的成员变量为该类的公用变量只有一份
存在数据区 可以用类名去引用 Cat类名
Cat.sid= 100
不用new对象
字符串也分配在数据区
String name = "mimi"
name 是一个引用
静态方法
Cat.方法
静态方法不能访问非静态成员(调用静态方法时不会将对象的引用传给他 )
包名
packge com.xy.ni;
编译出来的类要位于正确的目录下面
一个类要用另外一个类
要么导入类的包名包 要么再用的时候写入类的包名
注意 :class 文件最上层包的父目录 位于 classpath下面
运行的程序要有main入口
(操作小技巧)
Java.lang里面的包不需要引入直接用
jdk自带jir包
把自己的类打成jar包
设置 jir包为clathpath路径
继承
只有一个父类
访问控制符
不能new这个类
重写
是子类对父类的方法不满意
重写方法不能用比被重写方法更小的权限
(重载是一个类里)
super关键字
super一般用在
子类的方法中要用 父类继承了来的成员变量或者方法
Object的常用方法
直接打印对象 返回 对象@+hash码
(hash码记录对象的位置)
默认的equals方法和==是一样的 即为比较了两个对象的引用![]
对象转型
(父类转型子类之后就可以看到整个 对象就能够调用子类的属性和方法了)
(不用写一个一个的方法了)
动态绑定和多态
是对于方法来说 执行方法时调用的代码是跟据实际的类型来确定的 不是引用的类型
public static void main(String args[]){
Object b = new Object();
Zidan c =new Zidan();
Feiji d = new Feiji();
Duotai m= new Duotai();
m.fangfa(d);m.fangfa(c);m.fangfa(b);
}
public void fangfa(Object a){
a.draw();
}
}
class Object{
public void draw(){
System.out.printf("wuti ");
}
}
class Zidan extends Object{
public void draw() {
System.out.printf("zidan ");
}
}
class Feiji extends Object{
public void draw() {
System.out.printf("feiji ");
}
}
(自己写的小程序纪念一下 哈哈)
抽象类
接口
只能为public static final 修饰成员变量
方法只能是public
不只是成员变量 方法的访问也有局限
(idea的重写的快捷键 Alt+Ins) 也能写构造方法
总结
异常处理
(注意重写的方法不能抛出比被重写方法不同的异常)
(异常是一个类)
try{} catch(){}
Error 虚拟机出错
Runtime Exception 可以不catch
用try ,,catch异常时 先小再大
自定义异常
快捷键 格式化代码 ctrl+alt+L
public class Exception {
int l=-1 ;
public static void main(String[] args) {
try {
Exception p = new Exception();
p.shangpao();
} catch (MyException e) {
System.out.printf("gaochulai");
}
}
void dengji(int a) throws MyException {
if (a < 0) {
throw new MyException("hao");
}
System.out.printf(a + "dengji");
}
void shangpao() throws MyException {
dengji(l);
}
}
class MyException extends java.lang.Exception {
private String id;
public MyException(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
}
注意 上面class MyException extends java.lang.Exception
开始写成了 Exception 和定义的类重名了 叫师傅改的哈哈
数组
a[i] Integer . parseInt (args[i]) ;
把从命令行输入的字符换成数字
public class yinyouarray {
public static void main(String[] args) {
Date[] nihao = new Date[3];
for (int i = 0; i < 3; i++) {
nihao[i] = new Date(i + 3, 8, 2017);
Date.mm();
}
}
}
class Date {
static int year, month, date;
public Date(int a, int b, int c) {
this.date = a;
this.month = b;
this.year = c;
}
public static void mm() {
System.out.printf(year + " " + month + " " + date +" ");
}
}
基础数据类型包装类
解析字符串
file类
lastModified 上次修改时间 用long类型存时间 mkdir 创建目录
import java.io.File;
public class file {
public static void main(String[] args) {
File f = new File("path1/path2", "filename");
if (f.exists()) {
System.out.printf("" + f.getAbsolutePath() + " " + f.length());
} else {
f.getParentFile().mkdirs();
try {
f.createNewFile();
} catch (java.lang.Exception e) {
System.out.printf("exception");
e.printStackTrace();
}
}
}
}
(模仿模仿)
打印指定目录下的文件列表
谷歌到的listFile()和list
返回了字符类型的数组
public static void main(String[] args) {
//创建File对象
File file = new File("D:\\Android");
//获取该目录下的所有文件
String[] files = file.list();
for (String f : files){
System.out.println(f);
}
//listFiles是获取该目录下所有文件和目录的绝对路径
File[] fs = file.listFiles();
for (File f : fs){
System.out.println(f);
}
}
枚举类型 java.lang.Enum类
枚举类比于类 用来定义类
容器
下面是实现接口的类
set没有顺序不可重复
list有顺序可以 重复(eqals)
- 只能传入对象
-
依次调用了每个元素的toString方法
重写eqals方法必须重写hashCode方法
firstname是一个字符串的索引
字符串的hashCode已经实现了hash方法
底层的容器实现不同 遍历的方法就不一样 所以用Iterator来统一实现
set接口
List接口
不是Collection接口
Comparable接口
重写的toString 方法
name类要实现的排序
String类实现了排序
map
自动打包解包
泛型
类的后面跟< > 表示制定的类型 即为返回值的类型
在定义集合的时候去指定所放的类型
省去了强制转换
流
读取文件内容
复制文件内容
import java.io.*;
public class Io {
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
try {
out = new FileOutputStream("E:\\workspaces\\imooc\\.idea\\22.txt");
in = new FileInputStream("E:\\workspaces\\imooc\\.idea\\11.txt");
} catch (FileNotFoundException e) {
System.out.printf("找不到制定文件");
e.printStackTrace();
System.exit(-1);
}
try {
int b = 0;
int a = 0;
do {
b = in.read();
System.out.printf(b + " ");
out.write((char) b);
}
while (in.read() != -1);
} catch (IOException p) {
System.out.printf(" aaa");
p.printStackTrace();
}
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(模仿的复制)
数据流
print流Object流
![QQ截图20180821214920.png](https://upload-images.jianshu.io/upload_images/12298757-a6af713d54d9d542.png?imageMogr2/auto-or
ient/strip%7CimageView2/2/w/1240)
Serialzable 接口 要序列化必须实现的接口
线程
进程是一个静态的概念
一个cpu在一个时间只有一个进程
一个时间执行一下这个进程再换为另外的进程 多个就支持多线程
starte方法和run方法的区别 run就是个方法的调用 state是创建新的线程
两种方法创建新的线程
- 实现runable接口
-
继承于Thread
本身Thread也实现了runable接口 都要重写run方法
(推荐接口)
线程的stop方法 比较 暴力 不用
结束线程的方法 定义一个布尔类型的变量 放在while里 要结束时就调用那个变量改为false
设置优先级)
两个线程指向同一个对象
num在数据区 计数用的
两种锁定当前对象的方法 第二种是 执行方法时锁定当前对象
死锁
import java.io.*;
public class Io {
public static void main(String[] args) {
FileWriter out = null;
FileReader in = null;
// String m = out.getEncoding();
// System.out.printf(m+" ");
try {
out = new FileWriter("E:\\workspaces\\imooc\\.idea\\22.txt");
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new FileReader("E:\\workspaces\\imooc\\.idea\\11.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
int b = 0;
int a = 0;
for (int i = 0; i < 6000; i++) {
out.write(i);
}
do {
b = in.read();
System.out.printf((char) b + " ");
}
while (in.read() != -1);
} catch (IOException p) {
System.out.printf(" aaa");
p.printStackTrace();
}
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
干不出来汉字呀 (流)
import java.util.*;
public class ThreadTest {
public static void main(String[] a) {
MyThread oo = new MyThread();
oo.start();
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
oo.i = false;
}
}
class MyThread extends Thread {
Boolean i = true;
public void run() {
while (i) {
System.out.println("==l=" + "=====");
try {
sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}
}
}
写了些啥哦
时间和日期、
-
得到 long 表示的时间
System.out.print(" "+now);
@Override
public int hashCode() {
return super.hashCode();
}
};
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd hh;mm;ss");
String s= simpleDateFormat.format(data);
System.out.print(s+" ");
```为啥不对呀 好像Data类变了