Java编程入门
Java语言简介
- 是一个行业内通用的技术实现标准
- 是一门面向对象的编程语言
- 提供有方便的内存回收处理机制
- 避免了复杂的指针问题
- 是为数不多支持多线程编程的开发语言
- 提供有高效的网络处理能力
- 具有良好的可移植性
搭建Java开发环境
- JDK下载地址:https://www.oracle.com/java/technologies/javase-downloads.html
- 配置环境变量:此电脑→属性→高级系统设置→环境变量→系统变量
- 新建:
JAVA_HOME
→C:\Lin1118\java
(JDK的安装目录) - 编辑Path:在最前面增加
%JAVA_HOME%\bin;
(Win10的弹窗编辑模式会自动添加分号) -
初识Java开发
EditPlus编辑文件
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello 世界!");
}
}
编译代码:
javac HelloWorld.java
运行程序:java HelloWorld
Java基本概念
注释
/**
文档注释
@author Lin1118
*/
public class HelloWorld {
/*
多行注释
多行注释
*/
public static void main(String[] args) {
// 单行注释
System.out.println("Hello 世界!");
}
}
标识符
标识符可以由字母、数字、下划线、美元符组成,数字不能开头:
age
、lastName
、str2num
、temp
关键字
在程序中有特殊意义的字符就是关键字:
public
、class
、void
、byte
数据类型
整数型
public class Demo {
public static void main(String[] args) {
byte temp1 = 127;
short temp2 = 1024;
int temp3 = Integer.MAX_VALUE;
long temp4 = 4294967296L;
System.out.println(temp1);
System.out.println(temp2);
System.out.println(temp3);
System.out.println(temp4);
}
}
浮点型
public class Demo {
public static void main(String[] args) {
float temp1 = (float) 10.2;
float temp2 = 10.1F;
double temp3 = 3.14;
System.out.println(temp1 * temp2);
System.out.println(temp3);
}
}
字符型
public class Demo {
public static void main(String[] args) {
char temp1 = 'a';
int temp2 = temp1;
char temp3 = (char) (temp2 - 32);
System.out.println(temp1);
System.out.println(temp2);
System.out.println(temp3);
}
}
布尔型
public class Demo {
public static void main(String[] args) {
boolean flag1 = true;
boolean flag2 = false;
System.out.println(flag1);
System.out.println(flag2);
}
}
字符串
public class Demo {
public static void main(String[] args) {
String str = "Hello";
str = str + " World";
str += "!!!";
System.out.println(str);
int x = 8;
int y = 10;
System.out.println("x+y=" + x + y);
System.out.println("x+y=" + (x + y));
System.out.println("AB\"CD\"EFG\tAAAAAA\nBBBBB\u0065" + "OK");
System.out.println("Hello World".toUpperCase());
}
}
Java运算符
数学运算
| 加 | + | 取模 | % | | —- | —- | —- | —- | | 减 | - | 自增 | ++ | | 乘 | | 自减 | — | | 初 | / | 简便用法 | +=、-=、=、/=、%= |
public class Demo {
public static void main(String[] args) {
int x = 1;
int y = 1;
int z = x + y;
System.out.println(z);
System.out.println(Integer.MAX_VALUE + z);
int m = y++;
System.out.println(m);
System.out.println(y);
int n = --x;
System.out.println(n);
System.out.println(x);
System.out.println(10/3);
System.out.println(10%3);
}
}
关系运算
public class Demo {
public static void main(String[] args) {
int x = 1;
int y = 1;
boolean flag = x == y;
System.out.println(flag);
System.out.println(5 > 7);
System.out.println(5 < 7);
System.out.println(5 >= 7);
System.out.println(5 <= 7);
System.out.println(5 == 7);
char c = 'A';
System.out.println(c == 65);
}
}
逻辑运算
public class Demo {
public static void main(String[] args) {
boolean falg1 = true;
boolean falg2 = true;
System.out.println(falg1 && falg2);
System.out.println(falg1 || falg2);
System.out.println(!falg1);
}
}
位运算
public class Demo {
public static void main(String[] args) {
System.out.println(5 & 8);
System.out.println(5 | 8);
System.out.println(5 ^ 8);
System.out.println(2<<3);
System.out.println(2>>3);
System.out.println(2>>>3);
}
}
三目运算
public class Demo {
public static void main(String[] args) {
int x = 5;
int y = 8;
int max = x>y ? x : y;
System.out.println(max);
}
}
逻辑控制
if else
public class Demo {
public static void main(String[] args) {
int score = 99;
if(score >= 90) System.out.println("优秀");
if(score >= 90) {
System.out.println("优秀1");
System.out.println("优秀2");
System.out.println("优秀3");
}
if(score >= 90) {
System.out.println("优秀");
}else {
System.out.println("不优秀");
}
if(score >= 90) {
System.out.println("优秀");
}else if(score >= 60) {
System.out.println("还行");
}else {
System.out.println("很差");
}
}
}
switch
public class Demo {
public static void main(String[] args) {
String str = "B";
switch (str) {
case "A": {
System.out.println("AAA");
break;
}
case "B": {
System.out.println("BBB");
break;
}
case "C": {
System.out.println("CCC");
break;
}
default: {
System.out.println("DDD");
}
}
}
}
while
public class Demo {
public static void main(String[] args) {
int sum = 0;
int num = 1;
while(num <= 100) {
sum += num;
num++;
}
System.out.println(sum);
int count = 0;
do {
System.out.println(count);
count--;
}while(count >= 1);
}
}
for
public class Demo {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<=100; i++) {
sum += i;
}
System.out.println(sum);
}
}
循环控制
public class Demo {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
if(i == 3) break;
System.out.println(i);
}
System.out.println("----");
for(int i=1; i<=5; i++) {
if(i == 3) continue;
System.out.println(i);
}
}
}
循环嵌套
public class Demo {
public static void main(String[] args) {
for(int i=1; i<=9; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j + "*" + i + "=" + (j*i) + "\t");
}
System.out.println();
}
}
}
方法
定义方法
public class Demo {
public static void main(String[] args) {
fun();
fun();
System.out.println(max(5, 8));
System.out.println(max(9, 4));
}
public static void fun() {
System.out.println("AAA");
System.out.println("BBB");
System.out.println("CCC");
}
public static int max(int x, int y) {
return x>y ? x : y;
}
}
方法重载
public class Demo {
public static void main(String[] args) {
System.out.println(sum(5, 8));
System.out.println(sum(5, 8, 9));
System.out.println(sum(9.2, 4.5));
sum(9.2, 4.5, 5.6);
}
public static int sum(int x, int y) {
return x + y;
}
public static int sum(int x, int y, int z) {
return x + y + z;
}
public static double sum(double x, double y) {
return x + y;
}
public static void sum(double x, double y, double z) {
System.out.println(x + y + z);
}
}
方法递归
public class Demo {
public static void main(String[] args) {
System.out.println(sum(10));
System.out.println(sum(100));
System.out.println(sum(200));
}
public static int sum(int num) {
if(num == 1) return 1;
return num + sum(num-1);
}
}