如何从URL或String中删除文件名?
String os = System.getProperty("os.name").toLowerCase();
String nativeDir = Game.class.getProtectionDomain().getCodeSource().getLocation().getFile().toString();
//Remove the <name>.jar from the string
if(nativeDir.endsWith(".jar"))
nativeDir = nativeDir.substring(0, nativeDir.lastIndexOf("/"));
//Load the right native files
for(File f : (new File(nativeDir + File.separator + "lib" + File.separator + "native")).listFiles()){
if(f.isDirectory() && os.contains(f.getName().toLowerCase())){
System.setProperty("org.lwjgl.librarypath", f.getAbsolutePath()); break;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的,它的工作原理.据我所知,因为我使用"/"它只适用于Windows.我想让它与平台无关
为什么Java引入了一些没有定义方法的接口?例如Cloneable,Serializable,Type等等.
第二件事:在Class.class包中有一个registerNatives()没有body 定义的方法,从静态块调用,但Class.class不是抽象的,但是final.为什么这样?为什么Java需要一些没有body的方法来从静态块中调用.
我正在指导同事OCA-Java 7认证.他也参加了一门课程并在那里做了准备考试.其中一个问题是关于参考和对象类型.这是代码:
package com.company;
public class Vehicle implements Mobile {
public static void main(String[] args) {
Truck theTruck = new Truck();
Vehicle theVehicle = theTruck;
Mobile theMobile = theVehicle;
}
}
class Truck extends Vehicle {
}
interface Mobile {
}
Run Code Online (Sandbox Code Playgroud)
问题:什么是引用类型和对象类型theMobile?
以下是选择:
答案B被标记为正确答案......但是恕我直言答案C是对的.谁在这里错了?!
考虑上课Foo.
public class Foo {
private double size;
public double getSize() {
return this.size; // Always O(1)
}
}
Run Code Online (Sandbox Code Playgroud)
Foo有一个名为size的属性,它经常被访问,但从未被给定的方法修改过.每当在任何方法中多次访问变量时,我总是在变量中缓存一个属性,因为"有人告诉过我"而没有多想.即
public void test(Foo foo) {
double size = foo.getSize(); // Cache it or not?
// size will be referenced in several places later on.
}
Run Code Online (Sandbox Code Playgroud)
这值得吗,还是矫枉过正?
如果我不缓存它,那么现代编译器是否足够智能以自行缓存它?
在jQuery或JavaScript中是否有与strstr()PHP 相同的功能?
我有一个AJAX响应应该是1,2,3,12,13,23或123.我想检查1是否存在,然后如果2存在则则存在3.
我在我的应用程序中有几个区域,我从实例方法操作静态变量的值时得到错误.
"从实例方法写入静态字段".
如果我们将多线程排除在等式之外,即使多个实例写入同一个static变量,这种情况是否会造成任何潜在的问题?
I am using Java for a while and come up with this problem: I use hard-coded paths in windows like
"D:\Java-code\JavaProjects\workspace\eypros\src"
Run Code Online (Sandbox Code Playgroud)
The problem is that I need to escape the backslash character in order to use it with string. So I manually escape each backslash:
"D:\\Java-code\\JavaProjects\\workspace\\eypros\\src"
Run Code Online (Sandbox Code Playgroud)
Is there a way to automatically take the unescaped path and return an escaped java string.
I am thinking that maybe another container besides java string could do the trick (but I don't know …
我不明白为什么Constructor call must be the first statement in a constructor如果我转移this(1);到构造函数中的最后一行,下面的代码显示错误.
package learn.basic.corejava;
public class A {
int x,y;
A()
{
// this(1);// ->> works fine if written here
System.out.println("1");
this(1); //Error: Constructor call must be the first statement in a constructor
}
A(int a)
{
System.out.println("2");
}
public static void main(String[] args) {
A obj1=new A(2);
}
}
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow上检查了很多关于这个主题的答案,但我仍然无法理解这个原因.请帮我用一些简单的例子和解释来说明这个错误.
ArrayUtils从apache commons 看这个类,该文档说:
ArrayUtils()
Run Code Online (Sandbox Code Playgroud)
不应在标准编程中构造ArrayUtils实例.
我正在查看这个类的源代码,我看到他们将构造函数公开:
public ArrayUtils() {
super();
}
Run Code Online (Sandbox Code Playgroud)
由于该类的所有方法/字段都是静态的,因此我理解创建此类的实例是没有意义的.
那么,为什么不自己做的构造private中类似Math类,以避免新的实例的创建?
java ×9
constructor ×2
interface ×2
static ×2
string ×2
backslash ×1
caching ×1
class ×1
coding-style ×1
escaping ×1
filepath ×1
findbugs ×1
javascript ×1
jquery ×1
object-type ×1
strstr ×1
this ×1