快速入门java
[toc]
写在前面
最近花了两周系统学习了java,总体上感觉下来Java是以对象为核心的编程语言,我们知道C++、Python等语言,虽然他们都是面向对象的,但是”面向对象“一词在Java中体现的淋漓尽致,作为一个Go程序员来说,在语法层面Java确实和Go有较大区别,这里快速介绍java基础语法、面向对象、多线程、IO等。
java开发环境搭建
三个名词
在配置开发环境之前你应该了解三样东西:Jdk、Jvm、Jre
JDK(Java Development Kit)是Java开发工具包的缩写,它是用于开发Java应用程序的软件开发工具。 JDK包含了编译器、调试器、Java API类库、Java运行环境和其他一些工具。
JVM(Java Virtual Machine)是一个虚拟机,它是Java应用程序的运行环境。JVM可以在不同的操作系统上运行Java程序,因为Java代码被编译成Java字节码,而不是与特定操作系统相关的本地机器码。JVM将Java字节码转换成特定操作系统的本地机器码,并执行Java应用程序。
JRE(Java Runtime Environment)是Java应用程序运行所必需的环境。
JDK的安装
直接访问:https://www.oracle.com/java/technologies/downloads/选择对应版本即可。
安装完成后,可以测试一下是否安装成功,看到如下类似内容则安装成功,如果没有对应内容,您可能需要配置一下环境变量。
$ java -version
java version "18.0.2.1" 2022-08-18
Java(TM) SE Runtime Environment (build 18.0.2.1+1-1)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2.1+1-1, mixed mode, sharing)
IDEA的安装
我们知道一个好的编码工具会让我们事半功倍,这里直接推荐安装IDEA
到这里我们的环境搭建就完成了,接下来我们正式开始学习吧。
第一个Java程序
public class Main {
public static void main(String[] args) {
System.out.printf("Hello and welcome!");
}
}
直接在命令行运行,在源文件下使用命令:
$ javac Main.java
然后我们可以看到一个Main.class
字节码的文件
然后运行:
$ java Main
Hello and welcome!%
变量
每一门编程语言都有自己的变量,我们来看看Java的变量是如何的。
//变量
int a,b; //声明变量
a = 1;
b = 2;
System.out.println("\n"+ b+a);
//类型转换
double d = 1.12; //双精度浮点数
float f = 1.1f; //浮点默认是double,所以我们在使用单精度是需要指定单精度
if (d > f ) {
System.out.println(d);
}
d = f;
f = (float)d; //类型转换
System.out.println(f);
下来继续看:
mport java.util.Scanner;
public class typeDemo {
public static void main(String[] args) {
//数值类型
//变量
byte b = 100;
short s = 1000;
int i = 10000;
long l = 1000000000;
float f = 1.5f;//浮点默认是double
double d = 1.555; //双精度
char c = 'a'; //字符类型
String str = "hello,java";//字符串类型,在Java中string是一个类
boolean isHave = true; //布尔类型
System.out.println("\n" + s);
//类型转换
if (d > f) {
System.out.println(d);
}
d = f;
f = (float) d;
System.out.println(f);
}
}
常量
来看看Java的常量,常量使用关键字final
//常量
final Double PI = 3.14195;
final int YEAS = 365;
final int MAX_VALUE = 1000;
运算符
基本运算符
//算数运算符
int a1 = 10;
int a2 = 7;
float f1 = 0.75f;
float t1 = a1 + f1; //加
float t2 = a1 - f1; //减
float t3 = a1 * f1; //乘
float t4 = a1 / f1; //除
int a3 = a1 % a2; //取余
自增自减
//自增自减
int num1 = 10;
int num2;
num2 = ++num1; //num1 += 1, num2 = num1
num2 = num1++; //num2 = num1, num1++
num2 = --num1; //num1 -= 1, num2 = num1
num2 = num1--; //num2 = num1, num1--
a1++;
a2--;
关系运算符
//关系运算符
System.out.println(4 > 1); //true
System.out.println(4 < 1); //false
System.out.println(4 >= 4); //true
System.out.println(4 == 1); //false
System.out.println(4 != 1); //true
逻辑运算符
逻辑运算符: &&, ||, !
//逻辑运算符: &&, ||, !
Scanner scan = new Scanner(System.in);
int sour = scan.nextInt();
if (sour >= 90) {
System.out.println(n + "是优秀");
} else if (sour < 90 && sour >= 60) {
System.out.println(sour + "及格");
} else {
System.out.println(sour + "不及格");
}
条件语句
接下来我们来看看Java的条件语句是如何使用的:
if语句
if (d > f) {
System.out.println(d);
}
if-else
int sou = 60;
if (s >= 60) {
System.out.println(sou + "及格");
} else {
System.out.println(sou + "不及格");
}
if-else if -else
Scanner scan = new Scanner(System.in); //从控制台输入
int sour = scan.nextInt(); //将数据转换为int类型
if (sour >= 90) {
System.out.println(n + "是优秀");
} else if (sour < 90 && sour >= 60) {
System.out.println(sour + "及格");
} else {
System.out.println(sour + "不及格");
}
循环语句
while循环
public class CirculateDemo {
public static void main(String[] args) {
//while的使用
int i = 10;
while (i > 0) {
System.out.println(i);
i--;
}
}
}
public class CirculateDemo {
public static void main(String[] args) {
char ch = 'a';
int count = 1;
while(ch <= 'z') {
System.out.print(ch+" ");
if (count%13 == 0) {
System.out.println();
}
count++;
ch++;
}
}
}
public class CirculateDemo {
public static void main(String[] args) {
int sum = 0;
int num = 0;
while (num <= 100) {
sum += num;
num++;
}
System.out.println("sum:"+sum);
}
}
do-while循环
public class CirculateDemo {
public static void main(String[] args) {
//do-while的使用
int t = 0;
int sum0 = 0;
do{
sum0 += t;
t++;
}while (t <= 100);
System.out.println("sum0:"+sum0); //sum0:5050
}
}
猜数游戏
public class CirculateDemo {
public static void main(String[] args) {
int tag = ((int)Math.random()*10+1);
int ints;
do{
Scanner in = new Scanner(System.in);
ints = in.nextInt();
if (tag > ints) {
System.out.println("太小了");
}else if (tag < ints) {
System.out.println("太大了");
}
}while (tag != ints);
System.out.println("恭喜你,猜中了!答案是:"+tag);
}
}
//输出:
//10
//太大了
//1
//恭喜你,猜中了!答案是:1
for循环
完整for循环:
public class CirculateDemo {
public static void main(String[] args) {
int sum2 = 0;
for(int j = 0; j <= 100; j++) {
sum2 += j;
}
System.out.println(sum2); //5050
}
}
for(; k < 10; k++)
:
public class CirculateDemo {
public static void main(String[] args) {
int k = 0;
for(; k < 10; k++) {
System.out.println(k);
}
}
}
for(;; k++)
:
public class CirculateDemo {
public static void main(String[] args) {
k = 0;
for(;; k++) {
if(k == 10) {
break;
}
System.out.println(k);
}
}
}
双层循环:
public class CirculateDemo {
public static void main(String[] args) {
for(int m = 0; m < 4; m++) {
System.out.println();
for(int v = 0; v < 4; v++) {
System.out.print("*");
}
}
}
}
数组
数组是我们在平时或者开发中必不可少的数据结构type[]
例如:int[]、float[]等,下面我们来看看Java的数字是如何使用的
声明数组
int[] array;
声明赋值
int[] arr = {4,100,7,1,12};
这里我们来写一个选择排序算法:
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int[] arr = {4,100,7,1,12};
int[] array;
for(int i = 0; i < arr.length; i++) {
int mod = i;
for(int j = i+1; j < arr.length; j++) {
if(arr[mod] > arr[j]) {
mod = j;
}
}
int tag = arr[i];
arr[i] = arr[mod];
arr[mod] = tag;
}
}
}
我们还可以以下操作:
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
int[] arr1 = new int[5];
for(int i = 0; i < arr1.length; i++) {
Scanner in = new Scanner(System.in);
arr1[i] = in.nextInt();
}
int sum = 0;
for(int i = 0; i < arr1.length; i++) {
sum += arr1[i];
}
System.out.println("sum:"+sum);
比如我们现在来找数组中最大的元素:
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
//求一个数组的最大值
int[] arr2 = {23, 1, 54, 25, 5, 29};
int max = arr2[0];
for(int i = 1; i < arr2.length; i++) {
if(max < arr2[i]) {
max = arr2[i];
}
}
System.out.println(max); //54
}
}
遍历数组
方法一
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int[] arr2 = {23, 1, 54, 25, 5, 29};
//遍历数组
System.out.println("遍历数组:");
for(int n: arr2) {
System.out.println(n);
}
}
}
方法二
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int[] arr = {23, 1, 54, 25, 5, 29};
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
二维数组
二维数组也没什么好详细介绍的,直接看代码:
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
//二维数组
//声明:
int[][] intArray;
float floatArray[][];
double[] doublesArray[];
intArray =new int[3][3];
intArray[1][2] = 9;
//声明同时创建
char[][] ch = new char[3][3];
for(char[] n: ch) {
n = new char[3];
}
//创建一个float二维数组
floatArray = new float[3][];
floatArray[0] = new float[3];
floatArray[1] = new float[4];
floatArray[2] = new float[5];
for(float[] n: floatArray) {
for(float m: n) {
System.out.println(m);
}
}
}
}
未完待续
本作品采用《CC 协议》,转载必须注明作者和本文链接