小编DnR*_*DnR的帖子

Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128?

I have no idea why these lines of code return different values:

System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));
Run Code Online (Sandbox Code Playgroud)

The output is:

true
false
true
Run Code Online (Sandbox Code Playgroud)

Why does the first one return true and the second one return false? Is there something different that I don't know between 127 and 128? (Of course I know that 127 < 128.)

Also, why does the third one return true?

I have read the answer of this question, but I still didn't get …

java comparison integer

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

为什么Integer类缓存值在-128到127之间?

关于我之前的问题,为什么==与Integer.valueOf(String)的比较给出了127和128的不同结果?,我们知道Integer class有一个缓存存储-128和之间的值127.

只是想知道,为什么介于-128和127之间

Integer.valueOf()文档声明它" 缓存经常请求的值 ".但是-128,127经常要求真实的价值吗?我认为经常要求的价值观是非常主观的.
这背后有什么可能的原因吗?

从文档中还说:" ..并且可以缓存此范围之外的其他值. "
如何实现这一目标?

java caching

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

无法复制我的对象并更改值

我在制作要使用的对象的副本并更改该副本的值时遇到问题,而是更改了我的两个对象的值.对象的代码.

public class Board {
    private int[][] board;

    public Board() {
        board = new int[9][9];
    }
    public Board(int[][] layout){
        board = layout;
    }

    public int[][] getBoard(){
        return board;
    }
    public int getBoardValue(int y, int x){
        return board[y][x];
    }
    public void insertValue(int v, int y, int x){
        board[y][x] =v;
    }
}
Run Code Online (Sandbox Code Playgroud)

以及我一直试图开始工作的函数的代码

public Board copy(Board b) {
    Node node = new Node(b);
    int[][] layout = node.getBoard().getBoard();
    Board temp = new Board(layout);
    temp.insertValue(1,4,5);
    return temp;
}
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试在新对象中插入值1时,旧对象仍会更改.

java object

6
推荐指数
1
解决办法
84
查看次数

"line.separator"的实际价值是多少?

我一直认为line.separator做同样的事情\n,是的.但是当我测试这段代码时,我注意到这两个值实际上并不相同(可能甚至不接近?):

String str1 = System.getProperty("line.separator");
String str2 = "\n";

System.out.println(str1.equals(str2)); // output:false (as expected)
Run Code Online (Sandbox Code Playgroud)

然后我检查了两者的长度:

System.out.println(str1.length());     //output: 2
System.out.println(str2.length());     //output: 1
Run Code Online (Sandbox Code Playgroud)

想知道为什么str1.length()2的,我想这:

System.out.println("**"+str1.charAt(0)+"**");
Run Code Online (Sandbox Code Playgroud)

输出:

**

System.out.println("##"+str1.charAt(1)+"##");
Run Code Online (Sandbox Code Playgroud)

输出:

##
##
Run Code Online (Sandbox Code Playgroud)

所以,我注意到,实际换行符line.separator是第二个字符.那么第一个索引的值是什么,就像它应该打印****(至少),它的打印**相反?

java string char

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

VB.NET中的整数值舍入

我的代码如下:

Dim num1 As Integer
Dim num2 As Integer

num1 = 12.5
num2 = 17.5
Run Code Online (Sandbox Code Playgroud)

当我输出值时,我得到这个:

num1: 12
num2: 18
Run Code Online (Sandbox Code Playgroud)

如果12.5成为12,17.5应该是17

或者如果17.5成为18,12.5应该是13

我是Visual Basic的新手,很难找到任何参考.

vb.net

4
推荐指数
1
解决办法
275
查看次数

使用输入maxlength的限制是否足够?

maxlength仅通过设置来限制用户输入值是否足够?让我说我有这个代码:

    <input type="text" id="foo" maxlength="12">
Run Code Online (Sandbox Code Playgroud)

用户是否仍然可以(以任何有效或无效的方式)插入值超过12

当我们设置时maxlength,使用javascript再次验证它还是在后端(servlet等)是有用还是无用

html javascript validation

3
推荐指数
1
解决办法
361
查看次数

String#split方法混淆

我试过这段代码:

String str =",,,,,";
String str2=",,,,, ";
System.out.println(str.split(",").length);
System.out.println(str2.split(",").length);
Run Code Online (Sandbox Code Playgroud)

输出是:

0
6
Run Code Online (Sandbox Code Playgroud)

唯一的区别是str价值是,,,,,(没有 空间)和str2价值是,,,,,( 空间)

有人可以解释一下吗?

java string split

3
推荐指数
1
解决办法
69
查看次数

为什么getNamespaceURI()总是返回null?

为什么getNamespaceURI()总是返回null?printNSInfo方法有什么问题

public static void main(String[] args) {
    Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(args[0]);
    Element root = input.getDocumentElement();
    printNSInfo(root);
}

private static void printNSInfo(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getNamespaceURI() != null) {
            System.out.println("Element Name:" + node.getNodeName());
            System.out.println("Local Name:" + node.getLocalName());
            System.out.println("Namespace Prefix:" + node.getPrefix());
            System.out.println("Namespace Uri:" + node.getNamespaceURI());
            System.out.println("---------------");
        }
        if (node.hasAttributes()) {
            NamedNodeMap map = node.getAttributes();
            int len = map.getLength();
            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);
                if (attr.getNamespaceURI() != null) …
Run Code Online (Sandbox Code Playgroud)

java dom namespaces

3
推荐指数
1
解决办法
2163
查看次数

标签 统计

java ×6

string ×2

caching ×1

char ×1

comparison ×1

dom ×1

html ×1

integer ×1

javascript ×1

namespaces ×1

object ×1

split ×1

validation ×1

vb.net ×1