Java基礎(chǔ)
簡(jiǎn)單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.自動(dòng)生成文檔型注釋
/**
* 文檔型注釋
* @author 418096798
*
*/
public class test {
public static void main(String args[]) {
System.out.println("Hello World!");//帶換行打印
System.out.print("Hello World!");/*不帶換行打印*/
}
}
數(shù)據(jù)類型
整形
| 類型 | 儲(chǔ)存需求 | 取值范圍 |
|---|---|---|
| byte | 1字節(jié) | -128~127 |
| short | 2字節(jié) | -32768~32767 |
| int | 4字節(jié) | -2147483648~2147483647(超過20億) |
| long | 8字節(jié) | -9223372036854775808~9223372036854775807 |
浮點(diǎn)型
| 類型 | 儲(chǔ)存結(jié)構(gòu) | 取值范圍 |
|---|---|---|
| float | 4字節(jié) | ±3.40282347E+38F(有效位數(shù)6~7位) |
| double | 8字節(jié) | ±1.79769313486231570E+308 |
char型
| 轉(zhuǎn)義序列 | 名稱 | Unicode |
|---|---|---|
| \b | 退格 | \u0008 |
| \t | 制表 | \u0009 |
| \n | 換行 | \u000a |
| \r | 回車 | \u000d |
| \" | 雙引號(hào) | \u0022 |
| \' | 單引號(hào) | \u0027 |
| \\ | 反斜杠 | \u005c |
布爾型
變量
變量初始化
int age;
初始化常量后可賦值
int age = 17;
//一年后...
int age - 18;
常量
利用final聲明常量,聲明后不可更改
final int ID = 001;//ID不可更改
運(yùn)算符
運(yùn)算符優(yōu)先級(jí)
| 運(yùn)算符 | 結(jié)合性 |
|---|---|
| [].()(方法調(diào)用) | 從左向右 |
| !~ ++ -- +(一元運(yùn)算) -(一元運(yùn)算) ()(強(qiáng)制類型轉(zhuǎn)換)new | 從右向左 |
| */% | 從左向右 |
| + - | 從左向右 |
| << >> >>> | 從左向右 |
| < <= > >= instanceof | 從左向右 |
| == != | 從左向右 |
| & | 從左向右 |
| ^ | 從左向右 |
| | | 從左向右 |
| && | 從左向右 |
| ?: | 從左向右 |
| = += -= *= /= %/ &/ |= ^= <<= >>= >>>= | 從右向左 |
字符串
子串
String hello = "Hello wrold";
String string = hello.substring(1, 4);//ell
拼接
String hello = "Hello";
String world = "world";
String message = hello + world;//HelloWorld
檢測(cè)字符串是否相等
String hello = "Hello";
String world = "world";
System.out.println(hello.equals(world));//false
字符串長(zhǎng)度
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個(gè)開始索引*/
int index = hello.indexOf("l", 4);//-1
替代
String hello = "Hello";
String replace = hello.replace("el", "");
刪除字符串前后空格
String hello = " Hello ";
String trim = hello.trim();
構(gòu)建字符串
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();//讀取輸入的下一行內(nèi)容
System.out.print("my name is ");
System.out.println(name);
}
}
格式化輸出
寬度為8、保留2位小數(shù)點(diǎn)
public static void main(String args[]) {
double x = 1.0 / 3;
System.out.printf("%8.2f", x);
}
printf中的轉(zhuǎn)換符
| 轉(zhuǎn)換符 | 類型 | 舉例 |
|---|---|---|
| d | 十進(jìn)制整數(shù) | 133 |
| x | 十六進(jìn)制整數(shù) | 9F |
| o | 八進(jìn)制整數(shù) | 527 |
| f | 定點(diǎn)浮點(diǎn)數(shù) | 3.14 |
| s | 字符串 | Hello |
| c | 字符 | H |
| b | 布爾 | True |
printf中的標(biāo)志
| 標(biāo)志 | 目的 | 舉例 |
|---|---|---|
| + | 打印正數(shù)和負(fù)數(shù)的符號(hào) | +108 |
| 空格 | 在正數(shù)之前添加空格 | 144 |
| 0 | 數(shù)字前面補(bǔ)0 | 007 |
| - | 左對(duì)齊 | 33.333 |
| , | 添加分組分隔符 | 10,000,000 |
| #(對(duì)于f格式) | 包含小數(shù)點(diǎn) | 3,333. |
| #(對(duì)于x或0格式) | 添加前綴0x或0 | 0x56E |
日期和時(shí)間的轉(zhuǎn)換符
import java.util.Date;
public class test {
public static void main(String args[]) {
System.out.printf("%tc", new Date());
}
}
| 轉(zhuǎn)換符 | 類型 | 舉例 |
|---|---|---|
| c | 完整的的日期和時(shí)間 | 周六 9月 28 14:41:55 CST 2019 |
| F | ISO 8601日期 | 2919-09-19 |
| D | 美國(guó)格式的日期(月/日/年) | 02/09/2018 |
| T | 24小時(shí)時(shí)間 | 18:05:27 |
| r | 12小時(shí)時(shí)間 | 06:05:27 pm |
| R | 24小時(shí)時(shí)間(沒有秒) | 18:05 |