發表文章

目前顯示的是有「Java基礎語法」標籤的文章

「Java」「Interface」 instanceof來判斷是否有實作interface

Given: interface Interface{ //Interface inter1 = new Super();//錯誤,Cannot convert Super to Interface //Super sup1 = new Interface();//錯誤,Cannot convert Interface to Super } class Super{ //Interface inter1 = new Super();//正確,假如父類別繼承Interface //Super sup1 = new Interface();//錯誤,Cannot convert Interface to Super } public class Sub extends Super implements Interface{ public static void main(String[] args) { Interface inter = new Sub(); System.out.println(inter instanceof Object); System.out.println(inter instanceof Interface); System.out.println(inter instanceof Super); System.out.println(inter instanceof Sub); Super sup = new Sub(); System.out.println(sup instanceof Object); System.out.println(sup instanceof Interface); System.out.println(sup instanceof Super); System.out.println(sup instanceof Sub); Sub sub = new Sub(); System.out.println(sub instanceof Object); System.out.println(sub instanceof Interface); System.out.println(sub instanceof Super); S...

「Java」Static 方法與欄位

圖片
Given: class StaticDemo{ private int Field;//物件欄位 private static int StaticField;//static欄位 public void Method() { //物件方法 Field=2; //可。物件欄位 StaticField=3;//可。static欄位 Method();//可。物件方法 CallStatic();//可。static方法 } public static void CallStatic() { StaticField=5;//可。static欄位 } public static void StaticMethod() { Field = 1;//不可。物件欄位 StaticField=3;//可。static欄位 Method();//可。物件方法 CallStatic();//不可。static方法 } } 解說:  在Static方法中,是無法呼叫物件欄位及物件方法。 但物件方法中,是可以呼叫所有的欄位及方法。 子類別中若有相同的簽名static方法,是無法被覆寫的。

「Java」Regular Expression 正則表示法

正則表示法: import java.util.Scanner; public class regular { public static void main(String[]args) { String use; Scanner scan = new Scanner(System.in); System.out.println("請輸入資訊:"); use = scan.next(); if(use.matches("[0-9]{4}-[0-9]{6}")) { System.out.println("輸入的資料:"+use); }else if(use.matches(".{2}[市縣].{2,}[路街道]([.1-9]段)?(.|\\d)+(-\\d+)?[號].*")) { System.out.println("輸入的資料:"+use); }else if(use.matches("^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$")) { System.out.println("輸入的資料:"+use); }else { System.out.print("輸入失敗"); } } } 以下資料來源 :  Java Document (https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) 線上測試軟體: https://www.freeformatter.com/java-regex-tester.html#ad-output http://myregexp.com/ Summary of regular-expression constructs Construct Matches Characters x The character x \\ The backslash character \0 n ...

「Java」 throw 與 throws

解析: 為何要放在try...catch之中? 因為所有 IO 操作指令都可能拋出例外, 因此必須放在 try...catch 區塊中。 Java中的讀取與寫入檔案可以透過FileReader和FileWriter完成 讀取檔案(使用BufferedReader方便讀取整行) FileReader 與 FileWriter 兩個類別用在處理字元(char)單位。 也對應到 FileInputStream 與 FileOutputStream 。 可是當需要處理純文字,就不是用以上的方法。 而是使用 BufferedReader 與 BufferedWriter 緩衝區來處理檔案讀寫,並加速速度。 FileReader: FileReader(File file) // 建立一個新的FileReader,給指定的文件讀取。 FileReader(FileDescriptor file) // 建立一個新的FileReader,給指定的FileDescriptor讀取。 FileReader(String fileName) // 建立一個新的FileReader,給要讀取的文件的名稱。 繼承來自於: Java.io.InputStreamReader Java.uti.Reader Java.io.Object FileWriter: FileWriter(File file) // Constructs a FileWriter object given a File object. FileWriter(File file, boolean append) // Constructs a FileWriter object given a File object. FileWriter(FileDescriptor fd) // Constructs a FileWriter object associated with a file descriptor. FileWriter(String fileName) // Constructs a FileWriter object given a file name. FileWriter(String fileName, boolean append) // Constructs a...

「Java」 FileReader和FileWriter

解析: 為何要放在try...catch之中? 因為所有 IO 操作指令都可能拋出例外, 因此必須放在 try...catch 區塊中。 Java中的讀取與寫入檔案可以透過FileReader和FileWriter完成 讀取檔案(使用BufferedReader方便讀取整行) FileReader 與 FileWriter 兩個類別用在處理字元(char)單位。 也對應到 FileInputStream 與 FileOutputStream 。 可是當需要處理純文字,就不是用以上的方法。 而是使用 BufferedReader 與 BufferedWriter 緩衝區來處理檔案讀寫,並加速速度。 FileReader: FileReader(File file) // 建立一個新的FileReader,給指定的文件讀取。 FileReader(FileDescriptor file) // 建立一個新的FileReader,給指定的FileDescriptor讀取。 FileReader(String fileName) // 建立一個新的FileReader,給要讀取的文件的名稱。 繼承來自於: Java.io.InputStreamReader Java.uti.Reader Java.io.Object FileWriter: FileWriter(File file) // Constructs a FileWriter object given a File object. FileWriter(File file, boolean append) // Constructs a FileWriter object given a File object. FileWriter(FileDescriptor fd) // Constructs a FileWriter object associated with a file descriptor. FileWriter(String fileName) // Constructs a FileWriter object given a file name. FileWriter(String fileName, boolean append) // Construct...

「Java」 parseInt

語法與解析: static int parseInt(String s) 將字串解析為十進位整數 static int parseInt(String s, int radix) 將字串解析為指定進位的整數 用法: public class ParseInt { public static void main(String[] args) { String s1 = "11"; int a1 = Integer.parseInt(s1); int a2 = Integer.parseInt(s1, 16); System.out.println(a1); //answer: 11 System.out.println(a2); //answer: 17 String s2 = "one"; int b1 = Integer.parseInt(s1); int b2 = Integer.parseInt(s1, 16); System.out.println(b1); //wrong System.out.println(b2); //wrong } }

[Java] PrintStream

Java.io.PrintStream 是一個 print 的方法,不同的地方在於將字串寫到檔案的時候不會產生 IOException ,使用方法請看下面的筆記:    PrintStream ps = new PrintStream(System.out); // 一般的 output    PrintStream ps = new PrintStream(new File()); // 寫到檔案    ps.print("values"); // 要寫入的值 範例: import java . io .*; public class PrintStreamDemo { public static void main ( String [] args ) { // create printstream object PrintStream ps = new PrintStream ( System . out ); // print this string ps . print ( "This is a String" ); // print new line ps . println (); // print a new string ps . println ( "New line" ); // flush the stream ps . flush (); } } 結果: This is a String New line

[Java]OCAJP證照通過

耗費了一年的時間,才通過OCAJP的證照,至於為什麼這麼久,也是因為我沒認真的在準備他。 我也不是一次就成功,而是到了第四次才通過它。這時候有些人因該就開始動金錢腦,算出我花了多少?

Java - String類別

String 類別有以下幾種:

[Java][API]Action Listener

Document Reference By: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html  The Action Listener API The ActionListener Interface Because ActionListener has only one method, it has no corresponding adapter class. Method Purpose actionPerformed(actionEvent) Called just after the user performs an action. The ActionEvent Class Method Purpose String getActionCommand() Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommand that lets you set this string. int getModifiers() Returns an integer representing the modifier keys the user was pressing when the action event occurred. You can use the ActionEvent -defined constants SHIFT_MASK , CTRL_MASK , META_MASK , and ALT_MASK to determine which keys were pressed. For example, if the user Shift-selects a menu item, then the following expression is nonzero: actionEvent.getModifiers() & ActionEvent.SHIFT_MASK Object get...

[Java] for迴圈 - 九九乘法表

public class Level { public static void main(String[] args){ for(int j=1;j 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

[Java] Switch條件式

import java.util.Scanner; public class Level { public static void main(String[] args){ //int scor =88; //int quotient = scor/10; //char level; Scanner scanner = new Scanner(System.in); System.out.println("Please type score: "); int score = scanner.nextInt(); int quotient = score /10; char level; switch(quotient){ case 10: case 9: level = 'A'; break; case 8: level = 'B'; break; case 7: level = 'C'; break; case 6: level = 'D'; break; default: level = 'E'; break; } System.out.printf("成績為%c%n",level); } }