锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

JavaSE基础篇

时间:2024-01-05 06:37:01 hs3一组常开10a继电器

Java基础

  • 一、介绍
    • 1、Java白皮书
    • 2、Java版本
  • 二、基础
    • 1、类结构和main函数
    • 2.基本类型和运算符
    • 3.选择和循环结构
    • 四、自定义函数
  • 三、对象与类
    • 1.面向对象思想
    • 2.面向对象的四个特征
    • 3、类和对象
    • 4、构造函数
    • 5.隐藏信息this
  • 四、继承
    • 1、继承
    • 2、抽象类和接口
    • 3.转型、多态和合同设计
  • 五、static、final和常量设计
    • 1、static
    • 2、单例模式
    • 3、final
    • 4、常量设计
    • 5、常量池
  • 六、package、import和classpath
    • 1、package
    • 2、import
    • 3、jar
    • 4.执行命令java文件
    • 5、Java访问权限
  • 七、异常
    • 1、异常分类
    • 2、异常处理
    • 自定义异常
  • 八、数据结构
    • 1、数组
    • 2、JCF
    • 3、List
      • ArrayList
      • LinkedList
      • ListCompare
      • Vector(同步)
    • 4、Set
      • HashSet
      • TreeSet
    • 5、Map
      • Hashtable
      • HashMap
      • LinkedHashMap & TreeMap
      • Properties
    • 6、JCF工具类
      • Arrays
      • Collections
      • Comparable.compareTo & Comparator.compare


此文章为JavaSE学习学习内容


一、介绍

1、Java白皮书

Java“白皮书”的关键术语:简单性、面向对象、分布式、健壮性、安全性、体系结构中立、可移植性、解释型、高性能、多线程、动态性

2、Java版本

Java版本:1.0、1.1、1.2、1.3、1.4、5.0(泛型类,for each自动装箱、枚举、静态导入 )、六、七、八(函数编程) 9(模板)


二、基础

1、类结构和main函数

在这里插入图片描述




2.基本类型和运算符










3.选择和循环结构

if-else

switch-case

int a = 1; switch(a){ 
          case 1: System.out.println("111111");    break; //满足后跳出case到default执行  case 2: System.out.println("222222");    break;  case 3: System.out.println("333333"); break; default: System.out.println("All need"); //不需要break,执行后自动跳出switch  } //输出结果是: 111111 All need 

while / do-while

for

break:中断循环并退出

continue:跳出本次循环,继续下次循环

4、自定义函数


三、对象与类

1、面向对象思想



2、面向对象四大特性

3、类和对象


对象赋值和基本类型赋值


4、构造函数





5、信息隐藏和this


四、继承

1、继承




2、抽象类和接口






子类在调用父类时 无论自己有没有构造方法都会先去执行父类无参的函数。哪怕父类是抽象类。虽然抽象类不能被实例化,但是可以在构造方法中初始化一些参数;也可以在子类中调用父类的构造方法。

3、转型、多态和契约设计





public class AnimalTest { 
        

	public static void haveLunch(Animal a)	{ 
        
		a.eat();
	}
	
	public static void main(String[] args) { 
        
		Animal[] as = new Animal[4];
		as[0] = new Cat();
		as[1] = new Dog();
		as[2] = new Cat();
		as[3] = new Dog();
		
		for(int i=0;i<as.length;i++) { 
        
			as[i].move();  //调用每个元素的自身的move方法
		}
		for(int i=0;i<as.length;i++) { 
        
			haveLunch(as[i]);
		}
		
		haveLunch(new Cat());  //Animal a = new Cat(); haveLunch(a);
		haveLunch(new Dog());
		haveLunch(
				new Animal()
				{ 
        
					public void eat() { 
        
						System.out.println("I can eat from an anonymous class");						
					}
					public void move() { 
        
						System.out.println("I can move from an anonymous class");
					}
				});
	}
}
输出结果:
Cat: I can move
Dog: I can move
Cat: I can move
Dog: I can move
Cat: I can eat
Dog: I can eat
Cat: I can eat
Dog: I can eat
Cat: I can eat
Dog: I can eat
I can eat from an anonymous class

五、static、final和常量设计

1、static



public class Potato { 
        
	static int price = 5;
	String content = "";
	public Potato(int price, String content)
	{ 
        
		this.price = price;
		this.content = content;
	}	
	public static void main(String[] a)
	{ 
        
		System.out.println(Potato.price); //Potato.content wrong
		System.out.println("----------------------------------");
		Potato obj1 = new Potato(10,"青椒土豆丝");
		System.out.println(Potato.price);
		System.out.println(obj1.price);
		
		System.out.println("----------------------------------");
		Potato obj2 = new Potato(20,"酸辣土豆丝");
		System.out.println(Potato.price);
		System.out.println(obj2.price);
		
	}
}

输出结果是:
5
--------------------------------
10
10
--------------------------------
20
20



public class StaticMethodTest { 
        
	int a = 111111;
	static int b = 222222;
	public static void hello()
	{ 
        
		System.out.println("000000");
		System.out.println(b);
		//System.out.println(a); //error, cannot call non-static variables
		//hi() //error, cannot call non-static method
	}
	public void hi()
	{ 
        
		System.out.println("333333");
		hello();                  //ok, call static methods
		System.out.println(a);    //ok, call non-static variables
		System.out.println(b);    //ok, call static variables
	}
	public static void main(String[] a)
	{ 
        
		StaticMethodTest.hello();
		//StaticMethodTest.hi(); //error, 不能使用类名来引用非静态方法
		StaticMethodTest foo = new StaticMethodTest();
		foo.hello();  //warning, but it is ok
		foo.hi();     //right
	}
}


public class StaticBlockTest { 
        

	public static void main(String[] args) { 
        
		System.out.println("000000000000000");
		// TODO Auto-generated method stub
		StaticBlock obj1 = new StaticBlock();
		StaticBlock obj2 = new StaticBlock();
	}

}


class StaticBlock
{ 
        
	//staticl block > anonymous block > constructor function 
	static
	{ 
        
		System.out.println("22222222222222222222");
	}
	{ 
        
		System.out.println("11111111111111111111");
	}
	public StaticBlock()
	{ 
        
		System.out.println("33333333333333333333");
	}
	{ 
        
		System.out.println("44444444444444444444");
	}
}
输出结果是:
000000000000000
22222222222222222222
11111111111111111111
44444444444444444444
33333333333333333333
11111111111111111111
44444444444444444444
33333333333333333333

2、单例模式



单例模式:外部不允许new(private 构造函数),内部只允许new一次(static 对象)。


public class Singleton { 
        
	private static Singleton obj = new Singleton(); //共享同一个对象
	private String content;
	
	private Singleton()  //确保只能在类内部调用构造函数
	{ 
        
		this.content = "abc";
	}
	
	public String getContent() 	{ 
        
		return content;
	}
	public void setContent(String content) { 
        
		this.content = content;
	}	
	
	public static Singleton getInstance()	{ 
        
		//静态方法使用静态变量
		//另外可以使用方法内的临时变量,但是不能引用非静态的成员变量
		return obj;
	}
	
	
	public static void main(String[] args) { 
        
		Singleton obj1 = Singleton.getInstance();
		System.out.println(obj1.getContent());  //abc
		
		Singleton obj2 = Singleton.getInstance();
		System.out.println(obj2.getContent());  //abc
		
		obj2.setContent("def");
		System.out.println(obj1.getContent());
		System.out.println(obj2.getContent());
		
		System.out.println(obj1 == obj2); //true, obj1和obj2指向同一个对象
	}

}

输出结果是:
abc
abc
def
def
true

3、final


4、常量设计


5、常量池



Integer


public class BoxClassTest { 
        
	public static void main(String[] args)
	{ 
        
		int i1 = 10;
		Integer i2 = 10;                // 自动装箱
		System.out.println(i1 == i2);   //true
		// 自动拆箱 基本类型和包装类进行比较,包装类自动拆箱
		
		Integer i3 = new Integer(10);
		System.out.println(i1 == i3);  //true
		// 自动拆箱 基本类型和包装类进行比较,包装类自动拆箱
		
		System.out.println(i2 == i3); //false
		// 两个对象比较,比较其地址。 
		// i2是常量,放在栈内存常量池中,i3是new出对象,放在堆内存中
		
		Integer i4 = new Integer(5);
		Integer i5 = new Integer(5);
		System.out.println(i1 == (i4+i5));   //true
		System.out.println(i2 == (i4+i5));   //true
		System.out.println(i3 == (i4+i5));   //true
		// i4+i5 操作将会使得i4,i5自动拆箱为基本类型并运算得到10. 
		// 基础类型10和对象比较, 将会使对象自动拆箱,做基本类型比较
		
		Integer i6 = i4 + i5;  // +操作使得i4,i5自动拆箱,得到10,因此i6 == i2.
		System.out.println(i1 == i6);  //true
		System.out.println(i2 == i6);  //true
		System.out.println(i3 == i6);  //false
	}	
}

String



public class StringNewTest { 
        
	public static void main(String[] args) { 
        
		String s0 = "abcdef";
		String s1 = "abc";
		String s2 = "abc";
		String s3 = new String("abc");
		String s4 = new String("abc");
		System.out.println(s1 == s2); //true 常量池
		System.out.println(s1 == s3); //false 一个栈内存,一个堆内存
		System.out.println(s3 == s4); //false 两个都是堆内存
		System.out.println("=========================");
		
		String s5 = s1 + "def";    //涉及到变量,故编译器不优化
		String s6 = "abc" + "def"; //都是常量 编译器会自动优化成abcdef
		String s7 = "abc" + new String ("def");//涉及到new对象,编译器不优化
		System.out.println(s5 == s6); //false
		System.out.println(s5 == s7); //false
		System.out.println(s6 == s7); //false
		System.out.println(s0 == s6); //true 
		System.out.println("=========================");

		
		String s8 = s3 + "def";//涉及到new对象,编译器不优化
		String s9 = s4 + "def";//涉及到new对象,编译器不优化
		String s10 = s3 + new String("def");//涉及到new对象,编译器不优化
		System.out.println(s8 == s9); //false
		System.out.println(s8 == s10); //false
		System.out.println(s9 == s10); //false
	}
}







六、package、import和classpath

1、package


2、import



3、jar


4、命令行执行java文件


5、Java访问权限


A.java

package test1;

public class A { 
        
	private int v1 = 1;
	int v2 = 2;
	protected int v3 = 3;
	public int v4 = 4;
	
	private void showV1() 
	{ 
        
		System.out.println(v1);
	}
	void showV2()
	{ 
        
		System.out.println(v2);
	}
	protected void showV3()
	{ 
        
		System.out.println(v3);
	}
	public void showV4()
	{ 
        
		System.out.println(v4);
	}
}

B.java:B和A在同一个包下,但没有继承关系,因此B只能通过new方式访问A中private以外的变量和方法

package test1;

//B and A are in the same package
public class B { 
        
	public void show()
	{ 
        
		//B is not subclass of A
// System.out.println(v1); //error
// System.out.println(v2); //error
// System.out.println(v3); //error
// System.out.println(v4); //error 
// showV1(); //error
// showV2(); //error
// showV3(); //error
// showV4(); //error
		
		A obj = new A();
		//System.out.println(obj.v1); error, private
		System.out.println(obj.v2);
		System.out.println(obj.v3);
		System.out.println(obj.v4);
		
		//obj.showV1(); error, private
		obj.showV2( 

相关文章