Java基础
简单Java程序
Hello World
public class test {
public static void main(String args[]) {
System.out.println("Hello World!");
}
注释
java有三种注释:
1.单行注释
System.out.println("Hello World!");//这是一条单行注释
2.多行注释
/*这是一条多行注释
第二行
第三行*/
3.自动生成文档型注释
/**
* 文档型注释
* @author 418096798
*
*/
public class test {
public static void main(String args[]) {
System.out.println("Hello World!");//带换行打印
System.out.print("Hello World!");/*不带换行打印*/
}
}
数据类型
整形
| 类型 | 储存需求 | 取值范围 |
|---|---|---|
| byte | 1字节 | -128~127 |
| short | 2字节 | -32768~32767 |
| int | 4字节 | -2147483648~2147483647(超过20亿) |
| long | 8字节 | -9223372036854775808~9223372036854775807 |
浮点型
| 类型 | 储存结构 | 取值范围 |
|---|---|---|
| float | 4字节 | ±3.40282347E+38F(有效位数6~7位) |
| double | 8字节 | ±1.79769313486231570E+308 |
char型
| 转义序列 | 名称 | Unicode |
|---|---|---|
| \b | 退格 | \u0008 |
| \t | 制表 | \u0009 |
| \n | 换行 | \u000a |
| \r | 回车 | \u000d |
| \" | 双引号 | \u0022 |
| \' | 单引号 | \u0027 |
| \\ | 反斜杠 | \u005c |
布尔型
变量
变量初始化
int age;
初始化常量后可赋值
int age = 17;
//一年后...
int age - 18;
常量
利用final声明常量,声明后不可更改
final int ID = 001;//ID不可更改
运算符
运算符优先级
| 运算符 | 结合性 |
|---|---|
| [].()(方法调用) | 从左向右 |
| !~ ++ -- +(一元运算) -(一元运算) ()(强制类型转换)new | 从右向左 |
| */% | 从左向右 |
| + - | 从左向右 |
| << >> >>> | 从左向右 |
| < <= > >= instanceof | 从左向右 |
| == != | 从左向右 |
| & | 从左向右 |
| ^ | 从左向右 |
| | | 从左向右 |
| && | 从左向右 |
| ?: | 从左向右 |
| = += -= *= /= %/ &/ |= ^= <<= >>= >>>= | 从右向左 |
字符串
子串
String hello = "Hello wrold";
String string = hello.substring(1, 4);//ell
拼接
String hello = "Hello";
String world = "world";
String message = hello + world;//HelloWorld
检测字符串是否相等
String hello = "Hello";
String world = "world";
System.out.println(hello.equals(world));//false
字符串长度
String hello = "Hello";
int length = hello.length();
System.out.println(length);//5
大小写
String hello = "Hello";
String upper = hello.toUpperCase();
String lower = hello.toLowerCase();
System.out.println(upper);
System.out.println(lower);
索引
String hello = "Hello";
int index = hello.indexOf("l");//2
/*从第4个开始索引*/
int index = hello.indexOf("l", 4);//-1
替代
String hello = "Hello";
String replace = hello.replace("el", "");
删除字符串前后空格
String hello = " Hello ";
String trim = hello.trim();
构建字符串
String hello = "Hello";
String world = "World";
StringBuilder builder = new StringBuilder();
builder.append(hello);
builder.append(world);
String massae = builder.toString();
System.out.print(massae);//HelloWorld
输入输出
读取输入
import java.util.Scanner;
public class test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("what is your name?");
String name = in.nextLine();//读取输入的下一行内容
System.out.print("my name is ");
System.out.println(name);
}
}
格式化输出
宽度为8、保留2位小数点
public static void main(String args[]) {
double x = 1.0 / 3;
System.out.printf("%8.2f", x);
}
printf中的转换符
| 转换符 | 类型 | 举例 |
|---|---|---|
| d | 十进制整数 | 133 |
| x | 十六进制整数 | 9F |
| o | 八进制整数 | 527 |
| f | 定点浮点数 | 3.14 |
| s | 字符串 | Hello |
| c | 字符 | H |
| b | 布尔 | True |
printf中的标志
| 标志 | 目的 | 举例 |
|---|---|---|
| + | 打印正数和负数的符号 | +108 |
| 空格 | 在正数之前添加空格 | 144 |
| 0 | 数字前面补0 | 007 |
| - | 左对齐 | 33.333 |
| , | 添加分组分隔符 | 10,000,000 |
| #(对于f格式) | 包含小数点 | 3,333. |
| #(对于x或0格式) | 添加前缀0x或0 | 0x56E |
日期和时间的转换符
import java.util.Date;
public class test {
public static void main(String args[]) {
System.out.printf("%tc", new Date());
}
}
| 转换符 | 类型 | 举例 |
|---|---|---|
| c | 完整的的日期和时间 | 周六 9月 28 14:41:55 CST 2019 |
| F | ISO 8601日期 | 2919-09-19 |
| D | 美国格式的日期(月/日/年) | 02/09/2018 |
| T | 24小时时间 | 18:05:27 |
| r | 12小时时间 | 06:05:27 pm |
| R | 24小时时间(没有秒) | 18:05 |











网友评论