- 第1章 初次接触Java
- 第2章 Java语言基础
- 第4章 面向对象(上)
- 第5章 面向对象(中)
- 第6章 面向对象(下)
- 第7章 异常
- 第8章 Java常用类库与工具
- 第9章 线程
- 第10章 集合类
- 第14章 I/O 输入/输出
- 第15章 Java网络通信
- 第16章 JDBC
1.弄懂 JRE、JDK、JVM 之间的区别与联系
JVM :英文名称(Java Virtual Machine),就是我们耳熟能详的 Java 虚拟机。它只认识 xxx.class 这种类型的文件,它能够将 class 文件中的字节码指令进行识别并调用操作系统向上的 API 完成动作。所以说,jvm 是 Java 能够跨平台的核心,具体的下文会详细说明。
JRE :英文名称(Java Runtime Environment),我们叫它:Java 运行时环境。它主要包含两个部分,jvm 的标准实现和 Java 的一些基本类库。它相对于 jvm 来说,多出来的是一部分的 Java 类库。
JDK :英文名称(Java Development Kit),Java 开发工具包。jdk 是整个 Java 开发的核心,它集成了 jre 和一些好用的小工具。例如:javac.exe,java.exe,jar.exe 等。
显然,这三者的关系是:一层层的嵌套关系。JDK>JRE>JVM。
2.Java 为什么能跨平台,实现一次编写,多处运行?
Java 能够跨平台运行的核心在于 JVM 。不是 Java 能够跨平台,而是它的 jvm 能够跨平台。我们知道,不同的操作系统向上的 API 肯定是不同的,那么如果我们想要写一段代码调用系统的声音设备,就需要针对不同系统的 API 写出不同的代码来完成动作。
而 Java 引入了字节码的概念,jvm 只能认识字节码,并将它们解释到系统的 API 调用。针对不同的系统有不同的 jvm 实现,有 Linux 版本的 jvm 实现,也有 Windows 版本的 jvm 实现,但是同一段代码在编译后的字节码是一样的。引用上面的例子,在 Java API 层面,我们调用系统声音设备的代码是唯一的,和系统无关,编译生成的字节码也是唯一的。但是同一段字节码,在不同的 jvm 实现上会映射到不同系统的 API 调用,从而实现代码的不加修改即可跨平台运行。
第2章 Java语言基础1.数 据 类 型
(1)基本类型(8种)
整数类型:byte(8位), short(16位), int(32位), long(64位)
浮点类型:float(32位), double(64位)
字符类型:char(16位)
布尔类型:boolean
(2)复合类型
class(类) interface(接口) 数组
2.类型转换 3.逻辑表达式(布尔值,不能是int等) 4.switch(expression)中的expression新版本类型都可以 5.break lab; 跳出多重循环
第4章 面向对象(上)1.对象互发消息
(1)引用必须引用了特定的对象,否则会在运行时抛出NullPointerException异常 (2)对象必须定义了相应的属性或方法,否则编译不会通过 (3)被访问的属性或方法具有可访问的权限
2.区别对象和对象引用
(1)创建对象: new IntClass();
对象分配在堆上。
(2)声明一个对象引用: IntClass ic;
对象引用分配在栈上。
(3)初始化对象引用: ic = new IntClass();
例1: 对象内存空间分布: IntClass ic = new IntClass(); 实质是将创建的IntClass对象的地址赋给对象引用ic,从此ic与该对象关联,通过ic即可操纵该对象。
例2: 对象作为参数的特点: 普通数据类型作为参数传递是值传递,而对象是引用传递。
public class Test{
private static int a;
public static void main(String [] args){
modify(a);
System.out.println(a);
}
public static void modify(int a){
a++;
}
}
本程序输出为0,因为a++是对形式参数进行自增,而不是类属性 a进行自增。
例3: 对象的引用传递举例: 对象是引用传递,当对象作为参数传递时,传递的是对象的地址。
class IntClass{
int value;
}
public class test {
private static int a;
public static void main(String [] args){
IntClass a = new IntClass();
modify(a,8);
System.out.println(a.value);
}
public static void modify(IntClass s, int val){
s.value = val;
}
}
输出:8
3.区别Java和C++
Java :只有当你使用 new 操作符时,才会真正在内存中申请一块空间,创建一个新对象,并将该对象绑定到你所定义的变量名上。其它情况下,要么是将已有对象绑定到某个变量名上,要么就是定义的变量名是个空引用,没有绑定任何对象。 也就是说,定义变量名只是创建了一个新的标识符,跟创建对象没有关系,创建对象必须通过 new 来完成,只有创建对象时才会申请内存空间。
C++ :当你定义了一个变量 s 时,即使你没有给它赋值,也意味着你不但创建了一个新的标识符,同时还在栈中申请了对应的内存空间。 因此,C++ 中定义的变量名不仅仅是个标识符,还自动关联着栈中的一块内存空间。 而 C++ 中的 new 操作符表示的是在堆中申请内存,因为栈中的内存在运行时期大小是固定且有限的,因此需要动态内存分配的时候就需要用 new 来实现。这类似于 C 里面的 malloc 函数,只不过 new 操作符还封装了其它的操作。
总结而言,Java 中的变量名仅仅是一个用于引用内存中实际对象的标识符,如果你没给它关联对象,它就为空引用。而 C++ 中的变量名(非指针类型),虽然也是标识符,但却始终关联着实际的内存空间,当我们看到一个变量(非指针类型)时,就知道它代表着一块实际的内存空间。
4.创建数组(数组是对象)
1.一维数组
A.声明 (1)int
int[] test_int;
(2)String
String[] test_String;
(3)自定义类
MyClass[] test_mc;
(4)对象数组
Integer[] test_Integer;
B.初始化 a.使用关键字new进行定义 类型标识符[] 数组名 = new 类型标识符[数组长度];
(1)int 产生一个具有10个单元,类型为 int 的数组对象,所有单元的初值为0
int[] test_int = new int[10];
或
int[] test_int;
test_int = new int[10];
(2)String 产生一个具有10个单元,类型为 String 的数组对象,所有单元的初值为null
String[] test_String = new String[10];
或
String[] test_String;
test_String = new String[10]; // 注意:不要写成 new String(10)
(3)自定义类 产生一个具有10个单元,类型为 MyClass 的数组对象,所有单元的初值为null
MyClass[] test_mc = new MyClass[10];
或
MyClass[] test_mc;
test_mc = new MyClass[10];
(4)对象数组 产生一个具有10个单元,类型为 Integer 的数组对象,所有单元的初值为null
Integer[] test_Integer = new Integer[10];
或
Integer[] test_Integer;
test_Integer = new Integer[10];
b. 直接在声明的时候进行初始化 注意:这种定义方式只能在一行代码中,不能分开。
(1)int
int[] test_int = {1,2,3};
(2)String
或
String[] test_String = {"ab","cd","ef"};
或
String[] test_String = {new String("ab"),new String("cd"),new String("ef")};
(3)自定义类
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
(4)对象数组
Integer[] test_Integer = {3,5};
或
Integer[] test_Integer = {new Integer(3), new Integer(5)};
c. 采用如下方法定义及初始化 (1)int
(2)String String[] test_String = new String[] {“ab”,“cd”,“ef”}; 或
int[] test_int = new int[] {1,2,3};
(2)String
或
String[] test_String = new String[] {"ab","cd","ef"};
或
String[] test_String = new String[] {new String("ab"), new String("cd"), new String("ef")};
(3)自定义类
(4)对象数组 Integer[] test_Integer = new Integer[] {1,2,3}; 或
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = new MyClass[] {test_mc1,test_mc2,test_mc3};
(4)对象数组
或
Integer[] test_Integer = new Integer[] {1,2,3};
或
Integer[] test_Integer = new Integer[] {new Integer(1), new Integer(2), new Integer(3)};
C.使用foreach语句遍历一维数组 (1)int
public class MyClass {
public static void main(String[] args) {
int[] test_int = new int[10];
test_int[0] = 1;
for(int x: test_int) {
System.out.println(x + " ");
}
}
}
输出:1 0 0 0 0 0 0 0 0 0
(2)String
public class MyClass {
public static void main(String[] args) {
String[] test_String = {"ab","cd","ef"};
for(String x: test_String) {
System.out.print(x + " ");
}
}
}
输出:ab cd ef
(3)自定义类
public class MyClass {
public int value = 1;
public static void main(String[] args) {
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
for(MyClass x: test_mc) {
System.out.print(x.value + " ");
}
System.out.println();
for(MyClass x: test_mc) {
System.out.print(x + " ");
}
}
}
输出:1 1 1 test . MyClass@15db9742 test. MyClass@6d06d69c test . MyClass@7852e922
(4)对象数组
public class MyClass {
public int value = 1;
public static void main(String[] args) {
Integer[] test_Integer = {new Integer(3), new Integer(5)};
for(Integer x: test_Integer) {
System.out.print(x + " ");
}
}
}
输出:3 5
2.二维数组
A.声明 (1)int
int[][] test_int;
(2)String
String[][] test_String;
(3)自定义类
MyClass[][] test_mc;
(4)对象数组
Integer[][] test_Integer;
B.初始化 数组名 = new 类型说明符[数组长度][];
数组名 = new 类型说明符[数组长度][数组长度];
对于没有初始化的维度,其值为null:
int arr[][];
arr = new int[3][4];
其相当于下述4条语句:
arr = new int[3][]; // 创建一个有3个元素的数组,且每个元素也是一个数组
arr[0] = new int[4]; // 创建arr[0]元素的数组,它有4个元素
arr[1] = new int[4]; // 创建arr[1]元素的数组,它有4个元素
arr[2] = new int[4]; // 创建arr[2]元素的数组,它有4个元素
其等价于:
arr = new int[3]\[];
for(int i=0;i 安全性问题 --> 同步机制(synchronized原子性)–> 上锁、死锁
第10章 集合类
1.两大集合框架:Collection、Map
2.![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mjzKCore-1621734135139)(C:\Users\User\AppData\Roaming\Typora\typora-user-images\image-20210523092818578.png)]](https://img-blog.csdnimg.cn/20210523094823536.png)
3.![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0YAOStKY-1621734135146)(C:\Users\User\AppData\Roaming\Typora\typora-user-images\image-20210523092826703.png)]](https://img-blog.csdnimg.cn/20210523094832880.png)
4.Map的实现类:Hashtable、HashMap、TreeMap
5.Map遍历:
Set keys = map.keySet();
if(keys!=null){
Iterator iterator = keys.iterator();
while(iterator.hasNext()){
Object key = iterator.next();
Object value = map.get(key);
...;
}
}
Set entries = map.entrySet();
if(entries!=null){
Iterator iterator = entries.iterator();
while(iterator.hasNext()){
Map.Entry entry = iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
...;
}
}
第14章 I/O 输入/输出
1.标准输入:对象是键盘,Java对应类是System.in 标准输出:对象是屏幕,Java对应类是System.out 标准错误输出:对象也是屏幕,Java对应类是System.err
2.字节流
(1)InputStream
int read(byte b[ ]):读多个字节到数组中,返回读到的字节数目,如果读完则返回-1 int read(byte b[ ], int off, int len):从输入流中读取长度为len的数据,写入数组b中从索引off开始的位置,并返回读取的字节数,如果读完则返回-1
(2)OutputStream write(byte b[ ]):将字节数组中的数据输出到流中 write(byte b[ ], int off, int len):将数组b从off指定的位置开始,长度为len的数据输出到流中 flush():刷空输出流,并将缓冲区中的数据强制送出
3.ByteArrayInputStream构造方法:public ByteArrayInputStream(byte[] buf)——将字节数组作为字节流的数据源
public class ByteArrayStream {
public static void main(String[] args) {
byte[] b = "hello".getBytes(); // String->byte[]
ByteArrayInputStream bais = new ByteArrayInputStream(b);
int n = 0;
while((n=bais.read())!=-1) {
System.out.print((char)n); // hello
}
}
}
4.ByteArrayOutputStream构造方法:public ByteArrayOutputStream()——构造一个字节数组输出流,用于将多少字节写入到流中,最后可以整体转换为一个字节数组
public class ByteArrayStream {
public static void main(String[] args) {
byte[] b = "hello".getBytes(); // String->byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(b,0,b.length); // 把b中length长度的字节数组写到输出流上
System.out.println(new String(baos.toByteArray()));
}
}
5.FileInputStream构造方法:FileInputStream(String name)——以文件路径名字构造一个文件输入流,打开一个与实际文件的连接,用于从该流中读取文件字节流 6.FileOutputStream构造方法:FileOutputStream(String name)——以文件路径名字构造一个文件输出流,打开一个与实际文件的连接,用于文件的写字节流操作 7.PipedInputStream和PipedOutputStream:通常用于将一个程序的输出连接到另一个程序的输入。 输出流作为管道的发送端,输入流作为管道的接收端。 使用前需要调用connect方法将输出流和输入流连接起来。 通常一个线程执行管道输出流的写操作,另一个线程执行管道输入流的读操作。
public class PipedStream{
public static void main(String[] args) throws IOEception {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
new Thread(new Input(in)).start();
new Thread(new Output(out)).start();
}
}
class Input implements Runnable {
private PipedInputStream in;
public Input(PipedInputStream in) {
this.in = in;
}
public void run() {
byte[] buf = new byte[1024];
int len;
try {
len = in.read(buf);
String s = new String(buf, 0, len);
System.out.println("in "+s);
in.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable {
private PipedOutputStream out;
public Output(PipedOutputStream out) {
this.out = out;
}
public void run() {
try {
out.write("hello".getBytes());
} catch(IOException e) {
e.printStackTrace();
}
}
}
8.字节流 --> 字符流:InputStreamReader(InputStream in)
InputStreamReader ins = new InputStreamReader(System.in);
9.字符流 --> 字节流:OutputStreamWriter(OutputStream out)或PrintWriter(OutputStream out)
OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream("test.txt"));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), true);
out.println("Hello"); // 输出字符串“Hello”
10.过滤流BufferedReader的使用:用于缓存字符流,可以一行一行的读(只要是Reader的之类都可以作为BufferedReader的参数)
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
String c1;
int i = 0;
int[] e = new int[10];
while(i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?