Tip:快捷熱鍵:
public static void main(String[] args) {}
System.out.println();
基礎(chǔ)語法
package Chapter1;
class Direction {
enum DirectionArrow{
East,West, North, Sourth
}
DirectionArrow arrow;
}
public class BasicSyntax {
public static void main(String[] args) {
/*
* 多行注釋
* 多行注釋
*/
System.out.println("Hello World!");
// 單行注釋
/* 單行注釋 */
Direction direction = new Direction();
direction.arrow = Direction.DirectionArrow.West;
System.out.println(direction.arrow);
}
}
對象、類
package Chapter2;
public class Dog {
/* 繁殖 */
String breed;
/* 年齡 */
int age;
/* 顏色 */
String color;
/* 咬 */
void barking() {
}
/* 饑餓 */
void hungry() {
}
/* 睡覺 */
void sleeping() {
}
public Dog() {
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public Dog(String name) {
System.out.println("小狗的名字是" + name);
}
public static void main(String[] args) {
Dog myDog = new Dog("Tom");
myDog.setAge(10);
myDog.getAge();
System.out.println("小狗的狗齡是" + myDog.age);
}
}
基本數(shù)據(jù)類型
package Chapter3;
public class BasicDataType {
public static void main(String[] args) {
System.out.println("BasicDataType");
/*Byte*/
System.out.println("基本類型:byte 二進(jìn)制位數(shù):" + Byte.SIZE);
System.out.println("包裝類:java.lang.Byte");
System.out.println("最小值:Byte.MIN_VALUE =" + Byte.MIN_VALUE);
System.out.println("最大值:Byte.MAX_VALUE =" + Byte.MAX_VALUE);
System.out.println();
/*Short*/
System.out.println(Short.SIZE);
System.out.println("java.lang.Short");
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println();
/*Int*/
System.out.println(Integer.SIZE);
System.out.println("java.lang.Integer");
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println();
/*Long*/
System.out.println(Long.SIZE);
System.out.println("java.lang.Long");
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println();
/*Float*/
System.out.println(Float.SIZE);
System.out.println("java.lang.Float");
System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println();
/*Double*/
System.out.println(Double.SIZE);
System.out.println("java.lang.Double");
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println();
/*Char*/
System.out.println(Character.SIZE);
System.out.println("java.lang.Character");
System.out.println((int)Character.MIN_VALUE);
System.out.println((int)Character.MAX_VALUE);
System.out.println();
final double PI = 3.1415927;
byte a = 68;
char a = 'A';
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
char a = '\u0001';
String a = "\u0001";
}
}
變量類型
package Chapter4;
public class Variable {
/*
類變量:獨(dú)立于方法之外的變量,用 static 修飾。
實(shí)例變量:獨(dú)立于方法之外的變量,不過沒有 static 修飾。
局部變量:類的方法中的變量。*/
public static int allClicks = 0;
private static int allClicksTime = 0;
public String str = "Hello World";
private String onlyStr = "Private";
public void method() {
int i = 0;
}
}
修飾符
package Chapter5;
public class Modifier {
/*修飾符*/
/*
共有的,以 public 修飾符指定,對所有類可見。
受保護(hù)的,以 protected 修飾符指定,對同一包內(nèi)的類和所有子類可見。
默認(rèn)的,也稱為 default,在同一包內(nèi)可見,不使用任何修飾符。
私有的,以 private 修飾符指定,在同一類內(nèi)可見。*/
/*
static 修飾符,用來創(chuàng)建類方法和類變量。
final 修飾符,用來修飾類、方法和變量,final 修飾的類不能夠被繼承,修飾的方法不能被繼承類重新定義,修飾的變量為常量,是不可修改的。
abstract 修飾符,用來創(chuàng)建抽象類和抽象方法。
synchronized 和 volatile 修飾符,主要用于線程的編程*/
/*運(yùn)算符*/
String name = "James";
boolean result = name instanceof String; // 由于 name 是 String 類型,所以返回真
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。