Java学习基础
Java学习基础
Last edited 2023-4-3
type
Post
status
Published
date
slug
summary
学习Java的一些感悟看法,记录日常bug并尝试尝试提出解决办法
tags
学习
category
学习思考
icon
password
Property
Apr 3, 2023 01:07 PM
notion image

java入门

大二上学期开始学习java,虽然一直听说java是一门很厉害的语言,但没有系统去的学习😃😃 就我目前刚学来看,Java的语法很像c语言和javascript二者的结合,感觉很多东西都学过,一切都那么似曾相识。 但我知道这样是不对的,后面我会积极改正这种错误思想☹️☹️☹️

java学习路线

https://luxian.yupi.icu/#/

安装jdk

java的运行离不开jdk编译,同时也需要配置环境,环境配置直接搜教程即可

编译软件的使用

1.官网下载eclipse免费,安装简易就不再多说了
 
教程还是推荐用菜鸟教程
 

描述java语言特点

1.简单性 2.面向对象型 3.安全性 4.跨平台性---JAVA虚拟机 5.支持多线程 任务管理器 进程---线程
 

hello world的运行

众所周知,所有编译语言的学习都是从hello world开始的,java当然也不例外
public class main{ public static void main(String[] args) { System.out.println("帅哥美女你们好啊"); }}
 

标识符

标识符可以由任意顺序的大小写字母、数字、下划线(_)和美元 符号($)组成,但不能以数字开头,不能是Java中的关键字。
notion image

关键字

notion image
 

变量与常量

变量是一种可以在程序中改变的量。它们可以用来存储和表示数据,并且可以在程序的不同部分之间共享。在程序运行期间,随时可能产生一些临时数据,应用程序会将这些数据保存在一些内存单元中,每个内存单元都用一个标识符来标识。这些内存单元我们称之为变量,定义的标识符就是变量名,内存单元中存储的数据就是变量的值。
常量是一种不可更改的量,它们的值在程序执行期间保持不变。

数据类型*

notion image
notion image
基本类型
默认值
说明
byte
0
是int型变量所占空间的四分之一
short
0
是int型变量所占空间的二分之一
int
0
一般地整型变量默认为 int 类型
long
0L
主要使用在需要比较大整数的系统上
flout
0.0f
浮点数不能用来表示精确的值,如货币
double
0.0d
类比flout,但精度更高
char
‘u0000’
char 数据类型可以储存任何字符
String
null
字符串,即多个字符
boolean
false
只作为一种标志来记录 true/false 情况
引用数据类型
null
如数组和对象
notion image

数值型

整数型

notion image

浮点型

notion image
notion image

字符型

notion image
notion image

布尔型

notion image
 
 

数据类型转化

notion image

自动类型

notion image
notion image
将小范围转化为大范围,char可转int(在java中可实现,c语言不行)
notion image
notion image

强制类型

notion image
目标类型变里 =(目标类型)值
在对变量进行强制类型转换时,会发生取值范围较大的数据类型向取值范围较小的数据类型的转换,如将一个int类型的数转为byte类型,这样做极容易造成数据精度的丢失。
 
注:所谓表达式是指由变量和运算符组成的一个算式。变量在表达式中进行运算时,也有可能发生自动类型转换,这就是表达式数据类型的自动提升,如一个byte型的变量在运算期间类型会自动提升为int型
 

字符常量

notion image

字符串常量

notion image
 

进制转换

notion image
notion image
 

算术运算符

notion image
notion image
取模%公式:a%b=a-a/b*b;
运算
结果
10%3
1
-10%3
-1
10%-3
1
-10%-3
-1
结论
看除数符号,即a的符号

赋值运算符

notion image
notion image
notion image
notion image
notion image

比较运算符

notion image
notion image
 

逻辑运算符

notion image
notion image
notion image
 

运算优先级

notion image
 
 

位运算

notion image
 

程序语句*

选择

switch

notion image
notion image
notion image
 

while

notion image
notion image
 

跳转语句

notion image
notion image
notion image
 

方法

notion image
 

方法重载*

notion image
notion image
notion image
 

数组

数组在内存中是连续分配的
notion image
notion image

数组常见操作

遍历

通常用for循环
使用 for 循环可以遍历一个数组:
// 定义一个数组 int[] arr = {1, 2, 3, 4, 5}; // 使用 for 循环遍历数组 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } // 输出结果 1 2 3 4 5

最值

编写获取最大值方法
public static int getMax(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; }

排序

通常用冒泡排序
notion image
notion image
public class BubbleSort { /** * 冒泡排序方法 * @param array 需要排序的数组 */ public static void bubbleSort(int[] array) { int temp; // 定义一个临时变量 for (int i = 0; i < array.length - 1; i++) { // 外层循环,控制排序趟数 for (int j = 0; j < array.length - 1 - i; j++) { // 内层循环,控制每一趟排序多少次 if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void main(String[] args) { int[] array = {3, 5, 1, 2, 6}; System.out.println("排序前的数组:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); bubbleSort(array); System.out.println("排序后的数组:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } }

多维数组

notion image
notion image
notion image
notion image
 
 

修饰符

notion image
static方法名不能使用纯小写,需要用驼峰写法

String.substring()方法使用:

java String类substring() 方法返回字符串的子字符串
1.substring(n)->从n开始索引
2.substring(n,m)->从n开始到m结束

不同格式键盘输入

String email = new Scanner(System.in).nextLine(); int email = new Scanner(System.in).nextInt();

Java Number & Math

方法
function
say
rond
四舍五入
3.3→3,3.5→4
floor
向下取整
3.9→3,3.3→3
ceil
向上取整
3.3→4,3.9→4

bug

在写代码的过程难免会遇到一些bug,在这里我会一一列出以便debug

打印三角形

//正确打印三角形,demo01 public static void main(String[] args) { for(int i = 1; i <= 5; i++) { for(int j = 1; j <i; j++) { System.out.print("*"); } System.out.print("\\n"); } }
//错误示范,demo02 for (int i = 1; i < 5; i++) { // for (int j = 1; j <= i; j++) { // System.out.println("*"); // } // System.out.println("\\n"); // }
注意观察上面二片代码有什么区别,很明显对吧,输出的方式不一样
最后科普一下,print和println的区别:
虽然二者都是在控制台输出,但前者将输出光标定位在所显示的最后一个字符之后,而后者会在结尾加上换行符,将输出光标定位在下一行的开始

打印三角形的其他bug

public static void main(String[] args) { //打印三角形 //1.提示并接收用户输入的行数: System.out.println("请输入您要打印星星的行数:"); int n = new Scanner(System.in).nextInt(); //2.开始打印图形 //2.1外层循环控制打印的行数 i的取值范围:[1,n],一共n行 for(int i=1;i<=n;i++){ //2.2内层循环1 //控制每行打印空格的个数 j的取值范围:[0,n-i),即1 2 3... for(int j=0;j<n-i;j++){ System.out.print(" "); } //2.3内层循环2 //控制每行打印星星的个数 k的取值范围:[1,2*i-1],即1 3 5 ... for(int k=1;k<=2*i-1;k++){ System.out.print("*"); } //2.4打印完本行所有内容后添加换行 System.out.println(); } }
这里用到了三层循环,注意j和k是并列的并不是包含关系
 
  • 学习
  • mybatisjava面向对象思想