Java = operator
Integer n = 5; System.out.println(n) // 5! How can i reproduce this behavior in my classes? ...
Integer n = 5; System.out.println(n) // 5! How can i reproduce this behavior in my classes? ...
I have a function that returns an id number if the argument exists in the database. If not, it returns null. Is this begging for a null pointer exception? Negative id numbers are not permitted, but I thought it would be clearer to have non-existent arguments returning null instead of an error code like -1. What do you think? private Int...
Why second code is faster? Map<Integer,Double> map=new HashMap<Integer,Double>(); for(int i=0;i<50000;i++) for(double j=0.;j<10000;j++){ map.put(i,j); } Map<Integer,Double> map=new HashMap<Integer,Double>(); for(int i=0;i<50000;i++) for(double j=0.;j<10000;j++){ map.put(new Integer(i),ne...
Hi All, I was doing one of these online Java tests and I was asked this question: Q: Indicate correct assignment: Long l = 1; Double d = 1; Integer i = 1; String s = 1; Object o = "1"; System.out.println(o); o = 1; System.out.println(o); Please try it yourself before you go any further. Well I can tell you I got it wrong, I inves...
How can we new primitive types in JNI. I have a function that returns a jobject. It is possible to return jint, jchar, etc. There is NewString, why not NewInteger, NewCharacter, NewDouble, etc. There is no autoboxing at JNI layer at the moment. I can go with the NewObject call, but this will be too much overhead to create primitive t...
Autoboxing is rather scary. While I fully understand the difference between == and .equals I can't but help have the follow bug the hell out of me: final List<Integer> foo = Arrays.asList(1, 1000); final List<Integer> bar = Arrays.asList(1, 1000); System.out.println(foo.get(0) == bar.get(0)); System.out.println(foo.get(...
Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles: byte b = 0; ++b; b++; --b; b--; b += b -= b *= b /= b %= b; b <<= b >>= b >>>= b; b |= b &= b ^= b; And thanks to auto-boxing and auto-unboxing, the following also compiles: Integer ii = 0; ++ii; ii++; --ii; ii--; ii += i...
Consider the following snippet: int i = 99999999; byte b = 99; short s = 9999; Integer ii = Integer.valueOf(9); // should be within cache System.out.println(new Integer(i) == i); // "true" System.out.println(new Integer(b) == b); // "true" System.out.println(new Integer(s) == s); // "true" System.out.pri...
Hi, Can someone explain to me the usage of Integer, Boolean etc in place of their primitive types in JAVA? I can't seem to grasp the advantages their are providing. They seem to create unnecessary problems of handling null values. Thanks! ...
Would you consider autoboxing in Java to be a form of polymorphism? Put another way, do you think autoboxing extends the polymorphic capabilities of Java? What about implicit conversions in Scala? My opinion is that they are both examples of polymorphism. Both features allow values of different data types to be handled in a uniform m...
Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let'...
hi, the following code throws NPE for me: int num = Integer.getInteger("123"); is my compiler invoking getInteger on null since it's static? that doesn't make any sense! can someone explain what's happening? thanks. ...
(Sorry if this is a duplicate, I have no idea how I would even search for this) I just saw code similar to this: public class Scratch { public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b); Integer c = 100, d = 100; System.out.println(c == d); } } ...
Is there any reason to use Integer.valueOf(X) to initialize a final Integer, as below: public class MyClass { public static final Integer DAY_1 = Integer.valueOf(1); // Why do it this way? public static final Integer DAY_2 = 2; // When it can be done this way? } I understand this was necessary in older versions of Java before auto...
Assigning values without using usual notation like "this.<Double>getAnything(int flag)" private <T> T getAnything(int flag) { Object o = null; if (flag==0) o=new String("NewString"); else if (flag==1) o=new Double(0D); return (T)o; } private void someMethod() { String s = getAnything(0); Double ...
Run the following Java code: boolean b = false; Double d1 = 0d; Double d2 = null; Double d = b ? d1.doubleValue() : d2; Why is there a NullPointerException? ...
What is happening when a java.lang.Double object is initialized without using a call to the constructor but instead using a primitive? It appears to work but I'm not quite sure why. Is there some kind of implicit conversion going on with the compiler? This is using Java 5. public class Foo { public static void main(String[] args...
public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a1 = 1000, a2 = 1000; System.out.println(a1==a2);//=>true Integer b1 = 1000, b2 = 1000; System.out.println(b1 == b2);//=>false ...
Usually the compiler generates code to perform boxing and unboxing. But what does the compiler, if the boxed values are not needed? Is the (Oracle standard) compiler smart enough to optimize it away? Take a look at this method: public static void requireInRange(int index, Object[] array) { if(index < 0 || index >= array.length) ...
Hello everyone. I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so? public String test(Object value) { if (! (value instanceof Boolean) ) return "invalid"; if (((Boolean) v...