- import java.util.scanner
- public class ScannerDemo{
- public static void main(String[] args){
-
- Scanner scanner = new Scanner(System.in);
- System.out.print("請輸入您的名字");
- System.out.print("喔嗨喲!%s!/n",scanner.next());
- }
- }
Running Result : 請輸入您的名字 : Finanshow 喔嗨喲!Finanshow
Java基礎語法複習要點
第一.數據與運算
<1>數據類型(Primitive Type)
整數:short(2 byte) int(4 byte) long(8 byte)
字節: Byte(1 byte)
浮點數: float(4 byte) double (8 byte)
布爾數 : boolean (2 byte)
字符 :char 必須瞭解Uicode編碼表 在Unicode出現之前有許多的字符編碼標準美國的ASCII 西歐語言中的ISO 8859-1,中國的GB118030
<2>位運算:&(AND) ; | (OR) ; ^(XOR) ; ~(補碼) ; <<(左移) ; >>(右移) ;
巧用位運算判斷奇偶
((number&1)!= 0 ? '是' : '否'));
二進制中,奇數最右邊肯定爲1,偶數最右邊肯定爲0
整數4 : 00000100 整數1 : 00000001 AND後 00000000
整數3 : 00000011 整數1 : 00000001 AND後 00000001
PS:爲了舉例方便,運算時,INT類型要考慮32位長度而不只是8位,因爲INT佔據內存字節有4個BYTE
<3>遞增(increment) 和 遞減(decrement) 運算
++i; //相當於 i= i+1; 先對變量的值進行加法,然後再做賦值運算 同理--i;
i++; //先做賦值運算,再對變量i進行運算;
第二.裝箱autoboxing與拆箱unboxing
裝箱:將數據存入對象中,數據存在於stack,將其複製到heap中的行爲叫裝箱
拆箱:將數據從對象中抽離
<1>The relationship between Class and Object類和對象之間的關係
回顧前面的基本數據類型:long,int,double,float,boolean,byte並不是對象,在J2SE5.0之前,如果要讓基本數據類型像對象一樣進行操作,要使用所謂的打包類(Wrapper):Long,Integer,Double,Float,Boolean,Byted鞥
- public class WrapperDemo{
- public static void main(String[] args){
-
- int data1 = 10;
- int data2 = 20;
-
- Integer data1Wrapper = new Integer(data1);
- Integer data2Wrapper = new Integer(data2);
-
- System.out.println(data1Wrapper.doubleValue()/3);
-
- System.out.println(data1Wrapper.compareTo(data2Wrapper));
-
- }
- }
PS:J2SE5.0之後,提供autoBoxing自動打包機制
<2>自動裝箱和拆箱
J2SE5.0之後提供的自動裝箱功能,爲了讓我們瞭解"自動"在實際代碼中的體現,讓我們來重寫上買你的代碼
- public class AutoBoxingDemo{
- public static void main(String[] args){
-
- Integer data1 = 10;
- Integer data2 = 20;
- System.out.println(data1.doubleValue()/3);
- System.out.println(data1.compareTo(data2));
- }
-
- }
學習自動裝箱和拆箱還有許多資源:
Autoboxing: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
第三:數組 JAVA中,數組不光是數據的集合,更是一個對象
<1>一維數組 int [] array = new int[10];//完整的數據定義方法
int [] array = new int[] {2,3,4,1,3};//不用聲明數組的大小
通過數組的定義,我們可以看出來數組是int[]類型的引用,即對象
<2>二維數組
int [][] arr = {{1,2,3},{4,5,6}};//定義了一個2Row3Clumnd的數組
<3>JAVA中數組的高級特性,用對象的概念去看待數組
int [] arr = new int[]{1,2,3,4,5}
int [] temp1 = arr;
int [] temp2 = arr;
實際上是temp1,temp2,arr同時引用了同一個數組對象
<4>foeach 和 數組
int [] arr = {1,2,3,4,5,6,3};//new int []可以省略
for (int element : arr)
System.out.println(element);
第四 字符串 :無論如何字符串運用得好不好,直接影響到程序的效率
<1> 字符串 在JAVA中以string的一個實例存在,每一個字符串對象本身會有幾個可操作的方法
length();//取得字符串長度
equals();//判斷源字符是否與目標字符串相等
toLowerCase();//轉小寫
toUpperCase();//轉大寫
<2>不可變字符串
string str = "Just";
str = "Justin";
怎麼樣實現刷新STR的值,原理跟引用有關,就是str重新指向了一個stack裏面的內容爲Justin而不是剛纔那個stackJust
第五 封裝 Enapusucation
<1>簡單的對象分析過程:
單純問題,從問題中的名詞來識別對象,複雜問題不一定行得通
OOP分析的相關書籍
Applying UML and Patterns;
An Intorduction to Object-Oriented Analysis and Design and the Unified Process ,Second Edition By Craig Larman
<2>使用class定義類
PS:一個文件中可以定義很多個類但是隻能有一個類是public 的,文件名稱要跟這個public的類同名
關於static:靜態成員屬於類所有,通過類名稱來操作存取static成員,靜態方法不允許調用非靜態成員,注意區別靜態成員,非靜態成員,非靜態方法,靜態方法之間的調用準則
- class Example1
- { static public int a;
- public int b;
- void Meth1(){
- a=10;
- b=20;
- }
- static void Meth2(){
- a=30;
- b=40;
- }
- public static void Main(){
- Example1 E1=new Example1();
- Example1 E2=new Example1();
- E1.Meth1();
-
- System.out.print("a={0},b={1}",Example1.a,E1.b);
-
- Example1.a=Example1.a+50;
- E2.b=Example1.a+60;
- System.out.print("a={0},b={1}",Example1.a,E2.b);
- }
- }
<3>關於方法
*重載OverLoad
*遞歸Recursion
舉個遞歸求最大公因子的例子
- private static int gcd(){
-
- if (n == 0)
- return m;
- else
- return gcd(n , m%n);
- }
- private static int gcd(int m, int n){
- int r;
- while(n != 0 ){ r = m%n ; m = n; n = r }
- return m;
- }