一、hallo world
1、选择File-New-Maven project
2、勾选第一项,跳过骨架选择
3、输入group id(com.lihaozhe)和Artifact id,点击完成
4、右键main方法,新建类,输入包名(group.Artifact)、类名
5、
package com.lihaozhe.day_06;
public class Demo01 {
public static void main(String[] args) {
System.out.println("我爱你中国"); //println("")输出后自动换行,去掉ln则不再换行
}
}
右键空白处,选择run as下的java application,即可运行项目并在控制台输出
我爱你中国
二、注释
1、注释分为单行注释,多行注释,和文档型注释
package com.lihaozhe.day04;
public class Demo02 {
/**
* 程序入口
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 注释 就是对一段程序的描述 便于程序员理解和使用该程序
// 单行注释
/*
* 多
* 行
* 注
* 释
*/
System.out.println("我爱你中国");
/**
* 文档型注释
* 一般用于注释一个类、一个方法或属性
*/
}
}
2、为文件添加默认注释
打开window-preference,找到java-Code Style-Code Templates,选中code-New Java files进行编辑(点击Edit),以文档型注释添加所需的默认注释如$data(日期)、$time(时间)等
这里也可以编辑新java文件的默认代码
三、Test方法的使用
1、右键test/java,新建类,类名需要有test,包是main的包
2、编写时不需要main方法
3、需要修改poml.xml文件,添加以下代码
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
其中版本号需要自行查找,之后等待在线下载junit组件包
查找方式为打开网址https://mvnrepository.com/,搜索juint,点击api,即可查看最新版本,点击最新版本,可以找到如上代码
4、
package com.lihaozhe.day04;
import org.junit.jupiter.api.Test; //拉取junit
public class DemoTest {
@Test //关联test
public void test01() {
System.out.println("北风卷地百草折");
}
@Test
public void test02() {
System.out.println("胡天八月即飞雪");
}
@Test
public void test03() {
String a = "忽如一夜春风来";
System.out.println(a);
}
@Test
public void test04() {
String b = "千树万树梨花开";
System.out.println(b);
}
选中哪个执行哪个,选中空白执行全部
四、变量与常量
1、变量的声明、赋值与输出
@Test
public void test05() {
int a = 10; //变量的赋值:数据类型 变量名 赋值符号 变量值分号
//变量就是在内存中凯尼一个空间,名就是空间的名字,查找用,值就是空间中所存放的东旭
System.out.println(a); //变量的输出 输出变量时不能加"",若增加,则只输出变量名
//八种基本的数据类型
byte z = 10; //数值型-整数型-byte变量,占用1B空间
short y = 10; //数值型-整数型-short变量,占用2B空间
int x = 10; //数值型-整数型-int变量,占用4B空间
long w = 10L; //数值型-整数型-long变量,占用8B空间,变量赋值时需要加L
double v = 10.0d; //数值型-浮点型-double变量,占用8B空间,变量赋值时需要加d
float u = 10.0f; //数值型-浮点型-float变量,占用4B空间,变量赋值时需要加f
char t = 't'; //字符型-char变量,占用2B空间,在java中采用unicode编码,所以占用2B
boolean s = true; //布尔型-boolean变量,占用1B空间,只有true或false
boolean r = false;
}
2、数据类型的修改
小转大自动转,大转小强制转
int a = 10; long b = a; // √
long a = 10; int b = a; // ×
强制转换可以但会造成数据失真
long a = 1;
int b = (int) a;
System.out.print(b);
3、常量,不可修改
final int a = 1;
此后a不可修改
五、字符串
1、字符串声明、赋值与读取
@Test
public void test01() {
String a = "忽如一夜春风来,";
String b = "千树万树梨花开。";
System.out.println(a); // 输出字符串时不能加"",若增加,则只输出变量名
System.out.println(b);
}
2、字符串拼接
@Test
public void test03() {
String a = "忽如一夜春风来,";
String b = "千树万树梨花开。";
System.out.print("北风卷地白草折,胡天八月即飞雪。" + "\n");
System.out.println(a + b); //+左右有一端为字符串即表示拼接,左右均需要有空格
System.out.println("散入珠帘湿罗幕,狐裘不暖锦衾薄。");
String c = "将军角弓不得控,";
System.out.println(c.concat("都护铁衣冷难着。")); //.concat 用的极少,比较麻烦
}
六、转义字符
@Test
public void test02() {
System.out.print("aaaaa\""); // \" 输出"
System.out.print("bbbbb\n"); // \n 换行
System.out.print("ccccc\r"); // \r 回车
System.out.print("ddddd\t"); // \t tab位
System.out.print("eeeee\\" + "\n"); // \\ 转义输出\
}
七、预定义格式
@Test
public void test04() {
System.out.println("""
北风卷地白草折,胡天八月即飞雪。
忽如一夜春风来,千树万树梨花开。
散入珠帘湿罗幕,狐裘不暖锦衾薄。
将军角弓不得控,都护铁衣冷难着
""");
//使用"""xxx"""输出xxx,内部格式可直接输出
}
八、运算符
1、数学运算符
@Test
public void test05() {
int a = 1;
int b = 2;
int c = a % b; // % 取模运算,只取余数
double e = (a + 0.0f) / b;
double f = (a + 0.0f) / 4; //java中除法为地板除
System.out.println(c);
System.out.println(e);
System.out.println(f); //Java中浮点型计算能但不精确
}
@Test
public void test06() {
int a = 10;
System.out.println(a++); //自增运算,此时输出a原值,因为先取a值输出才进行自增
System.out.println(++a); //自增运算,此时输出a++的值,因为先进行自增才取a值输出
System.out.println(a);
int b = a++; //b输出a原值,因为先取了a值赋给b才进行a的自增运算,之后并没有第二次给b赋值
// int b = a;
// a = a + 1;
System.out.println(b);
System.out.println(a);
}
2、比较运算符
@Test
public void test13() {
System.out.print("10 == 10 >>>" + (10 == 10) + "\t"); // == 等于,比较对象是否相等
System.out.println("10 == 20 >>>" + (10 == 20));
System.out.print("10 != 10 >>>" + (10 != 10) + "\t"); // != 不等于,比较对象是否不等
System.out.println("10 != 20 >>>" + (10 != 20));
System.out.print("10 > 20 >>>" + (10 > 20) + "\t"); // > 大于,返回x大于y的结果
System.out.println("10 < 20 >>>" + (10 < 20)); // < 小于,返回x小于y的结果
System.out.print("10 <= 10 >>>" + (10 <= 10) + "\t"); // <= 小于等于,返回x小于等于y的结果
System.out.print("10 <= 11 >>>" + (10 <= 11) + "\t");
System.out.println("10 <= 9 >>>" + (10 <= 9));
System.out.print("10 >= 10 >>>" + (10 >= 10) + "\t"); // >= 大于等于,返回x大于等于y的结果
System.out.print("10 >= 11 >>>" + (10 >= 11) + "\t");
System.out.println("10 >= 9 >>>" + (10 >= 9));
// 输出值为True或False
}