更新時間:2022-12-12 12:28:22 來源:動力節(jié)點 瀏覽1260次
System.out.println("哪吒javase講的真好");
println源碼:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
private void newLine() {
try {
synchronized (this) {
ensureOpen();
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
package com.nezha.javase;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("你是誰?");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.println("你幾歲了?");
int age = in.nextInt();
System.out.println("我是"+name + ",我今年"+age+"歲啦!");
}
}
(1)類型轉(zhuǎn)換字符
b | Boolean值 | h | 散列碼(16進制) |
c | Unicode字符 | s | String |
d | 整數(shù)型(10進制) | x | 整數(shù)(16進制) |
e | 浮點數(shù)(科學計數(shù)) | % | 字符“%” |
f | 浮點數(shù)(10進制) |
(2)代碼實例
package com.nezha.javase;
public class Test {
public static void main(String[] args) {
int age = 29;
String name = "哪吒";
String info = String.format("My name is %s and my age is %d", name, age);
System.out.println(info);
}
}
(3)控制臺輸出
(1)什么是包
為了更好地組織類,Java 提供了包機制,用于區(qū)別類名的命名空間。
(2)包的作用
把功能相似或相關(guān)的類或接口組織在同一個包中,方便類的查找和使用。
如同文件夾一樣,包也采用了樹形目錄的存儲方式。同一個包中的類名字是不同的,不同的包中的類的名字是可以相同的,當同時調(diào)用兩個不同包中相同類名的類時,應(yīng)該加上包名加以區(qū)別。因此,包可以避免名字沖突。
包也限定了訪問權(quán)限,擁有包訪問權(quán)限的類才能訪問某個包中的類。
(3)Java 包作用域
1)public
可以被任何類使用
2)private
只能被定義它的類使用
3)protected
可以被自己、同一包下的其它類、子類使用
4)默認作用域
可以被自己和同一包下的其他類使用
(1)import
為了能夠使用某一個包的成員,我們需要在 Java 程序中明確導入該包,使用 "import" 語句可完成此功能。
如果在一個包中,一個類想要使用本包中的另一個類,那么該包名可以省略。
(2)static import靜態(tài)導入
jdk1.5里引入了“Static Import”機制,借助這一機制,可以用略掉所在的類或接口名的方式,來使用靜態(tài)成員。static import和import其中一個不一致的地方就是static import導入的是靜態(tài)成員,而import導入的是類或接口類型。