You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 基础语法
### 注释
##### 单行
//······//
##### 多行
/* ······· */
##### 文档注释JAVA特有
文档注释可以被 javadoc 解析,生成一套以网页文件形式体现该程序的说明文档
/** ········ */
API文档
### 标准输出输入
```java
calss PersonalInfo{
public static void main(String[] args){
System.out.println("~~~");
}
}
```
### 变量
##### 数据类型
数据类型 标识符 = 数值 ;
###### 整型
byte 1字节
short 2字节
int 4字节
long 8字节
###### 浮点型
float 单精度 4字节
double 双精度 8字节
###### 字符类型和布尔类型
char 2字节
boolean a = true ;
boolean b = false ;
4字节
##### 变量的自动类型提升和强制转换
容量小与容量大的变量做运算时,结果自动转换为容量大的数据类型(容量不☞内存,☞范围)
反之报错 (反之容易泄露)
### String类
字符串
String str = "";
运算:双引号连接,单引号加法
### 运算符
| 算数运算符 | 赋值运算符 | 比较运算符 |
| :-------------: | :----------------------------------------: | :-------------: |
| +,-,*,/,%,++,-- | =,+=,-=,*=,/=,%=,>>=,<<=,>>>=,&=,\|=,^= 等 | <,<=,>,>=,==,!= |
| 逻辑运算符 | 位运算符 | 条件运算符 | Lambda运算符 |
| :--------------: | :----------------: | :--------------: | :----------: |
| &,\|,^,!,&&,\|\| | &,\|,^,~,<<,>>,>>> | 条件?结果1:结果2 | -> |
### 标准输入输出流
```java
//导包
import java.util.Scanner;
class Scannertest{
public static void main(String[] args){
System.out.println("输出");
//定义
Scanner scan = new Scanner(System.in);
//使用
String name = scan.next();
}
}
```
##### 生成随机数
在指定范围内生成随机数
Math类中的random方法返回一个[0.0,1.0)之间的数
### 数组
##### 一维数组
###### 初始化
```java
//声明
double[] prices;
//赋值
prices = new double[]{20.32,4.1,5.1}
```
```java
String [] foods = new String[4];
```
##### 二维数组
###### 初始化
```
String [] foods = new String[4][4];
```
##### 数组常用方法
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//比较
System.out.println(Arrays.equals(arr1,arr2));
//输出
System.out.println(Arrays.toString(arr1));//包含括号
//填充
Arrays.fill(arr1, 0);
System.out.println(Arrays.toString(arr1));
//排序
int[] arr3 = {1, 123, 3, 123, 5, 2323, 7, 8, 9, 0};
Arrays.sort(arr3);
System.out.println(Arrays.toString(arr3));
//二分查找,前提必须有序
int index = Arrays.binarySearch(arr3, 2323);
//返回下标,找不到返回负数
}
}
```