JAVA的数据类型分为俩大类
-
基本类型(数值类型,boolean类型)
-
引用类型(类,接口,数组)
8大类型:
-
byte占1字节 -128~127
-
short占2字节-32768~32767
-
int占4字节-2147483648~2147483647
-
long占8字节-9223372036854775808~9223372036854775807
-
float占4字节
-
double占8字节
-
char占2字节
-
boolean占1位,只有true和false
1bit表示1位
1Byte表示1字节 1Byte=8bit
1024B=1KB
1024KB=1M
1024M=1G
整数拓展
package demo1;
public class Main {
public static void main(String[] args) {
//整数拓展怎么表示进制: 二进制0B 十进制 八进制0 十六进制0x
//十进制
int i = 10;
//八进制
int i1 = 010;
//十六进制
int i2 = 0x10;
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
}
}
浮点数拓展
package demo1;
public class Main {
public static void main(String[] args) {
//float 有线 离散 舍入误差 大约 接近但不等于
//double
//最好完全使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
System.out.println(f == d);
float d1 = 22123124124123123f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
}
}
字符拓展
package demo1;
public class Main {
public static void main(String[] args) {
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);
System.out.println(c2);
System.out.println((int) c2);
//所有的字符本质还是数字
//编码 Unicode 表:(97 = a 65 = A) 2字节 0-65536
//U0000~UFFFF
char c3 = '\u0061';
//输出为a
System.out.println(c3);
//转义字符
// \t 制表符
// \n 换行符
String sa = new String("hello");
String sb = new String("hello");
System.out.println(sa == sb);
String sc = "hello";
String sd = "hello";
System.out.println(sc == sd);
}
}
布尔拓展
package demo1;
public class Main {
public static void main(String[] args) {
//布尔值拓展
boolean flag = true;
//if(flag==true)和if(flag)相同
}
}
类型转换
低-----》高
byte,shout,char->int->long->float->double
运算中,不同类型的数据先转化为同一类型,然后进行运算
package demo1;
public class Main {
public static void main(String[] args) {
int i = 128;
//高到低需要强制转换
byte b = (byte) i;//内存溢出
System.out.println(i);
System.out.println(b);
/*
注意点
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.在把高容量转换低容量的时候,墙砖转换
4.转换的时候可能村子内存溢出,或者精度问题
*/
System.out.println((int)23.7);//23
System.out.println((int) -45.89f);//-45
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char) d);
}
}
变量
变量是什么:就是可以变化的量
Java是一种强类型语言,每个变量都必须声明其类型
Java变量是程序中最基本的存储单元,其要塞包括变量名,变量类型和作用域
运算符
算术运算符:+ - * % ++ –
赋值运算符:=
关系运算符:> < >= <= == != instanceof
逻辑运算符:&& || !
位运算符:& | ^ ~ >> << >>>
条件运算符:?:
拓展赋值运算符:+= -= *= /=
三元运算符
x ? y 😒
如果x==true,结果为y否则为z
包机制
为了更好地组织类,java提供了包机制,用于区别类名的命名空间。
包语句的语法格式为:
package pkg1[.pkg2[.pkg3]];
一般利用公司域名倒置作为包名
为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用“import”语句可完成此功能
JavaDoc
javadoc命令是用来生成自己API文档的
参数信息:
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
package demo1;
/**
* @author pmy0
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
* @author pmy0
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
命令行生成文档:
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
异常
异常指程序运行中出现的不期而至的各种状况,如:文件找不到、网络连接失败、非法传参等。
异常发生在程序运行期间,它影响了正常的程序执行流程
检查性异常:最具代表的检查性异常时用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽视。
运行时异常:运行时异常时可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽视。
错误ERROR:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的。
Java把异常当错对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类。
在Java API中已经定义了许多异常类,这些异常类分为俩大类,错误Error和异常Exception。异常分为运行时异常和非运行时异常
异常处理机制
- 抛出异常
- 捕获异常
异常处理五个关键词:
- try
- catch
- finally
- throw
- throws
package demo1;
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 0;
new Main().test(1,0);
/* //Ctrl+Alt+t(IDEA快捷键)
try{//try监控区域
if(b==0){
throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
}
System.out.println(a / b);
}catch (Error e){//catch(想要捕获的异常类型) 捕获异常
System.out.println("Error");
e.printStackTrace();//打印错误的栈信息
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable e){
System.out.println("Throwable");
}finally {//处理善后工作
System.out.println("finally");
}*/
}
//假设在方法中,处理不了这个异常,就在方法上抛出
public void test(int a,int b) throws ArithmeticException{
if(b==0){
throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
}
System.out.println(a / b);
}
}
自定义异常
例如:
package demo2;
public class MyException extends RuntimeException{
public MyException(){
super();
}
public MyException(String message){
super(message);
}
}
package demo2;
import java.util.Scanner;
public class Test {
//向上抛出
static void divide(int a, int b) throws MyException{
try {
if(b==0){
throw new MyException("被除数不能是0");
}
System.out.println("答案是:"+(a / b));
}catch (MyException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws MyException{
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();
int b = cin.nextInt();
System.out.println("你输入的除数是"+a+",被除数是"+b);
divide(a,b);
}
}
IO
创建文件或者文件夹
package io;
import java.io.File;
import java.io.IOException;
public class main {
public static void main(String[] args) throws IOException {
try {
//创建文件
File file1 =new File("C:\\Users\\pmy0\\Desktop\\xhq.txt");
//文件存在则创建失败否则成功
boolean flag =file1.createNewFile();
System.out.println(flag?"文件创建成功":"文件创建失败");
//"================================================================="
//创建一个文件夹
File file2 =new File("C:\\Users\\pmy0\\Desktop\\xhq");
//文件存在则创建失败否则成功
boolean flag2 = file2.mkdir();
System.out.println(flag2?"文件夹创建成功":"文件夹创建失败");
//"================================================================="
//创建一个多级文件夹
File file3 = new File("C:\\Users\\pmy0\\Desktop\\jt\\hrz");
//文件存在则创建失败否则成功
boolean flag3 = file3.mkdirs();
System.out.println(flag3?"多级文件夹创建成功":"多级文件夹创建失败");
} catch (IOException e) {
e.printStackTrace();
}
}
}
查询文件
package io;
import java.io.File;
import java.io.IOException;
public class main {
public static void main(String[] args) throws IOException {
try {
File file1 =new File("C:\\Users\\pmy0\\Desktop\\xhq.txt");
file1.createNewFile();
//获取文件大小
Long size = file1.length();
System.out.println("size="+size);
//获取文件名
String fileName = file1.getName();
System.out.println("fileName" + fileName);
//获取文件的路径
String path = file1.getPath();
System.out.println("path" + path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
递归遍历获取文件夹下所有文件路径
package io;
import java.io.File;
import java.io.IOException;
public class main {
public static void showFile(String pathName){
File f1 =new File(pathName);
//判断文件是否是文件夹
boolean flag = f1.isDirectory();
//选择某个文件夹下所以文件
if(flag){
File[] files = f1.listFiles();
//判断文件夹是否为空
if(files!=null) {
for (File tempFile : files){
boolean flag2 = tempFile.isDirectory();
if(flag2){
showFile(tempFile.getPath());
}else{
//获取此文件的路径
String filePath = tempFile.getPath();
System.out.println("普通文件----" + filePath);
}
}
}else {
return;
}
}else{
//获取此文件的路径
String filePath = f1.getPath();
System.out.println("普通文件----" + filePath);
}
}
public static void main(String[] args) throws IOException {
main.showFile("D:\\");
}
}
字节输入流
- 有水厂(数据)
- 铺设管道(FileInputStream,BufferdInputStream)
- 开水龙头,关水龙头
InputStream(字节输入流祖宗类)
- FileInputStream(慢,一个个字节的读中文在UTF-8一个3字节,GB2312是2字节)
- BufferdInputStream(快)
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* @author pmy0
*/
public class io {
public static void main(String[] args) {
try {
/*1.在文件和程序之间铺设管道
File f1 = new File("C:\\Users\\pmy0\\Desktop\\xhq1.txt");
存在并且数据大于0
if(f1.exists()&&f1.length()>0){
FileInputStream f1s =new FileInputStream(f1);
}*/
FileInputStream fis =new FileInputStream("C:\\Users\\pmy0\\Desktop\\xhq.txt");
//2.开水龙头
/*int ch = fis.read();
int ch1 = fis.read();
int ch2 = fis.read();
System.out.println("ch=" + (char)ch);
System.out.println("ch1=" + (char)ch1);
System.out.println("ch2=" + (char)ch2);*/
int ch = 0;
while ((ch=fis.read())!=-1){
System.out.print((char) ch);
}
//3.关水龙头(关闭流)
fis.close();
} catch (Exception e) {
System.out.println("文件找不到");
}
}
}
字节输出流
- 有水厂(数据)
- 铺设管道(FileOutputStream,BufferdOutputStream)
- 输出数据,关流
OutputStream(字节输出流祖宗类)
- FileOutputStream(慢)
- BufferdOutputStream(快)
package io;
import java.io.FileOutputStream;
/**
* @author pmy0
*/
public class io {
public static void main(String[] args) {
try {
//1,创建水厂
String date = "hello";
//2.铺设管道
FileOutputStream fos =new FileOutputStream("D:\\hello.txt");
//如果是下面这个,文件中有东西他就会在后面追加
//FileOutputStream fos =new FileOutputStream("D:\\hello.txt",true);
//3.开水龙头
byte[] tempByte = date.getBytes();
//该文件存在时进行覆盖,否则新建
fos.write(tempByte);
//4.关水龙头
fos.close();
} catch (Exception e) {
System.out.println("文件找不到");
}
}
}
IO流实现复制
package io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class code {
public static void main(String[] args) {
try {
long startTime =System.currentTimeMillis();
//1.在文件和程序之间铺设管道
FileInputStream fis =new FileInputStream("D:\\hp.txt");
//创建一个程序通往盘符的通道
FileOutputStream fos =new FileOutputStream("D:\\hp1.txt");
//2.开水龙头
int ch = 0;
while ((ch=fis.read())!=-1){
fos.write(ch);
}
//3.关水龙头
fis.close();
fos.close();
long endTime =System.currentTimeMillis();
System.out.println("共消耗" + (endTime - startTime) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package io;
import java.io.*;
public class code1 {
public static void main(String[] args) {
try {
long startTime =System.currentTimeMillis();
//1.水厂(D:\hp.txt)
//2.创建一个程序通往盘符的通道
InputStream fis =new FileInputStream("D:\\hp.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//在程序与D:\\hp.txt建立管道
OutputStream fos = new FileOutputStream("D:\\hp1.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.关水龙头
//创建一辆车 一次1024
byte[] car = new byte[1024];
int len=0;
while ((len=bis.read(car))!=-1){
//bos.write(数组名,启示下标,多少);
bos.write(car,0,len);
}
//4,关流
bis.close();
bos.close();
long endTime =System.currentTimeMillis();
System.out.println("共消耗" + (endTime - startTime) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符流(读取纯文本文件比较方便)
Reader(字符输入流祖宗类)
- FileReader(慢,一个个字节的读中文在UTF-8一个3字节,GB2312是2字节)
- BufferdReader(快)
Writer(字符输出流祖宗类)
- FileWriter(慢)
- BufferdWriter(快)
package io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReaderDemo1 {
public static void main(String[] args) {
/*try {
//1.水厂 D:\\hp.txt
//2.铺水管
FileReader fr = new FileReader("D:\\hp.txt");
//3.开水龙头
char[] car =new char[1024];
int len=0;
while ((len=fr.read(car))!=-1){
String str = new String(car,0,len);
System.out.println(str);
}
//4.关水龙头
fr.close();
} catch (Exception e) {
e.printStackTrace();
}*/
try {
//1.水厂
String str = "java真的很简单?";
//2.铺管道
FileWriter fw = new FileWriter("D:\\hp1.txt");
//3.开水龙头
fw.write(str);
//4.关水龙头
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}