喝酒I创作I分享

 

生活中总有些东西值得分享

@醉翁猫咪 

Java基础教程(全代码解析)_i++

 

Java基础教程(全代码解析)_i++_02

 

 Java基础教程(全代码解析)

 

今天带来Java基础教程,本文放在【教程】菜单栏中方便学习。之前的代码格式不适合程序员看,现在换了一种格式Java基础教程(全代码解析)_父类_03,记得点赞哦

 

字面量:

 

整数字面量为整型(int)

小数字面量为双精度浮点型(double)

 

数据类型:

 

byte short int long float double

 

接下来代码展示理解

 

 

public class Test{
char c = 'a';
switch(c){
  case 'b':
  System.out.println('b');
  break;
  case 'c':
  System.out.println('c');
  break;
  case 'a':
  System.out.println('a');
  break;
  default:
  System.out.println('d');
 }
} 
}

 

swith( char byte short int)只允许四种类型

 

 

public class Test{
public static void main(String args[]){
  int score = 90;
  if(score > 85 && score <= 100){
   System.out.println("成绩为优");
  }
  else if(score > 75 && score <= 85){
   System.out.println("成绩为良");
  }
  else if(score > 60 && score <= 75){
   System.out.println("成绩为中");
  }
  else if(score <= 60 && score >= 0){
   System.out.println("成绩为差");
  }
  else if(score > 100 || score < 0){
   System.out.println("成绩不在正常的范围之内");
  }
 }
}

 

for(int i = 0; i < 10; i++){
 System.out.println(i);
}

 

public class Test{
public static void main(String args[]){
  int i = 0;
  while(i < 10){
   System.out.println(i);
   i++;
  }
 }
}

 

打印100-200的素数:

 

 

class Test{
public static void main(String args[]){
  for(int i = 100; i < 201; i++){
   boolean b = false;
   
   for(int j = 2; j < i-1; j++){
    int k=i%j;
    if(k==0){
     b=true;
    }
   }
   //如果不是true就打印出素数
   
  if(!b){
   System.out.println(i);
  }
  }
 }
}

 

public class Test{
public static void main(String args[]){
  int i = 5;
  int j = i++ + 5;
  System.out.println(i);
  System.out.println(j);
  }
}

 

j=10;
i=6;

 

int j = ++i + 5;
System.out.println(j);
j=11;
i=6;

 

&逻辑与

&&短路与

 

class Test{
public static void main(String args[]){
  for(int i=1; i<5; i++){
   for(int j=0; j<4-i; j++){
    System.out.print(" ");
   }
   for(int k=0; k<i; k++){
    System.out.print("* "):
   }
   System.out.println("");
   }
  }
}

 

对象就是引用数据类型

 

Java虚拟机把内存分为栈内存和堆内存

 

class Test{
public static void main(String args[]){
  Dog d = new Dog();
  d.name="哈哈";
  d.age=2;
  d.jump();
  System.out.println("名字是"+d.name);
  }
}

 

重载的表达

 

 

class A{
void funA(){
  System.out.println("没有参数的funA函数");
 }
void funA(int i){
  System.out.println("有参数的funA函数");
 }
void funA(int i,double d){
  System.out.println("拥有两个参数的funA函数");
 }
}

 

class Test{
public static void main(String args[]){
  A a = new A();
  a.funA();
  a.funA(1,2.0);
  }
}

 

继承,封装,多态

 

什么是继承?

在现实世界当中,继承就是儿子得到老子的东西,在面向对象的世界当中,继承就是一个类得到了另一个类当中的成员变量和成员方法

 

Java只支持单继承,不允许多继承,继承是为了减少重复代码

 

使用super调用父类构造函数的方法

 

class Person{
 String name;
int age;
 Person(){
  System.out.prinltn("Person的无参数构造函数");
 }
 Person(String name,int age){
  this.name=name;
  this.age=age;
  System.out.println("Person有参数的构造函数");
 }

void eat(){
  System.out.println("定义吃饭的方法");
 }
}

 

class Student extends Person{
//子类继承父类
 Student(){
  //父类
  super();
  System.out.println("Student的无参数构造函数");
 }
 Student(String name,int age,int id){
  super(name,age);
  this.id=id;
 }
}

 

class Test{
public static void main(String args[]){
  Student student = new Student();
 }
}

 

什么是复写?

具有父子关系的两个类中,父类和子类各有一个函数,这两个函数的定义(返回值类型,函数名,参数列表)完全相同

 

对象的转型(多态性地体现)

 

什么是向上转型?向上转型就是将子类的对象赋值给父类的引用。

什么是向下转型?向下转型就是将父类的对象赋值给子类的引用。

 

Student s = new Student();
Person p = s;

class Student extends Person{
 String address;

void introduce(){
  super.introduce();
  System.out.println("我家在"+address);
 }
}

class Test{
public static void main(String args[]){
  String s = new Student();
  Person p = s;
  
  p.name = "hhh";
  p.age = 20;
  p.introduce();
  }
}

 

向下转型:

 

 

Student s1 = new Student();
Person p = s1;
Student s2 = (Student)p;

 

什么是抽象函数?

没有函数体的函数被称为抽象函数

 

什么是抽象类?

使用abstract定义的类称为抽象类

 

抽象类不能够生成对象

 

抽象类不能实例化,继承抽象类,那么该类必须为抽象类

 

abstract class Person{
abstract void eat();
}

class Chinese extends Person{
void eat(){
  System.out.pritln("hhh");
 }
}

class Test{
public static void main(String args[]){
  Person p = new Chinese();
  p.eat();
 }
}

 

代码:

 

 

abstract class Person{
 Person(){
  System.out.println("Person没有参数的构造函数");
 }

 Person(String name,String age){
  this.name=name;
  this.age=age;
 }

 String name;
int age;
void introduce(){
  System.out.println("我的名字是"+name+",我的年龄是"+age);
 }
abstract void eat();
}
}

class Chinese extends Person{
 String address;
 Chinese(){
  super();
  System.out.println("Chinese的构造函数"); 
  }
  
  Chinese(String name,int age,String address){
   super(name,age);
   this.address=address;
  }
  void eat(){
   //复写
   System.out.println("吃饭");
  }
}

class Test{
public static void main(String args[]){
  Person p = new Chinese();
  p.eat();
  }
}

 

访问权限

 

public > protected > default > private

 

 

什么是接口(interface)

接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象

 

interface Student{
public void read();
public void write();
}

class ChineseStudent implements Student{
//复写
public void read(){
  System.out.println("read");
 }
public void write(){
  System.out.println("write");
 }
}

class Test{
public static void main(String args[]){
  ChineseStudent chinesestudent = new ChineseStudent();
  Student student = chinesestudent;
  
  student.read();
  student.write();
 }
}

实现接口用implements关键字,
一个接口可以实现多个接口,
一个接口可以继承多个接口

interface Student{
public void read();
public void write();
}

interface Teacher{
public void teach();
public void test();
}

class Person implements Student,Teacher{
public void read(){
  System.out.println("read"):
 }
public void write(){
  System.out.println("write");
 }
public void teach(){
  System.out.println("teach"):
 }
public void test(){
  System.out.println("test"):
 }
}

class Test{
public static void main(String args[]){
  Person person = new Person();

  Student student = person;
  student.read();
  student.write();

  Teacher teacher = person;
  teacher.teach();
  teacher.close();
 }
}

 

工厂方法模式:

 

 

interface Printer{
public void open();
public void close();
public void print(String s);
}

class Printer1 implements Printer{
public void open(){
  System.out.println("printer1 open");
 }
public void close(){
  System.out.println("printer1 close");
 }
public void print1(String s){
  System.out.println("print1"+s);
 }
}

class Printer2 implements Printer{
private void clean(){
  System.out.println("printer2 clean");
 }
public void close(){
  this.clean();
  System.out.println("print2 close");
 }

public void open(){
  System.out.println("print2 open");
 }

public void print(String s){
  System.out.println("print2"+s);
 }
}

class Test{
public static void main(String args[]){
  //根据用户的选择
  printer.open();
  printer.print("test");
  printer.close();
  }
}

 

工厂设计模式

 

 

class PrinterFactory{
public static Printer getPrinter(int flag){
  Printer printer = null;

  //int flag = 0;
  
  if(flag == 0){
   printer = new printer1();
  }
  else(flag == 1){
   printer = new CanonPrinter();
  }
  return printer;
 }
}

class Test{
public static void main(String args[]){
  //Printer gerPrinter(int flag)
  int flag = 1;
  Printer printer = PrinterFactory.getPrinter(flag);
  printer.open();
  printer.print("test");
  printer.close();
 }
}

 

 

什么是异常?

try...catch...finally结构的使用方法

 

 

class Test{
public static void main(String args[]){
  
  try{ 
   int i = 1 / 0;
   }
   catch(Exception e){
    e.printStackTrace();
   }
   finally{
    System.out.println("finally");
   }
   System.out.println(5);
  }
}

class Test{
public static void main(String args[]){
  try{
   Thread.sleep(1000);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

 

throw和throws的作用区别:

 

 

class Person{
private int age;

public void setAge(int age) throws Exception{
  if(age<0){
   RuntimeException e = new RuntimeException("年龄不能小于0");
   throw e;
  }
  this.age = age;
  }
}

class Test{
public static void main(String args[]){
  Person person = new Person();
  try{
   person.setAge(-1);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

 

IO分为三种:

 

第一种:

  1. 输入流

  2. 输出流

第二种:

  1. 字节流

  2. 字符流

第三种分类:

  1. 节点流

  2. 处理流

 

IO当中的核心类

InputStream OutputStream FileInputStream FileOutputStream

字节流的核心类

InputStream OutputStream

 

FileInputStream

 

 

class Test{
public static void main(String args[]){
  FileInputStream fis = null;
  try{
   fis = new FileInputStream("e:/read.txt");
   byte[] buffer = new byte[100];
   fis.read(buffer,0,buffer.length);
  
   for(int i = 0;i<buffer.length;i++){
    System.out.println(buffer[i]);
   }
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

 

 

字符表达

FileInputStream和FileOutputStream

 

 

class Test{
public static void main(String args[]){
  FileInputStream fis = null;
  FileOutputStream fos = null;
  
  try{
   fis = new FileInputStream("e:/read.txt"):
   fos = new FileOutputStream("e:/write.txt");
  
   byte[] buffer = new byte[100];
   int temp = fis.read(buffer,0,buffer.length);
   fos.write(buffer,0,temp);
  
   //String s = new String(buffer);
   //s = s.trim();
   //System.out.println(s);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

 

优化:

 

 

class Test{
public static void main(String args[]){
  //声明输入流,输出流引用
  FileInputStream fis = null;
  FileOutputStream fos = null;
  
  try{
   fis = new FileInputStream("e:/read.txt");
   fos = new FileOutputStream("e:/write.txt");
  
   byte[] buffer = new byte[1024];
   while(true){
    int temp = fis.read(buffer,0,buffer.length);
    if(temp == -1){
     break;
    }
    fos.write(buffer,0,temp);
  }
  }catch(Exception e){
   System.out.println(e);
  }
  finally{
   try{
    fis.close(); 
    fos.close();
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}
}

 

 

字符流

 

字节输入流:Reader <-- FileReader

int read(char[] c,int off,int len)

字节输出流:Writer <-- FileWriter

void write(char[] c,int off,int len)

 

 

public class TestChar{
public static void main(String args[]){
  FileReader fr = null;
  FileWriter fw = null;
  try{
   fr = new FileReader("e:/read.txt");
   fw = new FileWriter("e:/write.txt");
   
   char[] buffer=new char[100];
   int temp = fr.read(buffer,0,buffer.length);
   fw.write(buffer,0,temp);
   }
   catch(Exception e){
    System.out.println(e);
   }
   finally{
    try{
    }
    catch(Excepiton e){
     System.out.println(e); 
    }
   }
}

 

 

节点流和处理流

处理流使用实例

装饰者模式

 

节点流与处理流的关系

 

BufferedReader介绍

一行一行的读取

 

处理流,Reader,Writer以及他们所有的子类都属于字符流

 

BufferedReader属于字符流,处理流,然后呢?它又是处理流

BufferedReader全称字符输入处理流

 

FileReader 和 BufferedReader

 

class Test{
public static void main(String args[]){
  FileReader fileReader = null;
  BufferReader bufferReader = null;
  try{ 
   fileReader = new FileReader("e:/read.txt");
   bufferedReader = new BufferedReader(fileReader);
   String line = null;
   while(true){
    line = bufferedReader.readLine();
    if(line == null){
     break;
    }
    System.out.println(line); 
   }
  
   //String line = bufferedReader.readLine();
   //System.out.println(line);
   }
   catch(Exception e){
    System.out.println(e);
    }
    finally{
     try{
      bufferedReader.close(); 
      fileReader.close();
     }
     catch(Exception e){
      System.out.println(e);
     }
    }
   }
}

 

装饰者模式

 

 

interface worker{
public void doSomeWork();
}

class worker1 implements worker{
public void doSomeWork(){
  System.out.println("work1");
 }
}

class worker2 implements worker{
public void doSomeWork(){
  System.out.println("work2");
 }
}

class A implements worker{
private Worker worker;

public A(Worker worker){
  this.worker=worker;
 }
public void doSomeWork(){
  System.out.println("哈哈");
  worker.doSomeWork();
 }
}

class Test{
public static void main(String args[]){
  Worker1 worker1 = new Worker1();
  A a = new A(worker1);
  a.doSomeWork();

  Worker2 worker2 = new Worker2();
  A a2 = new A(worker2);
  a2.doSomeWork();
 }
}

 

如何生成内部类的对象?

 

 

class Test{
public static void main(String args[]){
  A a = new A();
  
  A.B b = new A().new B();
  //或者A.B b = a.new B();
 }
}

class A{
int i;
class B{
  int j;

  int funB(){
   int result = i+j;
   return result;
  }
 }
}

class Test{
public static void main(String args[]){
  A a = new A();
  A.B b = a.new B();

  a.i=3;
  a.j=1;
  
  int result = b.funB();
  System.out.println(result);
  }
}

class A{
int i;
class B{
  int j;
  int funB(){
   int result = A.this.i+this.j;
   return result;
  }
 }
}

 

匿名内部类

 

 

interfacce A{
public void doSomething();
}

class B{
public void fun(A a){
  System.out.println("B函数");
  a.doSomething();
 }
}

class Work implements A{
public void doSomething(){
  System.out.println("doSomething");
 }
}

class Test{
public static void main(String args[]){
  Work work = new Work();
  A a = work;

  B b = new B();
  b.fun(a);
  }
}

 

匿名内部类

 

 

class Test{
public static void main(String args[]){
  B b = new B();
  b.fun(new A(){
   public void doSomething(){
    System.out.println("匿名内部类");
   }
  });
 }
}

 

多进程:在操作系统中能同时运行多个程序

多线程:同一个应用程序中多个顺序流同时执行

 

创建线程的方法

 

继承类Thread并重写run(),run()称为线程体;用这种方法定义的类不能再继承其他类。

 

class FirstThread extends Thread{
public void run(){
  for(int i=0;i<100;i++){
   System.out.println("FirstThread"+i);
  }
 }
}

class Test{
public static void main(Sting args[]){
  FirstThread ft = new FirstThread();
  ft.start();
  
  for(int i = 0; i<100;i++){
   System.out.println("main"+i):
  }
 }
}

 

接口Runnable的类作为线程的目标对象

 

 

class Test implements Runnable{
public void run(){
  for(int i = 0;i<100;i++){
   System.out.println("Runnable"+i);
  }
 }
}

class Test{
public static void main(String args[]){
  Test test = new Test();
  Thread t = new Thread(test);
  System.out.println(t.getPriority());
  t.start();
 }
}

 

中断线程

 

 

Thread.sleep();
Thread.yield();//让出自己正在使用的CPU

 

设置线程的优先级

 

 

getPriority();
setPriority();

class Test implements Runnable{
public void run(){
  for(int i = 0;i<100;i++){
   System.out.println("Runnable"+i);
   if(i==50){
    try{
     Thread.sleep(2000);
    }
    catch(Exception e){
     System.out.println(e);
    }
   }
  }
 }
}

class Test{
public static void main(String args[]){
  RunnableImp1 ri = new RunnableImp1();
  Thread t = new Thread(ri);
  
  t.setPriority(Thread.MAX_PRIORITY);
  //t.setPriority(Thread.MIN_PRIORITY);
  
  t.start();
  System.out.println(t.getPriority());
  }
}


class Test{
public static void main(String args[]){
  MyThread myThread = new MyThread();

  Thread t1 = new Thread(myThread);
  Thread t2 = new Thread(myThread);

  t1.setName("线程1");
  t2.setName("线程2");

  //分别启动
  t1.start();
  t2.start();
 }
}

class MyThread implements Runnable{
int i = 100;
public void run(){
  while(true){
   System.out.println(Thread.currentThread().getName()+i);
   i--;
   Thread.yield();
   if(i<0){
    break;
   }
  }
 }
}

//同步代码块

class MyThread implements Runnable{
int i = 100;
public void run(){
  while(true){
   synchronized(this){
    System.out.println(Thread.currentThread().getName()+i);
    i--;
    Thread.yield();
    if(i<0){
     break;
    }
   }
  }
 }
}

深入synchronized关键字

class Service{
public void fun1(){
  synchronized(this){
   try{
    Thread.sleep(3*1000);
   }
   catch(Exception e){
    System.out.println("fun1");
   }
  }
  
public void fun2(){
  synchronized(this){
   System.out.println("fun2");
  }
 }
}

class MyThread1 implements Runnable{
private Service service;
public MyThread1(Service service){
  this.service = service;
 }
public void run(){
  service.fun1();
 }
}

class MyThread2 implements Runable{
private Service service;
public MyThread2(Service service){
  this.service = service;
 }
public void run(){
  service.fun2();
 }
}

class Test{
public static void main(String args[]){
  Service service = new Service();
  Thread t1=new Thread(new MyThread1(service));
  Thread t2=new Thread(new MyThread2(service));

  t1.start();
  t2.start();
 }
}

 

 

同步锁 锁住的是service

同步方法,同步代码块锁住this

 

 

class Service{
public synchronized void fun1(){
  try{
   Thread.sleep(3*1000);
  }
  catch(Exception e){
   System.out.println(e);
  }
  System.out.println("fun1");
 }
public void fun2(){
  synchronized(this){
   System.out.println("fun2");
 }
 }
}


数组

class Test{
public static void main(String args[]){
  //数组的静态声明
  int arr [] = {5,2,7,8,9,0};

  arr[3] = 10;

  //System.out.println(arr[3]);

  for(int i = 0;i<5;i++){
    System.out.println(arr[i]);
   }

  }
}

class Test{
public static void main(String args[]){
  int arr[] = {2,4,6,7,8};
  
  System.out.println(arr.length);
  }
}

数组的动态声明

class Test{
public static void main(String args[]){

//动态声明
int arr [] = new int [10];
 System.out.println("arr数组长度"+arr.length);

   for(int i = 0;i<arr.length;i++){
    System.out.println(arr[i]);
   }
 }
}

二维数组

class Test{
public static void main(String args[]){
  //二维数组的定义方法,长度为3

   int arr [][] = {{1,2,3},{4,5,6},{7,8,9}};
   
   System.out.println(arr[1][1]);

   for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
     System.out.println(arr[i][j]);
    }
   }

 }
}

优化

class Test{
public static void main(String args[]){
  //二维数组的定义方法,长度为3

   int arr [][] = {{1,2,3},{4,5,6},{7,8}};
   
   System.out.println(arr[1][1]);

   for(int i = 0; i < arr.length; i++){
    for(int j = 0; j < arr[i].length; j++){
     System.out.println(arr[i][j]);
    }
   }

 }
}

动态

class Test{
public static void main(String args[]){

   //int arr [][] = {{1,2,3},{4,5,6},{7,8}};
   
   int arr [][] = new int[3][5]; 
  
   System.out.println(arr[1][1]);

   for(int i = 0; i < arr.length; i++){
    for(int j = 0; j < arr[i].length; j++){
     System.out.println(arr[i][j]);
    }
   }

 }
}

 

类集框架

一组类和接口,位于java.util包,主要用于存储和管理对象,主要分为三大类---集合,列表和映射。

 

什么是集合(Set)

集合中对象是没有顺序的,并且没有重复对象;

 

什么是列表(List)

集合中对象可以有重复的对象,可以按照顺序取,也可以指定取。

 

什么是映射(Map)

每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。

 

类集框架主体结构

 

interface
Iterator Collection
ListIterator List Set Map
LinkeList ArrayList HashSet SortedSet HashMap SortedMap
LinkedHashSet TreeSet LinkedHashMap TreeMap
Comparable Comparator Collections Arrays

//arrayList默认10,可无限长,关于泛型

public class Test{
public static void main(String args[]){

  //ArrayList arrayList = new ArrayList();
  ArrayList<String> arrayList = new ArrayList<String>();

  arrayList.add("a");
  arrayList.add("b");
  arrayList.add("c");

  //String s = arrayList.get(1);
  //System.out.println(s);

   for(int i=0;i<3;i++){
    String s = arrayList.get(i);
    System.out.println(s);
   }

  }
}

优化

public class Test{
public static void main(String args[]){
  ArrayList<String> arrayList = new ArrayList<String>();
  
  arrayList.add("a");
  arrayList.add("b");
  arrayList.add("c");
  arrayList.add("d");
  
  for(int i = 0; i<arrayList.size();i++){
   String s = arrayList.get(i);
   System.out.println(s);
  }
 }
}

 

类集框架

集合 无序 不可重复

列表 有序 可重复

映射

 

Collection和Iterator接口

Set与HashSet的使用方法

 

Set继承了Collection

 

public class Test{
  public static void main(String args[]){

   //HashSet<String> hashSet = new HashSet<String>();
   //Set<String> set = new HashSet<String>();
   
  //别管就是转,方便
   Set<String> set = new HashSet<String>();
   set.add("a");
   set.add("b");
   set.add("c");
   set.add("d");

   int i = set.size();
  
   System.out.println(i);

  }
}

不可以重复

public class Test{
  public static void main(String args[]){

   //HashSet<String> hashSet = new HashSet<String>();
   //Set<String> set = new HashSet<String>();
   
  //别管就是转,方便
   Set<String> set = new HashSet<String>();

   boolean b1 = set.isEmpty();
   System.out.println(b1);

   set.add("a");
   set.add("b");
   set.add("c");
   set.add("d");
   set.add("c");
    
   boolean b2 = set.isEmpty();
   System.out.println(b2);
   
   int i = set.size();
   
   System.out.println("clear之前的长度"+i);

   set.clear();
   
   int j = set.size();

   System.out.println(j);

  }
}

取数据,迭代  iterate器 (Iterator)

public class Test{
public static void main(String args[]){
  //HashSet<String> hashSet = new HashSet<String>();
  //Set<String> set = hashSet;
  //Iterator <-- Collection <-- Set <-- HashSet
  //hasNext() next()

  Set<String> set = new HashSet<String>();
  
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");

  Iterator<String> it = set.iterator();

   boolean b1 = it.hasNext();
   if(b1){
    String s = it.next();
    System.out.println(s);
   }

   boolean b2 = it.hasNext();
   if(b2){
    String s = it.next();
    System.out.println(s);
   }

  }
}

迭代器的使用
it.hasNext();
还有没有下一个元素,如果这个游标后面有元素就返回true,否则,false;

it.next();
返回游标所指位置的下一个元素,取出,用hasNext()看有没有,next取

优化
public class Test{
public stattic void main(String args[]){
  Set<String> set = new HashSet<String>();
  
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");

  Iterator<String> it = set.iterator();

  while(it.hasNext()){
   String s = it.next();
   System.out.println(s);
 }
 }
}

 

什么是映射(Map)

每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。

 

public class Test{
public static void main(String args[]){
  HashMap<String,String> hasMap = new HashMap<String,String>();
  Map<String,String> map = hasMap;

  map.put("1","a");
  map.put("2","b");
  map.put("3","c");
  map.put("4","d");

  int i = map.size();
  System.out.println(i);
 }
}

public class Test{
public static void main(String args[]){
  HashMap<String,String> hasMap = new HashMap<String,String>();
  Map<String,String> map = hasMap;

  map.put("1","a");
  map.put("2","b");
  map.put("3","c");
  map.put("4","d");
  map.put("3","e");

  int i = map.size();
  System.out.println(i);
  
  String s = map.get("3");
  System.out.println(ss);
 }
}

 

equals函数的作用

equals比较两个对象的内容是否相等

 

什么是对象的内容相等

1.对象的类型相同(使用instanceof操作符比较);

2.两个对象的成员变量的值完全相同;

 

class Test{
public static void main(String[] args){
  User u1 = new User();
  User u2 = new User();
  
  boolean b1 = u1.equals(u2);
  System.out.pritln(b1);
 }
}

 

 

 

Java基础教程(全代码解析)_构造函数_04