GVKun编程网logo

与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止(integer.valueof()和integer.parse()区别)

10

对于想了解与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止的读者,本文将是一篇不可错过的文章,我们将详细介绍integer.valueof

对于想了解与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止的读者,本文将是一篇不可错过的文章,我们将详细介绍integer.valueof()和integer.parse()区别,并且为您提供关于console.writeline和System.out.println、e.printStackTrace和System.out.println(e)之间的区别、Integer.MAX_VALUE >> 2的问题、Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理的有价值信息。

本文目录一览:

与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止(integer.valueof()和integer.parse()区别)

与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止(integer.valueof()和integer.parse()区别)

当我运行此类时,for循环似乎提前终止

class Test {    public static void main(String[] args) {        int result = 0;        int end = Integer.MAX_VALUE;        int i;        for (i = 1; i <= end; i += 2) {            System.out.println(i);        }        System.out.println("End:" + i);    }}

输出为:

135...3117331175End:31177

为什么到此结束?有趣的是,如果我System.out.println(i)在for循环中删除了,输出将是End:-2147483647。显然,ihas中的值wrappedround

我正在使用的Java版本是

Java(TM) SE Runtime Environment (build 1.6.0_16-b01)Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode)

答案1

小编典典

它是Java 6中的一个已知错误。JIT错误地优化了循环。我相信Java的最新版本没有此错误。

http://vanillajava.blogspot.co.uk/2011/05/when-jit-gets-it-
wrong.html

Java 6更新16已有两年多了。如果您无法更新到Java 7,建议您更新到最新版本的Java 6 update 25。

BTW Java 6将在几个月后终止免费支持(2012年12月)

console.writeline和System.out.println

console.writeline和System.out.println

console.writeline和之间的技术区别到底是什么System.out.println?我知道这会System.out.println写入标准输出,但这与控制台不同吗?

我不完全理解的文档进行console.writeline

答案1

小编典典

以下是使用System.out/
.err/.in和之间的主要区别System.console()

  • System.console()如果您的应用程序未在终端中运行,则返回null(尽管您可以在应用程序中处理此问题)
  • System.console() 提供读取密码而不回显字符的方法
  • System.outSystem.err使用默认平台编码,而Console类输出方法使用控制台编码

后一种行为可能不会立即显而易见,但是像这样的代码可以证明区别:

public class ConsoleDemo {  public static void main(String[] args) {    String[] data = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510",         "\u2502Hello\u2502",        "\u2514\u2500\u2500\u2500\u2500\u2500\u2518" };    for (String s : data) {      System.out.println(s);    }    for (String s : data) {      System.console().writer().println(s);    }  }}

在我的Windows XP(系统编码为Windows-1252,默认控制台编码为IBM850)上,该代码将写为:

????????Hello????????┌─────┐│Hello│└─────┘

请注意,此行为取决于将控制台编码设置为与系统编码不同的编码。由于多种历史原因,这是Windows上的默认行为。

e.printStackTrace和System.out.println(e)之间的区别

e.printStackTrace和System.out.println(e)之间的区别

可能是一个新手问题,但每个人似乎都在使用e.printStackTrace(),但System.out.println(e)在处理异常时我一直都在使用。有什么区别,为什么更e.printStackTrace()可取?

Integer.MAX_VALUE >> 2的问题

Integer.MAX_VALUE >> 2的问题

 @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  

        

int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  MeasureSpec.AT_MOST);  

        super.onMeasure(widthMeasureSpec, expandSpec);  

    }  

经常遇到这样的写法,一直没搞明白Integer.MAX_VALUE >> 2 就是所谓的尺寸,请求解释下哦,谢谢。

Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理

Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理

先上代码:

int a = Integer.MAX_VALUE;
int b = Integer.MIN_VALUE;		
System.out.println(a + a);  // -2
System.out.println(b + b);  // 0
System.out.println(a - b);  // -1 

为什么是这样的结果?参考这篇博客《原码, 反码, 补码 详解》(http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html)

这样就好理解了,假设Max值为:01111111 假设Min值为:11111111,有以下结论:

max:		min:
0 1111111	1 1111111	| 原码
0 1111111	1 0000000	| 补码

min + min:
0 0000000	| 补码
0 0000000	| 原码 = 0

max + max:
1 1111110	| 补码
1 0000010	| 原码 = -2


max + min:
1 1111111	| 补码
1 0000001	| 原码 = -1

 

关于与Integer.MAX_VALUE和使用System.out.println进行比较时,for循环会提前终止integer.valueof()和integer.parse()区别的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于console.writeline和System.out.println、e.printStackTrace和System.out.println(e)之间的区别、Integer.MAX_VALUE >> 2的问题、Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理的相关信息,请在本站寻找。

本文标签: