Java
Contents
Java
1、Hello world
java 的包名需要和目录一致,类名需要与文件名一致
[sugar@Sugar java]🐳 tree .
.
└── Hello
└── HelloWorld.java
1 directory, 1 file
HelloWorld.java
package Hello;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello,world");
}
}
[sugar@Sugar Hello]🐳 java HelloWorld.java
hello,world
[sugar@Sugar Hello]🐳
2、变量定义
int y;
int x = 1;
String s = "hello";
final double PI = 3.14; // PI是一个常量
double r = 5.0;
double area = PI * r * r;
var sb = new StringBuilder();
int a, b, c; // 声明三个int型整数:a、 b、c
int d = 3, e = 4, f = 5; // 声明三个整数并赋予初值
byte z = 22; // 声明并初始化 z
String s = "runoob"; // 声明并初始化字符串 s
double pi = 3.14159; // 声明了双精度浮点型变量 pi
char x = 'x'; // 声明变量 x 的值是字符 'x'。
public class RunoobTest {
// 成员变量
private int instanceVar;
// 静态变量
private static int staticVar;
public void method(int paramVar) {
// 局部变量
int localVar = 10;
// 使用变量
instanceVar = localVar;
staticVar = paramVar;
System.out.println("成员变量: " + instanceVar);
System.out.println("静态变量: " + staticVar);
System.out.println("参数变量: " + paramVar);
System.out.println("局部变量: " + localVar);
}
public static void main(String[] args) {
RunoobTest v = new RunoobTest();
v.method(20);
}
}
3、流程控制
出入输出
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建Scanner对象
System.out.print("Input your name: "); // 打印提示
String name = scanner.nextLine(); // 读取一行输入并获取字符串
System.out.print("Input your age: "); // 打印提示
int age = scanner.nextInt(); // 读取一行输入并获取整数
System.out.printf("Hi, %s, you are %d\n", name, age); // 格式化输出
}
}
Maven
配置settings
默认读取位置
- ~/.m2
curl -O https://repo1.maven.org/maven2/archetype-catalog.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/home/sugar/.m2/repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>archetype-catalog</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<archetypeCatalog>file:///Users/sugar/.m2/archetype-catalog.xml</archetypeCatalog>
</properties>
</profile>
</profiles>
</settings>
手动下载依赖
mvn -B -f pom.xml -s /usr/share/maven/ref/settings.xml dependency:resolve
打包
mvn package
mvn demo