由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - java main的疑问
相关主题
这段代码有什么问题?C++ interview programming exercise
写惯了C++ code,再写C code真不习惯find bugs of c++ codes
不算小的一个c++项目的code可以用什么工具自动生成吗大侠们救命, C++ operator new 问题
Nested classes inside one class (C++)码工试题 (转载)
ask a C++ inheritance questionQuestion about name resolution in C++
三个C syntax 弱问题有没有办法让一个类的变量只读,不是const?
C++ interview questions help问一个C++的问题
经典C++问题求助问个C++ 编译器临时变量的问题 (转载)
相关话题的讨论汇总
话题: java话题: main话题: foo话题: method话题: string
进入Programming版参与讨论
1 (共1页)
v****s
发帖数: 1112
1
不知道为啥java设计者要把入口函数main封装在一个class里面?总是感觉怪怪的。。
。。。
X****r
发帖数: 3557
2
There is no global function in Java, so main has to be a static method.
The extra benefit is that you can have multiple mains -- you are able
to specify which is your main class without recompiling.

【在 v****s 的大作中提到】
: 不知道为啥java设计者要把入口函数main封装在一个class里面?总是感觉怪怪的。。
: 。。。

g*****g
发帖数: 34805
3
main is just a static function, static functions are like
global functions with class scope and access control.
Give you a lot of advantages over a global scope like C++

【在 v****s 的大作中提到】
: 不知道为啥java设计者要把入口函数main封装在一个class里面?总是感觉怪怪的。。
: 。。。

v****s
发帖数: 1112
4
thanks.
but, if you have lots of java files, how can u tell which one has the entry
point main function?? it's not trivial to identify, right? i mean, compared
to C/Cpp which can write a main() outside of all classes.

【在 g*****g 的大作中提到】
: main is just a static function, static functions are like
: global functions with class scope and access control.
: Give you a lot of advantages over a global scope like C++

d****p
发帖数: 685
5
I think xantar has answered your question?
When starting JVM you specify an entry class to load whose main will be
called, like javac Foo where class Foo's main() is executed.

entry
compared

【在 v****s 的大作中提到】
: thanks.
: but, if you have lots of java files, how can u tell which one has the entry
: point main function?? it's not trivial to identify, right? i mean, compared
: to C/Cpp which can write a main() outside of all classes.

v****s
发帖数: 1112
6
sorry i didn't state my question clearly.
like in cpp, you always maintain only 1 main function, right? but in java,
main function is hidden in some certain java file, and is there any way to
retrieve it?

【在 d****p 的大作中提到】
: I think xantar has answered your question?
: When starting JVM you specify an entry class to load whose main will be
: called, like javac Foo where class Foo's main() is executed.
:
: entry
: compared

r****t
发帖数: 10904
7
What do you mean by "retrieve"?

java,
to

【在 v****s 的大作中提到】
: sorry i didn't state my question clearly.
: like in cpp, you always maintain only 1 main function, right? but in java,
: main function is hidden in some certain java file, and is there any way to
: retrieve it?

d****p
发帖数: 685
8
I kind of didn't get you point. What do you mean by "retrieve it"?
Looking again at my previous post, I am amused by "javac Foo" :-)
If you have a bunch of classes and you want to get all declared mains, you
can use reflect against these
classes.

【在 v****s 的大作中提到】
: sorry i didn't state my question clearly.
: like in cpp, you always maintain only 1 main function, right? but in java,
: main function is hidden in some certain java file, and is there any way to
: retrieve it?

g*****g
发帖数: 34805
9
You can have as many main methods as you like, although you can have
only one in each class.
Any java-based software has a script which has something like
java YourMainClass
It's not difficult to find out. And any modern editor can help you
find out all main methods in a package if you really need it.

【在 v****s 的大作中提到】
: sorry i didn't state my question clearly.
: like in cpp, you always maintain only 1 main function, right? but in java,
: main function is hidden in some certain java file, and is there any way to
: retrieve it?

v****s
发帖数: 1112
10
oh ic... thanks guys!

【在 g*****g 的大作中提到】
: You can have as many main methods as you like, although you can have
: only one in each class.
: Any java-based software has a script which has something like
: java YourMainClass
: It's not difficult to find out. And any modern editor can help you
: find out all main methods in a package if you really need it.

相关主题
三个C syntax 弱问题C++ interview programming exercise
C++ interview questions helpfind bugs of c++ codes
经典C++问题求助大侠们救命, C++ operator new 问题
进入Programming版参与讨论
v****s
发帖数: 1112
11
oh ic... thanks guys!

【在 g*****g 的大作中提到】
: You can have as many main methods as you like, although you can have
: only one in each class.
: Any java-based software has a script which has something like
: java YourMainClass
: It's not difficult to find out. And any modern editor can help you
: find out all main methods in a package if you really need it.

s******n
发帖数: 876
12
my question is why main() is special in java. why can't java command line
launcher start with any static method, with any literal arguments.
>java com.example.Foo.bar("abc", 42)

【在 v****s 的大作中提到】
: 不知道为啥java设计者要把入口函数main封装在一个class里面?总是感觉怪怪的。。
: 。。。

X****r
发帖数: 3557
13
1. The OS only sends a list of strings as arguments when starting an
application. It should be the application job on how they should
be interpreted, not the VM or the class loader. So it is only
natural for entry method to have single parameter of String[].
You're free to use additional libraries to help you parse the
command line, e.g. JArgs.
In fact, the syntax you have suggested is not suitable for an
application. Most applications only use positional parameters
for p

【在 s******n 的大作中提到】
: my question is why main() is special in java. why can't java command line
: launcher start with any static method, with any literal arguments.
: >java com.example.Foo.bar("abc", 42)

s******n
发帖数: 876
14
how is that a limitation? let me change the syntax a little bit to fit the
unix cmd line convention:
>java com.example.Foo.bar abc 42
Let's put in some realistic names instead of foobars
> java net.sf.Subversion.update -r 1.5
class Subversion
public static update(String... args)
Are multiple entries useful? Usually not for end users; but we never ask end
users to invoke java like that, a shell wrapper is provided.
For people who do use java cmd line this way, they are all developers
g*****g
发帖数: 34805
15
It's not that java can't do this, but this added flexibility
doesn't do anything current java isn't capable of doing. It
only makes more complex.

end

【在 s******n 的大作中提到】
: how is that a limitation? let me change the syntax a little bit to fit the
: unix cmd line convention:
: >java com.example.Foo.bar abc 42
: Let's put in some realistic names instead of foobars
: > java net.sf.Subversion.update -r 1.5
: class Subversion
: public static update(String... args)
: Are multiple entries useful? Usually not for end users; but we never ask end
: users to invoke java like that, a shell wrapper is provided.
: For people who do use java cmd line this way, they are all developers

X****r
发帖数: 3557
16
Okay, in your new example, how would java know whether 42 is a number
or a string?
Unnecessary flexibility is a bad thing. What you described can be
easily implement in a library when needed, for example, some testing
framework allow you to specify classes and methods to run from
the command line. There is no point to put this into the core language.

the
ask end
and

【在 s******n 的大作中提到】
: how is that a limitation? let me change the syntax a little bit to fit the
: unix cmd line convention:
: >java com.example.Foo.bar abc 42
: Let's put in some realistic names instead of foobars
: > java net.sf.Subversion.update -r 1.5
: class Subversion
: public static update(String... args)
: Are multiple entries useful? Usually not for end users; but we never ask end
: users to invoke java like that, a shell wrapper is provided.
: For people who do use java cmd line this way, they are all developers

s******n
发帖数: 876
17

good point, it must be resolved.
a cmd line argument can always be interpreted as a String literal. it may
also be parsed into a primitive literal: int/long, float/double, char, true/
false/null - if that succeeds, there is no ambiguity in which primitive type
it ends up with.
Now we need to resolve method overloading, in the same way a compiler does
it(JLS 15.12.2.5), only in our purpose, a primitive literal is a "subtype"
of a string literal.
This won't confuse a programmer who's not confused

【在 X****r 的大作中提到】
: Okay, in your new example, how would java know whether 42 is a number
: or a string?
: Unnecessary flexibility is a bad thing. What you described can be
: easily implement in a library when needed, for example, some testing
: framework allow you to specify classes and methods to run from
: the command line. There is no point to put this into the core language.
:
: the
: ask end
: and

d****p
发帖数: 685
18
So your point is make EACH java method callable from command line?
This is not doable.
Suppose you have a method
void foo(Foo f) where Foo is an object.
How can you provide argument f for invoking the method in command line? And
since you could not
provide it, why expose this method to command line for unnecessary fuss?
And your concept is really bad for performance: it needs the java runtime to
inteprete string in argument
list to suitable types for function lookup and value conversion and this

【在 s******n 的大作中提到】
:
: good point, it must be resolved.
: a cmd line argument can always be interpreted as a String literal. it may
: also be parsed into a primitive literal: int/long, float/double, char, true/
: false/null - if that succeeds, there is no ambiguity in which primitive type
: it ends up with.
: Now we need to resolve method overloading, in the same way a compiler does
: it(JLS 15.12.2.5), only in our purpose, a primitive literal is a "subtype"
: of a string literal.
: This won't confuse a programmer who's not confused

d*********8
发帖数: 2192
19
好多C++的库也是这么搞的,结构很清楚,没啥复杂的呀?
1 (共1页)
进入Programming版参与讨论
相关主题
问个C++ 编译器临时变量的问题 (转载)ask a C++ inheritance question
What does the default constructor do?三个C syntax 弱问题
c++中的inline 函数是做什么的?C++ interview questions help
请教struct inside class的问题(C++)经典C++问题求助
这段代码有什么问题?C++ interview programming exercise
写惯了C++ code,再写C code真不习惯find bugs of c++ codes
不算小的一个c++项目的code可以用什么工具自动生成吗大侠们救命, C++ operator new 问题
Nested classes inside one class (C++)码工试题 (转载)
相关话题的讨论汇总
话题: java话题: main话题: foo话题: method话题: string