小编Not*_*bug的帖子

从java中的URL/Path中删除文件名

如何从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 string filepath platform-independent

15
推荐指数
5
解决办法
4万
查看次数

没有方法的接口

为什么Java引入了一些没有定义方法的接口?例如Cloneable,Serializable,Type等等.

第二件事:在Class.class包中有一个registerNatives()没有body 定义的方法,从静态块调用,但Class.class不是抽象的,但是final.为什么这样?为什么Java需要一些没有body的方法来从静态块中调用.

java java-native-interface interface class

14
推荐指数
2
解决办法
2万
查看次数

引用类型和对象类型

我正在指导同事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参考类型为"Truck",对象类型为"Truck"
  • C引用类型为"Mobile",对象类型为"Truck"
  • D参考类型为"Car",对象类型为"Mobile"

答案B被标记为正确答案......但是恕我直言答案C是对的.谁在这里错了?!

java reference-type object-type

13
推荐指数
2
解决办法
1148
查看次数

您是否在本地变量中缓存属性?

考虑上课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)

这值得吗,还是矫枉过正?

如果我不缓存它,那么现代编译器是否足够智能以自行缓存它?

java caching coding-style

12
推荐指数
2
解决办法
1379
查看次数

接口中的变量

为什么接口中使用的变量是PUBLIC STATIC FINAL?为什么特别是"静态"?

java static interface

11
推荐指数
2
解决办法
4万
查看次数

JavaScript或jQuery相当于PHP的strstr()函数

在jQuery或JavaScript中是否有与strstr()PHP 相同的功能?

我有一个AJAX响应应该是1,2,3,12,13,23或123.我想检查1是否存在,然后如果2存在则则存在3.

javascript jquery strstr

10
推荐指数
2
解决办法
3万
查看次数

FindBugs错误:从实例方法写入静态字段

我在我的应用程序中有几个区域,我从实例方法操作静态变量的值时得到错误.

"从实例方法写入静态字段".

如果我们将多线程排除在等式之外,即使多个实例写入同一个static变量,这种情况是否会造成任何潜在的问题?

java static code-analysis findbugs

8
推荐指数
1
解决办法
2万
查看次数

escaping backslash in java string literal

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 …

java string escaping backslash

8
推荐指数
1
解决办法
5万
查看次数

构造函数调用必须是构造函数中的第一个语句

我不明白为什么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上检查了很多关于这个主题的答案,但我仍然无法理解这个原因.请帮我用一些简单的例子和​​解释来说明这个错误.

java constructor this

7
推荐指数
1
解决办法
2万
查看次数

为什么不私有没有args构造函数?

ArrayUtils从apache commons 看这个类,该文档说:

ArrayUtils() 
Run Code Online (Sandbox Code Playgroud)

不应在标准编程中构造ArrayUtils实例.

我正在查看这个类的源代码,我看到他们将构造函数公开:

public ArrayUtils() {
   super();
}
Run Code Online (Sandbox Code Playgroud)

由于该类的所有方法/字段都是静态的,因此我理解创建此类的实例是没有意义的.

那么,为什么不自己做的构造private中类似Math类,以避免新的实例的创建?

java constructor

7
推荐指数
1
解决办法
280
查看次数