变量
数据类型
基本数据类型 : byte(1),short(2),int(4),long(8),char(2),double,float,boolean
引用数据类型 : 数组(array),类(class),接口(interface)
常量 : 只能初始化一次
final int num = 20;
final修饰类,不能被继承(String,Math)
final修饰方法,不能被子类重写
final修饰属性,常量
+,-,*,/,%
++,--
&&,||,! (&,|)
int x = 1; x = x++;
表达式1 ? 表达式2:表达式3
局部变量(声明在方法里或参数中),全局变量(成员变量,实例变量,对象属性)
循环、数组、方法
for,while,do{}while()至少执行一次
int arr[] = {1,2,3}
int arr[] = new int[]{1,2,3};
int arr[] = new int[3];
String arr[] = new String[3];
Student arr[] = new Student[3];
冒泡排序,选择排序
二维数组 : int arr [ ][ ]= {{1,2,3},{4},{5,6}};
方法:
(public protected private) static 返回值 方法名(参数列表){
}
构造器(构造方法,构造函数)
public class Demo{
private int age;
public Demo(){
// 无参构造器
}
public Demo(int a){
this.age = age;
}
public int getAge(){
return this.age;
}
}
重载(静态多态,编译时多态,overload):同一类中,同名不同参的方法
面向对象
继承(extends)
一个类可以从一个现有类中派生出来,派生出的类称为子类,子类拥有父类所有属性和方法,除了私有 成员和构造器,子类还可以拓展自己独有的属性和方法,是代码重用的一种手段,也是实现开闭原则的基础
重写:子类重写父类方法,同名同参同返回值
封装(Wrapper )
采用不同的修饰符(public protected private 默认的)控制代码的可见范围
多态
对同一消息做出不同响应,在java中体现为重写(override,运行时多态)和重载
它是一种编程模式,①要有泛化;②要有上溯造型;③要有方法重写
解耦合
抽象(abstract)
将事物的主要特性抽象成概念模型,在java中接口,抽象类都是抽象的体现
抽象类 : 不能实例化(有构造器)
public abstract class Demo{
public void test(){}
public abstract void test1(int num);
}
抽象类和接口的异同:
接口和抽象类都不能实例化
接口没有构造器
接口中只有常量和抽象方法
类是单继承,接口是多实现
static:静态
主要修饰方法和属性
可以直接通过类名.属性名或方法名来调用
静态的属性和方法保存在方法区,他们是共享数据
public class Demo{
int age = 20;
static int num = 20;
public static void test(){
int num = 100;
}
public void test1(){
}
}
class Test{
public static void main(String[] args){
Demo.num = 100;
Demo.test();
Demo d = new Demo();
d.test1();
d.test();
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.age++;
d2.age++;
}
}
工厂模式
public class ProxyFactory{
public ProxyFactory getProxy(){
return new Proxy();
}
}
class Test{
public static void main(String args[]){
ProxyFactory pf = new ProxyFactory();
Proxy p = pf.getProxy();
}
}
代理模式
public class Proxy{
public Object getInstance(String str){
if(str.equals("A")){
return new A();
}
if(str.equals("B")){
return new B();
}
if(str.equals("C")){
return new C();
}
}
}
class Main{
public static void main(String args[]){
ProxyFactory pf = new ProxyFactory();
Proxy p = pf.getProxy();
A a = (A)p.getInstance("A");
B b = (B)p.getInstance("B");
C c = (C)p.getInstance("C");
}
}
单例模式(工厂) : 节省资源
// 饿汉模式
public class Singleton{
private static Singleton singleton = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return singleton;
}
}
// 懒加载
public class Singleton{
private static Singleton singleton;
private Singleton(){}
public static Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
异常处理
try..catch..finally
throws
throw
public class Demo{
public void test1() throws ClassNotFoundException{
Class.forName("com.neuedu.Student");
}
public void test2(){
try{
Class.forName("com.neuedu.Student");
}
catch(ClassNotFoundException e){
e.pringStackTrace();
}finally{
int num = 100;
}
}
public void test(){
test1(); // 会抛异常,因为test1没有处理
test2();// 不会
}
// throw
public void test4() throws NullPointerException{
int num = 10;
if(num != 10){
throw new NullPointerException(); // 主动抛出异常
}
}
}
final,finally,finalize()区别
final修饰....
finally用于异常处理,一般用于回收资源
finalize()与GC(垃圾回收)相关
字符串,包装类,Math,Date,Calendar,SimpleDateFormat
equals等方法
包装类与字符串之间互相转换
Math random(),floor(),ceil(),round(),
Date getTime()获取时间戳
Calendar c = Calendar.getInstance();
SimpleDateFormat 日期转换类
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(Date date);
Date date = sdf.parse(String strDate);
集合
Collection 单列集合
List 有序可重复
ArrayList 基于数组实现,随机访问速度快
List<String> list = new ArrayList();// 默认大小10
List<String> list = new ArrayList(20);
LinkedList 基于链表实现,插入删除快
Vector 基于数组实现,线程安全
Set 无序不可重复
HashSet
TreeSet
Map 双列集合
HashMap 允许null作为键值对
TreeMap
HashTable 不允许null作为键值对,线程安全
Entry内部接口遍历Map
Map<String,String> map = new HashMap();
for(Entry<String,String> entry : map.entrySet()){
System.out.println(entry.getKey()+","+entry.getValue());
}
Collections 有一些静态方法,操作集合
文件相关
public class Test{
public void test(){
// 创建对象
File file1 = new File("D:\\abc.txt");
// 判断对象所代表的文件路径是否存在
if(!file1.exsits){
// 创建文件
file1.creatNewFile();
}
// 创建对象
File file2 = new File("D:\\abc\\xyz");
if(!file2.exsits){
// 创建文件夹
file2.mkdirs();
}
}
}
多线程
程序 : 一切软件
进程 :正在运行的程序
线程 : 进程中的进程(例如:360安全卫士是一个程序,运行起来后它是一个进程,360安全卫士下载软件,下载软件这就是一个线程)
一个java类如何成为线程类
public class Test1 extends Thread{
@override
public void run(){
// 书写线程功能的方法
for(int i = 0;i < 10;i++){
Sysout("Test1 : " + i);
}
}
}
public class Test2 implements Runnable{
@override
public void run(){
// 书写线程功能的方法
for(int i = 0;i < 10;i++){
Sysout("Test2 : " + i);
}
}
}
public class Main{
public static void main(String args[]){
Test1 t1 = new Test1();
t1.start(); // 启动线程
try{
Thread.sleep(2000); // 线程休眠
}catch(Exception e){
e.printStackTrace();
}
Test2 t2 = new Test2();
Thread thread = new Thread(t2);
thread.start();// 启动线程
for(int i = 0;i < 10;i++){
Sysout("main : " + i);
}
}
}
start()和run()区别
start是启动线程方法,run是线程功能方法
sleep和wait区别
sleep线程休眠方法,是Thread类的静态方法,休眠时间到,线程会自动唤醒
wait是线程等待方法,是属于对象的方法,wait是不会自动唤醒的,需要手动唤醒,notifyAll方法是唤醒方法
多线程涉及同步问题
同步锁synchronized
关键字
网友评论