Tagged Questions
97
votes
8answers
3k views
Any idea why I need to cast an integer literal to (int) here?
In the following example
int i = -128;
Integer i2 = (Integer) i; // compiles
Integer i3 = (Integer) -128; /*** Doesn't compile ***/
Integer i4 = (Integer) (int) -128; // compiles
Integer i4 = -128; ...
90
votes
4answers
3k views
Weird “[]” after Java method signature
I looked at some Java code today, and I found some weird syntax:
public class Sample {
public int get()[] {
return new int[]{1, 2, 3};
}
}
I thought that can't compile and wanted to fix ...
58
votes
6answers
2k views
Strange array return type
Has any one seen the array [] placed after the method signature like this?
public static String mySplit(String s)[] {
return s.split(",");
}
public static void main(String... args) {
...
54
votes
3answers
3k views
What is the underscore actually doing in this Java code? [closed]
I just began to learn Java.
My friend who is helping me study just sent me this and said 'figure this out'.
Unfortunately I am unable to read this. It looks like Perl to me.
class _{_ ...
49
votes
5answers
7k views
Is it possible to make anonymous inner classes in Java static?
In Java, inner classes can be either static or not. If they are static, they do not contain a reference to the pointer of the containing instance (they are also not called inner classes anymore, they ...
45
votes
3answers
19k views
Null check in an enhanced for loop
What is the best way to guard against null in a for loop in Java?
This seems ugly :
if (someList != null) {
for (Object object : someList) {
// do whatever
}
}
Or
if (someList == ...
43
votes
15answers
47k views
Is there a goto statement in java?
I'm confused about this. Most of us have been told that there is no goto statement in Java.
But I found that it is one of the keyword in Java. Where can it be used? If it can not be used, then why was ...
34
votes
8answers
6k views
Is it OK to use == on enums in Java?
Is it OK to use == on enums in Java, or do I need to use .equals()? In my testing, == always works, but I'm not sure if I'm guaranteed of that. In particular, there is no .clone() method on an enum, ...
32
votes
2answers
19k views
Sqlite table constraint - unique on multiple columns
I can find syntax "charts" on this on the sqlite website, but no examples and my code is crashing. I have other tables with unique constraints on a single column, but I want to add a constraint to ...
31
votes
2answers
971 views
My java code has an obvious error. Why does it compile and run?
public class HelloWorld {
public static void main (String args[]){
System.out.println ("Hello ");
http://www.google.com
System.out.println ("World!");
}
}
The above ...
28
votes
8answers
19k views
In Java, can I define an integer constant in binary format?
Similar to how you can define an integer constant in hexadecimal or octal, can I do it in binary?
I admit this is a really easy (and stupid) question. My google searches are coming up empty.
27
votes
3answers
2k views
How does “object.new” work? (Does Java have a .new operator?)
I came across this code today whilst reading Accelerated GWT (Gupta) - page 151.
public static void getListOfBooks(String category, BookStore bookStore) {
serviceInstance.getBooks(category, ...
26
votes
10answers
10k views
What do curly braces by themselves mean in java?
for example, I have the following code (generated, not written)
if(node.getId() != null)
{
node.getId().apply(this);
}
{
List<PExp> copy = new ...
26
votes
4answers
497 views
Why is declaration of the variable required inside a for-each loop in java
The usual form the of for each loop is this:
for(Foo bar: bars){
bar.doThings();
}
But if I want to retain bar until after the loop, I can not use the for each loop:
Foo bar = null;
// - ...
22
votes
9answers
13k views
Closures in Java 7
I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer.
What would this syntax look like?
I read somewhere that ...