方法重载53,54
构造方法55,56
静态方法
静态代码块执行与顺序有关
继承
instanceof可以判断是否为空
protected
final
继承里的静态方法
native修饰
比较对象 用equals方法
Class类
反射!!!
面向对象3要素
枚举enum
接口
异常Throwable
泛型
注解123
Lambda
@Test
public void copyFromLocalBigFile() throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(new File("/Users/andy/Downloads/bigdata/software/apache-maven-3.6.1-bin.tar.gz")));
FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/maven.tgz"),
new Progressable() {
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in, out ,4096);
}
快捷键
插件
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
https://github.com/alibaba/druid/
#Java#
编写 Java 程序时,应注意以下几点:
Java 所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。 关于 Java 标识符,有以下几点需要注意:
像其他语言一样,Java可以使用修饰符来修饰类中方法和属性。主要有两类修饰符:
Java 中主要有如下几种类型的变量
类可以看成是创建Java对象的模板。 通过下面一个简单的类来理解下Java中类的定义:
public class Dog{
String breed;
int age;
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
一个类可以包含以下类型变量:
对象是根据类创建的。在Java中,使用关键字new来创建一个新的对象。创建对象需要以下三步:
public class Puppy{
public Puppy(String name){
//这个构造器仅有一个参数:name
System.out.println(“小狗的名字是 : “ + name );
}
public static void main(String[] args){
// 下面的语句将创建一个Puppy对象
Puppy myPuppy = new Puppy( “Tommy” );
}
}
通过已创建的对象来访问成员变量和成员方法,如下所示:
/* 实例化对象 */
Object referenceVariable = new Constructor();
/* 访问类中的变量 */
referenceVariable.variableName;
/* 访问类中的方法 */
referenceVariable.methodName();
下面的例子展示如何访问实例变量和调用成员方法:
public class Puppy{
int puppyAge;
public Puppy(String name){
// 这个构造器仅有一个参数:name
System.out.println(“小狗的名字是 : “ + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( ){
System.out.println(“小狗的年龄为 : “ + puppyAge );
return puppyAge;
}
public static void main(String[] args){
/* 创建对象 */
Puppy myPuppy = new Puppy( “Tommy” );
/* 通过方法来设定age */
myPuppy.setAge( 2 );
/* 调用另一个方法获取age */
myPuppy.getAge( );
/*你也可以像下面这样访问成员变量 */
System.out.println(“变量值 : “ + myPuppy.puppyAge );
}
}
在本节的最后部分,我们将学习源文件的声明规则。当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则。
Java 的两大数据类型:
常量在程序运行时是不能被修改的。 在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似:
final double PI = 3.1415927;
整型、实型(常量)、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。 转换从低级到高级。
低 ------------------------------------> 高
byte,short,char—> int —> long—> float —> double
数据类型转换必须满足如下规则:
必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型,同样float数据类型的位数为32,可以自动转换为64位的double类型
public class ZiDongLeiZhuan{
public static void main(String[] args){
char c1=‘a’;//定义一个char类型
int i1 = c1;//char自动类型转换为int
System.out.println(“char自动类型转换为int后的值等于”+i1);
char c2 = ‘A’;//定义一个char类型
int i2 = c2+1;//char 类型和 int 类型计算
System.out.println(“char类型和int计算后的值等于”+i2);
}
}
public class QiangZhiZhuanHuan{
public static void main(String[] args){
int i1 = 123;
byte b = (byte)i1;//强制类型转换为byte
System.out.println(“int强制类型转换为byte后的值等于”+b);
}
}
在Java语言中,所有的变量在使用前必须声明。声明变量的基本格式如下:
type identifier [ = value][, identifier [= value] …] ;
// example
int a, b, c; // 声明三个int型整数:a、 b、c
Int d = 3, e = 4, f = 5; // 声明三个整数并赋予初值
Byte z = 22; // 声明并初始化 z
String s = “runoob”; // 声明并初始化字符串 s
Double pi = 3.14159; // 声明了双精度浮点型变量 pi
Char x = ‘x’; // 声明变量 x 的值是字符 ‘x’。
Java语言支持的变量类型有:
public class Variable{
static int allClicks=0; // 类变量
String str=“hello world”; // 实例变量
public void method(){
Int i =0; // 局部变量
}
}
import java.io.*;
public class Employee{
// 这个实例变量对子类可见
public String name;
// 私有变量,仅在该类可见
private double salary;
//在构造器中对name赋值
public Employee (String empName){
name = empName;
}
//设定salary的值
public void setSalary(double empSal){
salary = empSal;
}
// 打印信息
public void printEmp(){
System.out.println(“名字 : “ + name );
System.out.println(“薪水 : “ + salary);
}
public static void main(String[] args){
Employee empOne = new Employee(“RUNOOB”);
empOne.setSalary(1000.0);
empOne.printEmp();
}
}
import java.io.*;
public class Employee {
//salary是静态的私有变量
private static double salary;
// DEPARTMENT是一个常量
public static final String DEPARTMENT = “开发人员”;
public static void main(String[] args){
salary = 10000;
System.out.println(DEPARTMENT+”平均工资:”+salary);
}
}
Java语言提供了很多修饰符,主要分为以下两类:
public class ClassName {
// …
}
private boolean myFlag;
static final double weeks = 9.5;
protected static final int BOXWIDTH = 42;
public static void main(String[] arguments) {
// 方法体
}
Java中,可以使用访问控制符来保护对类、变量、方法和构造方法的访问。Java 支持 4 种不同的访问权限。
本类 | 同包 | 子类 | 任意位置 | |
---|---|---|---|---|
public | ||||
protected | N | |||
默认 | N | N | ||
private | N | N | N |
请注意以下方法继承的规则:
父类中声明为 public 的方法在子类中也必须为 public。
父类中声明为 protected 的方法在子类中要么声明为 protected,要么声明为 public,不能声明为 private。
父类中声明为 private 的方法,不能够被继承。
为了实现一些其他的功能,Java 也提供了许多非访问修饰符。
静态变量: Static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。 静态变量也被称为类变量。局部变量不能被声明为 static 变量。
静态方法: Static 关键字用来声明独立于对象的静态方法。静态方法不能使用类的非静态变量。静态方法从参数列表得到数据,然后计算这些数据。
对类变量和方法的访问可以直接使用 classname.variablename 和 classname.methodname 的方式访问。
public class InstanceCounter {
private static int numInstances = 0;
protected static int getCount() {
return numInstances;
}
private static void addInstance() {
numInstances++;
}
InstanceCounter() {
InstanceCounter.addInstance();
}
public static void main(String[] arguments) {
System.out.println(“Starting with “ +
InstanceCounter.getCount() + “ instances”);
for (int I = 0; I < 500; ++i){
new InstanceCounter();
}
System.out.println(“Created “ +
InstanceCounter.getCount() + “ instances”);
}
}
public class Test{
final int value = 10;
// 下面是声明常量的实例
public static final int BOXWIDTH = 6;
static final String TITLE = “Manager”;
public void changeValue(){
value = 12; //将输出一个错误
}
}
public class Test{
public final void changeName(){
// 方法体
}
}
抽象类:
abstract class Caravan{
private double price;
private String model;
private String year;
public abstract void goFast(); //抽象方法
public abstract void changeColor();
}
抽象方法
Synchronized 关键字声明的方法同一时间只能被一个线程访问。synchronized 修饰符可以应用于四个访问修饰符。
序列化的对象包含被 transient 修饰的实例变量时,java 虚拟机(JVM)跳过该特定的变量。 该修饰符包含在定义变量的语句中,用来预处理类和变量的数据类型。
Volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。 一个 volatile 对象引用可能是 null。
计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量。我们可以把运算符分成以下几组:
条件运算符也被称为三元运算符。该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量
variable x = (expression) ? value if true : value if false
public class Test {
public static void main(String[] args){
int a , b;
a = 10;
// 如果 a 等于 1 成立,则设置 b 为 20,否则为 30
b = (a == 1) ? 20 : 30;
System.out.println( “Value of b is : “ + b );
// 如果 a 等于 10 成立,则设置 b 为 20,否则为 30
b = (a == 10) ? 20 : 30;
System.out.println( “Value of b is : “ + b );
}
}
该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。 instanceof运算符使用格式如下:
( Object reference variable ) instanceof (class/interface type)
String name = “James”;
boolean result = name instanceof String; // 由于 name 是 String 类型,所以返回真
顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,,就需要使用循环结构。 Java中有三种主要的循环结构:
While是最基本的循环,它的结构为:
while( 布尔表达式 ) {
//循环内容
}
对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。 Do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
do {
//代码语句
}while(布尔表达式);
虽然所有循环结构都可以用 while 或者 do…while表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。 For循环执行的次数是在执行前就确定的。语法格式如下:
for(初始化; 布尔表达式; 更新) {
//代码语句
}
关于 for 循环有以下几点说明:
一个 if 语句包含一个布尔表达式和一条或多条语句。
If 语句的语法如下:
if(布尔表达式)
{
//如果布尔表达式为true将执行的语句
}else{
//如果布尔表达式的值为false
}
If 语句后面可以跟 else if…else 语句,这种语句可以检测到多种可能的情况。 使用 if,else if,else 语句的时候,需要注意下面几点:
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
Switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch case 语句语法格式如下:
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
getClass().getName() + '@' + Integer.toHexString(hashCode())
// 默认实现
// 使用==判断 这个判断2个java对象内存地址
public boolean equals (Object obj) {
return (this == obj);
}
class MyTime {
int year;
int month;
int day;
public MyTime() {}
public MyTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
public Boolean equals(Object obj) {
int year1 = this.year;
int month1 = this.month;
int day1 = this.day;
if (obj instanceof MyTime) {
MyTime t = (MyTime) obj;
int year2 = t.year;
int month2 = t.month;
int day2 = t.day;
}
if (year1 == year2 && month1 == month2 && day1 == day2) {
return true;
}
return false;
}
public Boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof MyTime)) {
return false;
}
// t1.equals(t1); 内存地址相同
if (this == obj) {
return true;
}
MyTime t = (MyTime) obj;
if (this.year == t.year && this.month == t.month && this.day == t.day) {
return true;
}
return false;
}
public static void main(String[] args) {
// 大部分情况下,采用这样方式创建字符串
String a = "a";
// String也是一个类,不属于基本数据类型
// 存在构造方法
String b = new String("test");
String c = new String("test");
System.out.println(b == c); // false
// == 内存地址比较 基本数据类型可以
// 比较字符串 用equals方法
System.out.println(b.equals(c));
// String 重写toString方法
System.out.println(b.toString());
System.out.println(b);
}
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize
method to dispose of system resources or to perform other cleanup.
只有一个方法体,里面没有代码,JVM垃圾回收器调用
实际上Sun公司为程序员准备的一个时机,垃圾销毁时机。
如果希望在对象销毁执行一段代码,写在finalize() 中
静态代码块
// 在类加载时刻执行,只执行一次
// 这是Sun准备的类加载时机
static {
}
Java的GC不是随时启动,有条件
System.gc(); // 建议启动GC
内部类
分类
使用内部类可读性差
匿名内部类
public class test01 {
/**
* 因为static 静态内部类
*/
static class Inner1 {}
/**
* 没有static 实例内部类
*/
class Inner2 {}
public void doSome() {
/**
* 局部变量
*/
int a = 30;
/**
* 局部内部类
*/
class Inner3 {
}
}
// 匿名内部类
myMath.MySum(new Compute() {
@Override
public int sum(int a, int b) {
return a-b;
}
}, 100, 200);
}
属于引用数据类型,父类Object
是一个容器,同时容纳多个元素
数组是引用类型,数组对象存储在堆内存
数组中存java对象,存的是内存地址
数组一旦创建,长度不可变
java要求数组元素类型统一
有length属性
数组存的内存地址是连续的(数据结构)
所有的数组都是拿第一个元素的内存地址作为数组对象的内存地址
优点(检索效率高)
缺点
初始化
// 静态初始化
int[] array1 = {1,2,3,4,5};
// 动态 默认值 0
int[] array2 = new int[5];
数据类型 | 默认值 |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0F |
double | 0.0 |
boolean | false |
char | \u0000 |
引用数据类型 | null |
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MM {
private String name;
private int cup;
public static void main(String[] args) {
MM mm1 = new MM("a1", 1);
MM mm2 = new MM("a2", 2);
MM mm3 = new MM("a3", 3);
MM mm4 = new MM("a4", 4);
ArrayList<MM> mms = new ArrayList<>();
mms.add(mm1);
mms.add(mm2);
mms.add(mm3);
mms.add(mm4);
/*
比较方法
*/
Collections.sort(mms, new Comparator<MM>() {
@Override
public int compare(MM o1, MM o2) {
return o1.cup - o2.cup;
}
});
for (MM mm : mms) {
System.out.println(mm);
}
}
// 以下:常规方法
public MM(String name, int cup) {
this.name = name;
this.cup = cup;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCup() {
return cup;
}
public void setCup(int cup) {
this.cup = cup;
}
@Override
public String toString() {
return "MM{" +
"name='" + name + '\'' +
", cup=" + cup +
'}';
}
}
package com.ruozedata.spark.day02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MM implements Comparable<MM> {
private String name;
private int cup;
public static void main(String[] args) {
MM mm1 = new MM("a1", 1);
MM mm2 = new MM("a2", 2);
MM mm3 = new MM("a3", 3);
MM mm4 = new MM("a4", 4);
ArrayList<MM> mms = new ArrayList<>();
mms.add(mm1);
mms.add(mm2);
mms.add(mm3);
mms.add(mm4);
for (MM mm : mms) {
System.out.println(mm);
}
}
public MM(String name, int cup) {
this.name = name;
this.cup = cup;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCup() {
return cup;
}
public void setCup(int cup) {
this.cup = cup;
}
@Override
public String toString() {
return "MM{" +
"name='" + name + '\'' +
", cup=" + cup +
'}';
}
@Override
public int compareTo(MM o) {
return this.cup - o.cup;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 字节流读写
*/
public class InputOutStreamApp {
public static void main(String[] args) {
// read();
write();
}
private static void write() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("data/wc.data");
fos = new FileOutputStream("data/wc.txt");
byte[] buffer = new byte[1024];
int length = 0;
while ((length=fis.read(buffer, 0, buffer.length)) != -1) {
fos.write(buffer, 0, length);
fos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void read() {
FileInputStream fis = null;
try {
fis = new FileInputStream("data/wc.data");
byte[] buffer = new byte[1024];
int length = 0;
while ((length=fis.read(buffer, 0, buffer.length)) != -1) {
String result = new String(buffer, 0 ,length);
System.out.println(result);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.String;
public class ReadWriterApp {
public static void main(String[] args) {
read();
write();
}
private static void write() {
FileWriter writer = null;
try {
writer = new FileWriter("data/wc2.data");
writer.write("ssss");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void read() {
FileReader reader = null;
try {
reader = new FileReader("data/wc.data");
char[] buffer = new char[1024];
int length = 0;
while ((length = reader.read(buffer, 0 ,buffer.length)) != -1) {
String result = new String(buffer, 0, length);
System.out.println(result);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
public class BufferedApp {
public static void main(String[] args) {
read();
writer();
}
private static void writer() {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data/wc3.data", true)));
writer.write("zzzzz.xxxx...\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void read() {
// 字符流 文件读进来可以是字节流 字节流=> 字符流
// InputStreamReader 装饰器模式
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("data/wc.data")));
String message = null;
while ((message = reader.readLine()) != null) {
System.out.println(message);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package io;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
final int PORT = 8888;
final String HOST = "localhost";
Socket socket = null;
try {
socket = new Socket(HOST, PORT);
// 创建IO相关
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 输入内容的流
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String requestMessage = consoleReader.readLine();
// 将请求信息发送给服务器
writer.write(requestMessage+"\n");
writer.flush();
// 服务器返回内容
String responseMessage = reader.readLine();
System.out.println("接收到服务器端返回的数据 "+ responseMessage);
// 是否退出
if ("quit".equals(requestMessage)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
System.out.println("客户端已关闭"+socket.getPort());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package io;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
final int PORT = 8888;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
System.out.println("server start, port: " + PORT);
while (true) {
// 建立连接
Socket socket = serverSocket.accept(); // 阻塞式
System.out.println("客户端"+socket.getPort()+" 建立连接");
// 创建IO相关
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 接收客户端数据
String message;
while ((message = reader.readLine()) != null) {
System.out.println("客户端"+socket.getPort()+"发送信息是:"+message);
// 返回客户端数据
writer.write("---"+message+"\n");
writer.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
System.out.println("服务器关闭。。。");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
虚拟机:通过软件模拟的具有完整硬件系统功能的并且运行在一个完全隔离环境当中的一个计算机系统
IDEA插件查看字节:jclasslib bytecode viewer
类加载器(classloader)
Class文件:JVM规范
JVM:内存分配、字节码执行引擎、GC
![](./pictures/Java/JVM 运行时.png)
StackOverflowError
.OutOfMemoryError
.public void hello() {
String a = "hello";
LP lp = new LP("pk");
lp.eat();
}
class LP{
String name;
public LP(String name){}
}
![](./pictures/Java/Stack Heap.png)
堆管存储
栈管执行
# 解释执行
java -Xint -version
# 编译执行
java -Xcomp -version
JVM debug 调优
-XX:-PrintGCDetails # + 开启
jinfo -flag PrintGCDetails pid
jinfo -flag MetaspaceSize pid
javac a.java
# 查看字节码文件
javap -v -c a.class
Ljava/lang/String; = String
[Ljava/lang/String; = String[]
栈:
堆:Heap
public static void main(String[] args) {}
public void hello() {
String name = "pk";
world(name);
Student stu = new Student(name);
stu.study();
}
public void world(String name) {
String world = name + "world...";
}
class Student{
String name;
public Student(String name) {
this.name = name;
}
public void study() {}
}
1 哪些对象要被回收
2 什么时候
3 采用什么方式
1 判断一个对象是否可以回收
GC算法
minor gc young
major gc old
full gc
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。