I'm trying to figure out why this code isn't compiling correctly. When I try to compile, I get the error that it needs a main, when I add the main, it leads to a ton more errors that I'm honestly not sure how to fix. Can someone take a look at the code and help me out? If anyone could help it would be greatly appreciated. public class Polygon { private int numSides; //number of s
我想弄清楚为什么这段代码没有正确编译。 当我尝试编译时,我得到它需要一个主要的错误,当我添加main时,它会导致更多的错误,我真的不知道如何解决。 有人可以看看代码并帮助我吗? 如果有人可以帮助它,将不胜感激。 public class Polygon { private int numSides; //number of sides private double sideLength; //length of each side private double xCoord; //x-coordinate of th center of the polygon private doub
I have a char and I need a String . How do I convert from one to the other? You can use Character.toString(char) . Note that this method simply returns a call to String.valueOf(char) , which also works. As others have noted, string concatenation works as a shortcut as well: String s = "" + 's'; But this compiles down to: String s = new StringBuilder().append("").append('s').toString();
我有一个char ,我需要一个String 。 我如何从一个转换到另一个? 你可以使用Character.toString(char) 。 请注意,此方法只是返回一个对String.valueOf(char)的调用,该String.valueOf(char)也可以工作。 正如其他人所指出的那样,字符串连接也是一种捷径: String s = "" + 's'; 但是这可以归结为: String s = new StringBuilder().append("").append('s').toString(); 这是因为StringBuilder由char[] (通过String
This question already has an answer here: Possibly lossy conversion from long to int 2 answers "Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error." --- Wikipedia https://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays You cannot initialize an array with a long size. You can only implement
这个问题在这里已经有了答案: 从long到int 2的答案可能有损转换 “数组必须通过int值进行索引...尝试访问具有长索引值的数组组件时,会导致编译时错误。” ---维基百科https://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays 您无法初始化长尺寸的数组。 您只能使用满足以下条件的整数大小n来实现一个数组: 0 <= n <= Integer.MAX_VALUE 请参阅Java SE规范> Array Access。 所有数组都是0来源的。
I have this program that is pretty much a calculator but with a moving JLabel that is supposed to change colors every time you click the label, but i have 3 errors at the very bottom of the code that i have marked with a comment. all three are: error: incompatible types: possible lossy conversion from long to int public class TestCalculator { private ResultPane resultPane; public static void
我有这个程序几乎是一个计算器,但有一个移动的JLabel,它应该在每次点击标签时都会改变颜色,但是我在代码的最底部有3个错误,我标注了一条评论。 所有三个都是:错误:不兼容的类型:从long到int的可能有损转换 public class TestCalculator { private ResultPane resultPane; public static void main(String[] args) { new TestCalculator(); } public TestCalculator() { EventQueue.invokeLater(new Runnable(
The diamond operator in java 7 allows code like the following: List<String> list = new LinkedList<>(); However in Java 5/6, I can simply write: List<String> list = new LinkedList(); My understanding of type erasure is that these are exactly the same. (The generic gets removed at runtime anyway). Why bother with the diamond at all? What new functionality / type safety doe
Java 7中的钻石操作符允许如下代码: List<String> list = new LinkedList<>(); 但是在Java 5/6中,我可以简单地写出: List<String> list = new LinkedList(); 我对类型擦除的理解是这些完全相同。 (无论如何,该通用在运行时被删除)。 为什么要钻石呢? 它允许什么新的功能/类型安全? 如果它没有产生任何新的功能,为什么他们提到它作为一个功能? 我对这个概念的理解是否有缺陷? 与问题 Li
Why doesn't Java include support for unsigned integers? It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input. Furthermore, using unsigned integers can be a form of self-documentation, since they indicate that the value which the unsigned int was intended to hold is never supposed to be negative.
为什么Java不包含对无符号整数的支持? 在我看来,这是一个奇怪的遗漏,因为它们允许编写不太可能在意外大的输入上产生溢出的代码。 此外,使用无符号整数可以是一种自我记录的形式,因为它们表明无符号整数打算保存的值从不应该是负数。 最后,在某些情况下,无符号整数可以更有效地执行某些操作,例如除法。 包括这些的缺点是什么? 这来自对高斯林和其他人的采访,关于简单性: Gosling:对于我来说,作为一名语
In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error? That's because b += 1.0; is equivalent to b = (int) ((b) + (1.0)); . The narrowing primitive conversion (JLS 5.1.3) is hidden in the compound assignment operation. JLS 15.26.2 Compound Assignment Operators (JLS Third
在Java中,当你这样做时 int b = 0; b = b + 1.0; 您可能会失去精度错误。 但是,如果你这样做,为什么呢? int b = 0; b += 1.0; 没有任何错误? 那是因为b += 1.0; 相当于b = (int) ((b) + (1.0)); 。 缩小基元转换(JLS 5.1.3)隐藏在复合赋值操作中。 JLS 15.26.2复合赋值操作符(JLS第三版): E1 op = E2形式的复合赋值表达式等价于E1 =(T)((E1)op(E2)),其中T是E1的类型,只是E1只计算一次。 例
What I actually meant to ask is, if I write : byte b=13; short s=14; Then values on the right-hand side ie, 13 and 14 respectively are treated as byte and short Or is just read as an int type by the compiler. If they are read as bytes and shorts respectively then why, byte b1=13; byte b2=23; byte b3=b1+b2; And short s1=14; short s2=24; short s3=s1+s2; are invalid statements(short s3=s1+s2;
我真正要问的是,如果我写: byte b=13; short s=14; 然后,右侧的值分别为13和14分别被视为字节和短整型或由编译器将其作为int类型读取。 如果他们分别作为字节和短裤阅读,那么为什么, byte b1=13; byte b2=23; byte b3=b1+b2; 和 short s1=14; short s2=24; short s3=s1+s2; 在java中是无效的语句(短s3 = s1 + s2;和字节b3 = b1 + b2;),即使精度在这种情况下也不会受到影响。 如果它们只是被编译器读为int类型,
I'm working on a project where all conversions from int to String are done like this: int i = 5; String strI = "" + i; I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose? Normal ways would be Integer.toString(i) or String.valueOf(i) . The concatenation will work, but it is unconventional and could be a bad smell as it suggests the author doesn
我正在开发一个项目,其中从int到String所有转换都是这样完成的: int i = 5; String strI = "" + i; 我不熟悉Java。 正如我所想的那样,这是通常的做法还是错误的? 正常的方式是Integer.toString(i)或String.valueOf(i) 。 级联会起作用,但它不符合常规,可能是一种难闻的气味,因为它表明作者不知道上述两种方法(他们还不知道什么?)。 与字符串一起使用时,Java对+操作符有特殊的支持(请参阅文档),它将您发布
This could be the dumbest question ever asked but I think it is a total confusion for a newbie. Can somebody clarify what is meant by immutable? Why is a String immutable? What are the advantages/disadvantages of immutable objects? Why should a mutable object such as StringBuilder be preferred over String and vice-verse? A nice example (in Java) will be really appreciated. Immutable m
这可能是有史以来最愚蠢的问题,但我认为对于新手来说这是一个完全混淆的问题。 有人可以澄清不变的含义吗? 为什么String不可变? 不可变对象有什么优点/缺点? 为什么像StringBuilder这样的可变对象比String和副诗更受欢迎? 一个很好的例子(在Java中)将非常感激。 不可变意味着一旦对象的构造函数完成执行,那个实例就不能被修改。 这很有用,因为它意味着你可以传递对象的引用,而不用担心别人会改变它的内