是的,可以透過強制轉換將變數向下轉型為其範圍較低的替代型別。儘管這可能會導致資料丟失。請參見下面的示例 -示例線上演示public class Tester { public static void main(String[] args) { int a = 300; byte b = (byte)a; System.out.println(b); } }輸出它將輸出 -44
不可以。Java 建構函式不能返回值。如果需要,只需建立一個呼叫所需建構函式並返回所需值的方法即可。請參見下面的示例。public class Tester { public Tester(){} public static Tester getInstance(){ Tester tester = new Tester(); return tester; } }