只记录Java最入门的内容,模块并不完整,更深入的知识在Java基础、Java提高、JavaWeb模块
1、注释同C、关键字字母全部小写、常量略、变量命名规范同C,定义long时在整数后加L,定义float时在最后加F
2、标识符命名规范同C,另外小驼峰用来定义变量,大驼峰用来定义类
3、数据类型及类型转换如图,强转同C
4、算术运算符同C,整数除法注意*1.0、字符的“+”操作使用时,若包含多个数据类型,整个表达式类型自动进行提升,优先级如下,字符串操作相当于字符串拼接(特例是1+1+'字符串'为先做加法)。
5、赋值运算符中注意+=运算底层隐含了强制类型转换,自增自减同C
6、关系运算符同C,结果为true或false,逻辑运算符同C用来连接关系表达式、&&短路与 ||短路或
7、三元运算符同C condition?true:false
8、使用引用类型:
import 包路径名;
数据类型 变量名称 = new 数据类型();
变量名称.方法名
以Scanner
为例
import java.util.Scanner
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String str = sc.next();
9、流程if、while、for、break、continue同C,略
10、数组
数据类型[] 数组名 = new 数据类型[];
或者 数据类型 数组名[] = new 数据类型[];
int[] arr = new int[3]
,创建含有3个元素的int型数组 int[] arr = new int[] {10,20,30}
int[] arr = {10,20,30}
Arrays.toString(数组名)
打印数组元素数组类型[] 新数组名 = Arrays.copyOf(旧数组名,新数组长度)
11、Java内存分配
12、方法(相当于C的函数)
修饰符 返回值类型 方法名称(参数类型 参数名称,参数类型 参数名称){方法体;return 返回值;}
方法名称(参数值)
System.out.println(方法名称(参数值))
数据类型 变量名 = 方法名称(参数值)
,数据类型必须与方法返回值一致13、==:基本类型是比较数据内容,引用类型是比较地址值
private
关键字与getter()
和setter()
方法this
关键字 通过谁调用的方法,谁就是thisnew
创建对象时,就是在调用构造方法public 构造方法名(参数类型 参数名称){方法体;return;}
private
私有化getter
和setter
方法。对于boolean
类型,是get
和is
substring
equals
方法str.replace(oldStr,newStr);
split
String[] arr = str.split("分割点")
add()
:返回值为booleanget()
:索引从0开始size()
:长度byte
:Byte
short
:Short
int
:Integer
long
:Long
float
:Float
double
:Double
char
:Character
boolean
:Boolean
FileWriter
写文件:字符输入流\r\n
\n
\n
public coid write(int ch)
:ASCII或Unicode码表public coid write(String str)
:写一个完整的字符串public coid write(String str, int offset,int count)
:写入截取后的字符串public coid write(char[] array)
:写入完整的字符数组public coid write(char[] array,int offset,int count)
:写入截取后的字符数组FileReader
读文件public int read()
:一次只读取一个字符,返回值为ASCII或Unicode码,直到-1为止public int read(char[] buf)
:一次读取整个字符数组的数据,返回值代表数组当中读取到的有效个数。public void close()
://读取字符串的标准代码
FileReader fr = new FileReader("File06.txt");
char[] buf = new char[2];
int len;
while ((len = fr.read(buf)) != -1) {
String str = new String(buf, 0, len);
System.out.print(str);
}
fr.close();
BufferedWriter()
:肚子里有一个长度为8192的char[]
字符数组,当做缓冲区使用。缓冲数组满了之后,写到硬盘的文件中。若没有写满,等待下一次写入。关闭流时,将剩余有效部分写到硬盘文件中。 public void newLine()
;基本使用
FileWriter fw = new FileWriter("File06.txt");
BufferedWriter buffer = new BufferedWriter(fw);
buffer.write("Hello!");
buffer.close();
BufferedReader()
:跟BufferedWriter差不多public String readLine()
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("File06.txt");
BufferedReader buffer = new BufferedReader(fr);
// 读取单个字符
int read;
while ((read = buffer.read()) != -1) {
System.out.print((char) read);
}
// 读取字符数组
char[] buf = new char[2];
int len;
while ((len = buffer.read(buf)) != -1) {
String str = new String(buf,0,len);
System.out.print(str);
}
buffer.close();
}
/**readLine的用法*/
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("File06.txt");
BufferedReader buffer = new BufferedReader(fr);
String line = null;
while ((line = buffer.readLine())!=null) {
System.out.println(line);
}
buffer.close();
}
public static void main(String[] args) throws IOException {
//创建集合
ArrayList<String> list = new ArrayList<>();
list.add("小强1号");
list.add("小强2号");
list.add("小强3号");
list.add("小强4号");
//写文件
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("test01.txt"));
//遍历集合
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
bufferedWriter.write(str);
if (i==list.size()-1) {
break;
}
bufferedWriter.newLine();
}
bufferedWriter.close();
}
public static void main(String[] args) throws IOException {
//新建字符串
ArrayList<String> list = new ArrayList<String>();
//读取
BufferedReader bufferedReader = new BufferedReader(new FileReader("test01.txt"));
String str = null;
while ((str = bufferedReader.readLine())!=null) {
list.add(str);
}
bufferedReader.close();
System.out.println(list.toString());
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("Test03.txt"));
ArrayList<String> list = new ArrayList<String>();
String str = null;
while ((str = bufferedReader.readLine())!=null) {
list.add(str);
}
System.out.println(list.toString());
bufferedReader.close();
//逆向读取
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Test04.txt"));
for (int i = list.size()-1; i >= 0; i--) {
System.out.println(list.get(i));
bufferedWriter.write(list.get(i));
if (i==0) {
break;
}
bufferedWriter.newLine();
}
bufferedWriter.close();
}
同上
public interface 接口名称{}
接口当中可以包含的组成部分有抽象方法、常量、默认方法(Java 8)、静态方法(Java 8) 、私有方法(Java 9)
public abstract 返回值类型 方法名称 (参数类型 参数名称)
public class 实现类名称 implements 接口名称 {}
接口名称 引用名 = new 实现类名称()
;引用名.抽象方法名(参数)
;有且仅有一个抽象方法的接口,叫做函数式接口。使用Lambda表达式的前提是必须有函数式接口。
检测当前接口是否为函数式接口:@FunctionalInterface
接口:
@FunctionalInterface
public interface Calculator {
public abstract int sum(int a,int b);
}
使用:
public static void main(String[] args) {
method((int a,int b) -> { return a+b;} );
}
public static void method(Calculator calculator) {
int result = calculator.sum(10, 20);
System.out.println(result);
}
Lamda表达式的参数类型可以省略,如果参数值有一个那么小括号可以省略,如果语句只有一个那么大括号和return也可以省略。
public static 方法名(参数类型 参数名称)
public static 方法名(参数类型 参数名称)
对象名.成员方法名(参数值)
可以用来简化Lambda表达式,例如()->{}
写成类名称::方法名
类名称::方法名
接口:
@FunctionalInterface
public interface Calculator {
int getAbs(int num);
}
使用:
public static void main(String[] args) {
//Lambda表达式
method(num -> num >= 0 ? num : -num);
//引用
method(Math::abs);
}
public static void method(Calculator cal) {
int abs = cal.getAbs(-25);
System.out.println("结果是" + abs);
}
对象名::成员方法名
类定义:
public void makeFood(String food) {
System.out.println(food);
}
接口定义:
public interface Sitter {
void work(String food);
}
使用:
public static void main(String[] args) {
Cook cook = new Cook();
method( cook::makeFood );
}
public static void method(Sitter sitter) {
sitter.work("土豆");
}