關於“*”的問題

關於“*”的問題:
先來看看一段代碼
class Operatorer
{
 
   float multiplication(float i,float j){
  return i*j;
}
public static void main(String[] args) {
   float k = Float.parseFloat(args[0]);
   float l = Float.parseFloat(args[2]);//將字符串轉換爲float
   //char d = '*';
   Operatorer o = new Operatorer();
   if (args[1].equals("*"))// 這裏除了用“*”都不會有問題出現
  {
     p.rintln(args[0]+"乘以"+args[2]+"得"+o.multiplication(k,l));
  }
}
+----------------------+
輸入3 * 3
(注意了,錯誤發生):
+--------output--------+
Exception in thread "main" java.lang.NumberFormatException: For input string: "A2.class"
        at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
        at java.lang.Float.parseFloat(Unknown Source)
        at Operatorer.main(Operatorer.java:19)
+-=A2.class,是我當前目錄中的第一個文件(每次都是當前目錄中的第一個文件)-=+
輸入3 “*” 3
+--------output--------+
3乘以3得9.0
+-----------------------+
爲什麼會出現這種情況呢,開始我也是百思不得其解,最後在一個國外的論壇裏看見了類似的討論:
也是由一段代碼開始的討論:
public class MyTest
{
public static void main(String[] args)
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args[ i ]);
}
}
}
+-------------------------+
javac MyTest *
+-----output--------------+
---------- output(parameter) ----------
Asprint.class
Basefile.class
Changevalue.class
Changevalue.java
E19_BlankFinal.class
E19_BlankFinal.java
getSystemProperties.class
getSystemProperties.java
MyTest .java.bak
MyTest.class
MyTest.java
MyTest.java.bak
ok.class
ok.java
輸出完成 (耗時 0 秒) - 正常終止
+--------------------------------------+
上面列出了我當前目錄的所有文件(爲什麼呢?)
+--------------------------------------+
如果改成javac MyTest “*”則如你所期望的輸出*,但是這是爲什麼呢?
有人這樣說:“Your command interpreter or shell is pre-interpreting
the asterisk symbol in certain contexts/expressions.”and“ It is my windows
command interpreter pre-interpreted the asterisk symbol",對於這個問題我做了個簡單的
實驗:
+——-----------------------------------+
在命令行下敲入:javac *.class(or *); 結果如下:
javac: invalid flag: AAA.class //this is my first class file in current directory
Usage: javac
where possible options include:
  -g                        Generate all debugging info
  -g:none                   Generate no debugging info
  -g:{lines,vars,source}    Generate only some debugging info
  -nowarn                   Generate no warnings
  -verbose                  Output messages about what the compiler is doing
  -deprecation              Output source locations where deprecated APIs are used
  -classpath          Specify where to find user class files
  -sourcepath         Specify where to find input source files
  -bootclasspath      Override location of bootstrap class files
  -extdirs            Override location of installed extensions
  -d             Specify where to place generated class files
  -encoding       Specify character encoding used by source files
  -source          Provide source compatibility with specified release
  -target          Generate class files for specific VM version
  -help                     Print a synopsis of standard options
+----------------------------------------------+
當我的當前目錄裏面沒有class文件而只有java文件時,它將編譯所有當前目錄下的java文件
---------------------------------------------------
我的看法就是:在這裏,“*”在windows中代表通配符,當你輸入*號時,他就在你的當前目錄下尋找它所能
代表的文件,從上面就可以看出他是可以代表所有文件的,所以當我在第一個程序執行時輸入“*”,系統就
先把它當成通配符處理了,看看拋出的錯誤中有這麼一句For input string: "A2.class",就是將A2.class
當成Srtring傳入了程序中,但是當應用試圖把一個字符串轉換成一個數值類型,這個字符串沒有恰當的格式,
就拋出java.lang.NumberFormatException。如果你一定要用到*,又不想利用他的通配符的作用,那就請一定給
他加上雙引號。文章中的第二個程序由於列舉所有當前目錄下的文件,實際上就等同於命令行下的dir了,不是嗎?
(但是爲什麼總是先查找class文件,就不知道了,有知道的人給個答案)
發佈了23 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章