关键字:
被Java语句赋予特殊含义的单词,所有关键字都是小写
标识符:
程序中自定义的一些名称,由字母、数字、_ 以及 $ 符号组成,数字不能开头,区分大小写(命名规范)
注释:
// 单行注释
/* 多行注释 */
/** 文档注释 */
常量:
整数常量、小数常量、布尔型常量、字符常量、字符串常量、null
变量:
数据类型 变量名 [= 初始化值];
数据类型:
基本数据类型、引用数据类型
- 数值型
- 整数类型:byte、short、int、long
- 浮点类型:float、double
- 字符型:char
- 布尔型:boolean(true/false)
数据类型转换:
- 自动类型转换:低精度 -> 高精度(有可能丢失精度)
- 强制类型转换:(数据类型)表达式;
package g.javase;public class VarDemo {
public static void main(String[] args) {
// 数据类型 变量名 [= 初始化值];
int x = 4;
System.out.println("x = " + x);byte b = 3;
b = (byte) (b + 1); // 强制转换
System.out.println("b = " + b);char ch = 97;
System.out.println("ch = " + ch);char ch2 = ‘a‘;
System.out.println("ch2 = " + (ch2 + 1));}
}
运算符:
- 算术运算符:+、-、*、/、%、++、--
- 赋值运算符:= += -=
- 比较运算符:<、<=、>、>=、==、!=
- 逻辑运算符:&、|、^、!、&&、||
- 位运算符:<<、>>、>>>、&、|、^、~
- 三元运算符:()?():()
package g.javase;public class OperateDemo {
public static void main(String[] args) {int x = 1234;
System.out.println(x / 1000);System.out.println("5 + 5 = " + 5 + 5); // 字符串连接符
System.out.println(3 % 5);
System.out.println(-3 % 5);
System.out.println(3 % -5);System.out.println(2 * 8);
System.out.println(2 << 3);int a = 5;
int b = 6;
System.out.println("a = " + a + "\tb = " + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a = " + a + "\tb = " + b);}
}
流程控制:
- 选择:if、switch
- 循环:while、do while、for、foreach
- 其它:break、continue
函数:
修饰符 返回值类型 函数名(参数列表) {
执行语句;
[return 返回值;]
}
函数的重载:在同一个类中,允许存在一个以上的同名函数,只要它们的参数个数或参数类型不同即可
数组:
同一类型数据的集合
元素类型[] 数组名 = new 元素类型[数组长度];
package g.javase;import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {int[] a1 = new int[3];
a1[0] = 1;
a1[1] = 2;
a1[2] = 3;int[] a2 = new int[] { 1, 2, 3 };
int[] a3 = { 1, 2, 3 };
System.out.println("a1: " + Arrays.toString(a1));
System.out.println("a2: " + Arrays.toString(a2));
System.out.println("a3: " + Arrays.toString(a3));}
}
补充:
package g.javase;public class BinarySearch {
public static void main(String[] args) {}
public static int binarysearch(int[] a, int k) {
int max = a.length - 1;
int min = 0;
int mid;while (min <= max) {
mid = (min + max) / 2;
if (k > a[mid]) {
min = mid + 1;
} else if (k < a[mid]) {
max = mid - 1;
} else {
return mid;
}
}return -1;
}
}
BinarySearch
package g.javase;public class Hex {
public static void main(String[] args) {
int num = 28;
System.out.println(trans(num, 1, 1)); // to Binary
System.out.println(trans(num, 7, 3)); // to Oct
System.out.println(trans(num, 15, 4)); // to HexSystem.out.println(Integer.toBinaryString(num));
System.out.println(Integer.toOctalString(num));
System.out.println(Integer.toHexString(num));
}public static String trans(int num, int base, int offset) {
char[] chs = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘,
‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
char[] arr = new char[32];
int index = arr.length;while (num != 0) {
int temp = num & base;
arr[--index] = chs[temp];
num = num >>> offset;
}return toString(arr, index);
}public static String toString(char[] arr, int index) {
String temp = "";
for (int x = index; x < arr.length; x++) {
temp = temp + arr[x];
}return temp;
}
}
Ary
package g.javase;import java.util.Arrays;
public class SortDemo {
public static void main(String[] args) {
int[] a = { 12, 9, 23, 77, 6, 34};
int[] b = { 12, 9, 23, 77, 6, 34};System.out.println(Arrays.toString(a));
select(a);
System.out.println(Arrays.toString(a));System.out.println(Arrays.toString(b));
select(b);
System.out.println(Arrays.toString(b));}
// 选择排序
public static void select(int[] a) {for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}}
// 冒泡排序
public static void bubble(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
}
select/bubble_Sort
Java SE(1):基础语法