就像第一章所說同樣,此次學習爲了快,所以說明性的文字就不想寫太多了,直接帖代碼吧,代碼當中儘可能加一些註釋:java
package a.b; public class test { static void BasicVariables() { //1、變量的類型的學習 System.out.println("1、變量的類型的學習 "); // byte 表數範圍:-128~127, 存儲大小佔1byte byte a; a = 12; System.out.println("byte num Is " + a); // int 佔4字節 int b; b = 66633; System.out.println("int num Is " + b); // short 佔2字節 short c; c = 1234; System.out.println("short num Is " + c); // long 佔2字節 long d; d = 655366; System.out.println("long num Is " + d); float e; e = (float)12.6; System.out.println("fload num Is " + e); // int 佔4字節 double f; f = 33.4; System.out.println("double num Is " +f); // short 佔2字節 char g; g = 'a'; System.out.println("char num Is " + g); // long 佔2字節 boolean h; h = true; System.out.println("boolean num Is " + h); } static void AboutArrays() { // 2、 數組的學習 System.out.println("2、 數組的學習 "); // 基本類型數組賦值、輸出 int []a ; a = new int [5]; a[0] = a[1] = a[2] = a[3] = a[4] = 9; for (int i = 0; i < 5; i++) { System.out.println(a[i]); } // 基本類型數組賦值、輸出 int []b = new int [5]; for (int i = 0; i < 5; i++) { b[i] = a[i] + i +1; System.out.println(b[i]); } // 基本類型數組初始化時候賦值 int []c = new int [] {3,4,5,6,7}; for (int i = 0; i < 5; i++) { System.out.println(c[i]); } // 字符數組 String []d = new String [] {"you","are","my","small","apple"}; for (int i = 0; i < 5; i++) { System.out.println(d[i]); } } public static void main(String[] args) { //1、基本變量 BasicVariables(); //2、數組 AboutArrays(); } }
複製代碼數組
附帶一下輸出結果把:app
1、變量的類型的學習 ide
byte num Is 12 int num Is 66633 short num Is 1234 long num Is 655366 fload num Is 12.6 double num Is 33.4 char num Is a boolean num Is true
2、 數組的學習 函數
9 9 9 9 9 10 11 12 13 14 3 4 5 6 7 you are my small apple
這裏須要說一個問題,就是剛開始 我本身寫的兩個函數都不是靜態的,編譯的時候就發生了報錯學習
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 對象
Cannot make a static reference to the non-static method BasicVariables() from the type test內存
後來在網上查了以後:解釋爲在靜態方法中,不能直接訪問非靜態成員(包括方法和變量)。由於,非靜態的變量是依賴於對象存在的,對象必須實例化以後,它的變量纔會在內存中存在。it
因此解決方案就是如下兩種:io
複製代碼
// 方法一:在靜態函數main中,實例化對象以後進行調用非靜態函數BasicVariables public class test { void BasicVariables() { //1、變量的類型的學習 System.out.println("1、變量的類型的學習 "); } public static void main(String[] args) { test t = new test(); t.BasicVariables(); } } //方法二:直接定義靜態函數BasicVariables,在靜態函數main中直接調用 public class test { static void BasicVariables() { //1、變量的類型的學習 System.out.println("1、變量的類型的學習 "); } public static void main(String[] args) { BasicVariables(); } }