我想得到方法的调用者类,即
class foo{
bar();
}
Run Code Online (Sandbox Code Playgroud)
在方法栏中,我需要获取类名foo
,我找到了这个方法:
Class clazz = sun.reflect.Reflection.getCallerClass(1);
Run Code Online (Sandbox Code Playgroud)
但是,即使getCallerClass
是public
,当我试着调用它时,Eclipse说:
访问限制:由于对所需库C:\ Program Files\Java\jre7\lib\rt.jar的限制,无法访问Reflection类型的方法getCallerClass()
还有其他选择吗?
public class Strange1 {
public static void main(String[] args) {
try {
Missing m = new Missing();
} catch (java.lang.NoClassDefFoundError ex) {
System.out.println("Got it!");
}
}
}
public class Strange2 {
public static void main(String[] args) {
Missing m;
try {
m = new Missing();
} catch (java.lang.NoClassDefFoundError ex) {
System.out.println("Got it!");
}
}
}
class Missing {
Missing() { }
}
Run Code Online (Sandbox Code Playgroud)
如果您在删除后运行Strange1和Strange2 Missing.class
,Strange1将抛出,NoClassDefFoundError;
但Strange2将打印得到它!
有谁能解释一下?谢谢.
更新:
java字节码Strange1
:
0 new info.liuxuan.test.Missing [16]
3 dup
4 invokespecial …
Run Code Online (Sandbox Code Playgroud) 我的代码如下:
DatabaseMetaData dmd = connection.getMetaData();
ResultSet rs = dmd.getPrimaryKeys(null, null, tableName);
while(rs.next()){
primaryKey = rs.getString("COLUMN_NAME");
}
Run Code Online (Sandbox Code Playgroud)
rs rs.next()
总是返回时不是null false
,任何人都有这个想法吗?谢谢.
我从"java concurrency in practice"中获得了以下代码:
public class Holder{
private int n;
public Holder(int n){this.n = n;}
public void assertSanity(){
if(n != n) throw new AssertionError("This statement is false.");
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道这种情况n != n
,在某种情况下这可能是真的吗?
我正在查看Java中的跳过列表实现,我想知道以下方法的目的:
public static int randomLevel() {
int lvl = (int)(Math.log(1.-Math.random())/Math.log(1.-P));
return Math.min(lvl, MAX_LEVEL);
}
Run Code Online (Sandbox Code Playgroud)
和上面的方法有什么区别
Random.nextInt(6);
Run Code Online (Sandbox Code Playgroud)
谁能解释一下?谢谢。
test.c:
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(){
lua_State *L = luaL_newState(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
luaL_dofile(L,"test.lua"); /* runs Lua scrip */
lua_close(L);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
建立:
gcc -o test test.c -I/usr/local/Cellar/lua/5.1.5/include
Run Code Online (Sandbox Code Playgroud)
然后我得到了错误:
test.c:7:18: warning: implicit declaration of function 'luaL_newState' is
invalid in C99 [-Wimplicit-function-declaration]
lua_State *L = luaL_newState(); /* opens Lua */
^
test.c:7:14: warning: incompatible integer to pointer conversion initializing
'lua_State *' (aka 'struct lua_State *') with an expression of …
Run Code Online (Sandbox Code Playgroud) public static void main(String[] args){
System.out.println(1.-5); // -4.0
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么我得到了上述结果,在此先感谢.
看下面的代码:
public class Foo {
private static Foo sigleton=new Foo();
private static int count1;
private static int count2=0;
private Foo (){
count1++;
count2++;
}
public static Foo getInstance(){
return sigleton;
}
public static void main(String[] args) {
//Foo f= Foo.getInstance(); // case 1
//Foo f= new Foo(); // case 2
System.out.println(f.count1);
System.out.println(f.count2);
}
}
Run Code Online (Sandbox Code Playgroud)
对于每次运行,取消注释main方法中的一行.
为什么案例1和2的输出不同?
在我的机器中,我得到以下结果:
sizeof(long) = 8
sizeof(long int) = 8
Run Code Online (Sandbox Code Playgroud)
在哪里使用long int
,为什么不使用int
?