关于“*”的问题:
先来看看一段代码
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文件,就不知道了,有知道的人给个答案)
先来看看一段代码
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
-sourcepath
-bootclasspath
-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文件,就不知道了,有知道的人给个答案)