Java Basics - (first day)
A, Java Introduction
Java is an object-oriented software development programming language (JAVA SE, JAVA EE, JAVA ME)
features: simple, robust, secure, cross-platform, highly optimized virtual machine (Disadvantages: cumbersome syntax, you can not operate the hardware, poor GUI effects )
robustness: support for fault-tolerant, an exception is thrown, without affecting the normal operation of the program's
reliability: low chance of error
the JDK javac compiler (java source files -> bytecode class file) java application development tool for the JRE +
the JRE the JVM + Runtime Library java program running environment
Second, the program structure
Program Structure
[public] class 类名 {
[public] 返回值类型 [void] 方法名 (参数){
执行语句;
}
}
Comment character //单行注释 /* */多行注释 /** */
described multi-line comments for classes and methods
Third, variable
Variables can hold values of a basic type or point to an object, you must first define the principles for the use, re-assignment.
变量类型 变量名 = 初始值;
Fourth, data types
Data types include the basic data types and reference types
整数类型 long int short byte (long a = 1222L)
浮点类型 double float
布尔类型 boolean (true false)
字符类型 char (char s = 'a')
字符串类型 string (String str = "abc")
The minimum computer memory storage unit is a byte, a byte 8-bit binary number from 0 to 255
binary hexadecimal 0x 0b
V. Constant
final double PI = 3.14; //final修饰符,不可再次赋值
Sixth, integer arithmetic
+、-、*、/(除法运算结果为整数)
++、--、+=、-=
% 取余数
计算结果超过范围不会报错
>>、<<、>>>(不带符号位)移位
^异或(相不同结果为1)、~非运算
Casts lose precision (int)
Seven, floating-point operations
Many floating point can not be accurately represented, calculation errors, may be automatically promoted to float integer
24/5 4 results not automatically elevated to float, to 24.0 / 5 or 24 / 5.0
casts integer discarded directly decimal places, and the result of the integer 24.3 24.6 casts 24 are, respectively, 24.3 + 0.5 skill 24.6 + 0.5 and
the calculation result exceeds the range, the result is the maximum
Eight, Boolean operations
关系运算符 > >= < <= == !=
与运算 &&
或运算 ||
非运算 !
Ternary operator a b:? Ca is true, the result is b, the opposite was found to be c. b and c must be the same type of
Nine, characters and strings
字符类型 char 是基本数据类型 char c = 'a' 保存一个字符,可以是一个英文字符‘a’或一个中文字符‘中’
java使用的是unicode编码表示字符 char c = '\u0041' 表示英文字符A
字符串类型 String 是引用类型 String s1 = "abc" 字符串不可变,指向非持有
字符串的连接通过 "ab"+ "cd" 结果为"abcd"
转义字符 \n \r \t \\ \"
空值 null 空字符串 “”是一个空字符串对象
X. array type
Array type is a reference type, the plurality of elements of the same type, the array representation can be used
数组的定义
int [] ns = new int[5] 其中new int[5] 初始化,大小不可改变,索引从0开始
int [] ns = new int[]{1,2,3}
Eleven, the input and output
输出
System.out.println("abc") ;
System.out.print("abc");
输入
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine(); 读取字符串
int age = scanner.nextInt();读取整数
格式化输出
System.out.printf("%5.2f \n",str1); %是占位符
%d %f %s %%(表示%本身)
System.out.printf("%2$s %1$s %3$s \n","a","b","c"); 输出结果为:b a c
String str2 = String.format("%4.2f",3.1415926);
Twelve, if judgment
Program Structure
if(条件1){
执行语句1;
}
else if(条件2){
执行语句2;
}
else {
执行语句3;
}
浮点数用==判断是不靠谱
if(a==0.1) 使用if(Math.abs(a-0.1)<0.0001)
equals判断内容是否相等 a.equals(b)
如果a为null,会报错
引用类型判断是否指向同一个对象 ==
Thirteen, switch statements
Program Structure
switch(option){ option表达式
case 1: 执行语句1;break; case具有穿透性
case 2: 执行语句2;break;
default: 执行语句3;break;
}
Fourteen, while circulation
Program Structure
while(条件){
执行语句;
}
Fifteen, dowhile cycle
Program Structure
do{
执行语句;
}while(条件);
Sixteen, for circulation
Program Structure
for(初始值;条件判断;计算器){
执行语句;
}
for(;;) 死循环
for each cycle
for(int i: ns){ ns为数组
执行语句;
}
Seventeen, break and continue
Cycle, break out of this loop, continue this cycle is skipped
XVIII through the array
int ns = {1,2,3,4};
System.out.println(ns); ns为JVM中的引用地址
for(int i =0;i <ns.length();i++){
System.out.println(ns[i]);
}
for(int n:ns){
System.out.println(n);
}
System.out.println(Array.toString(ns));快速打印数组中所有的元素
Ninth, sort the array
Array sorting modify the array itself
冒泡排序法 使用两层for循环
int[] ns = {3,1,4,5,2};
for (int i = 0; i < ns.length; i++) {
for (int j = i+1; j < ns.length; j++) {
if (ns[i]>ns[j]) {
int temp = ns[j]; 两个变量交换值通过中间变量
ns[j] = ns[i];
ns[i] = temp;
}
}
}
System.out.println(Arrays.toString(ns));
Arrays.sort(ns) 大数组采用快速排序,小数组采用冒泡排序
System.out.println(Arrays.toString(ns));
XX, multidimensional arrays
Two-dimensional array
int[][] ns ={
{1,2,3},
{4,5,6},
{7,8,9}
}
System.out.println(Arrays.deepToString(ns)); 快速打印多维数组
XXI command line parameters
Command line parameters String [] args is a String [] array