• David (unregistered) in reply to stanbeard
    stanbeard:
    Exactly! All instances of 1 and 0 point to the same static values. If you use a lot of 1s and 0s it seems like this would save a lot of memory.

    But it's hard to imagine that memory is the only issue here. Computational performance would still suck - they still need to be converted back to ints for the CPU to perform actual operations.

  • Duc (unregistered) in reply to David

    The TWTF here is that most people didn't realise that auto boxing and/or Integer.valueOf() features are not available prior to java 5. And say, if you are still working with Websphere 6, you'll stuck with java 4, then actually the method makes lots of sense.

  • (cs)

    Real programmers produce programs only out of 0's and 1's (and they had better be cached). All the rest are luxuries for the simpletons.

  • (cs)
    Taking stuff from the database and sending it to browser?" Pah. From AWT 1.0 through Swing to whatever's new, jazzy, and fusion-orientated, it used to be that Java was great because you got cross-platform graphics for free.

    It was great because you got cross-platform graphics for free, and in a browser no less. When your alternative is a kludgy mish-mash of HTML and JavaScript, Java kicks ass.

    Too bad it didn't catch on there...

  • JohnFx (unregistered)

    Obviously the OP is missing the point. Once the developer who wrote these routines finishes overloading all the built in types, operators and language features they can add "built a compiler" to their resume. Right? </snark>

  • (cs) in reply to Sleepyhead
    Sleepyhead:
    public class CommentType { Comment c;
    public static void Post()
    {
        c.Post();
    }
    

    }

    CommentType.Post();

    c is null. What now?

  • jane doe (unregistered) in reply to java.lang.Chris;
    java.lang.Chris;:
    The first one is a mistaken attempt to prevent heap fragmentation by preventing the creation of many Integer objects for the integral values 0 and 1. It's a mistake because Integer.valueOf() already does this, and has done since the earliest days of Java.

    Except Integer.valueOf(int) was only added after 1.4. See javadoc for valueOf, in particular the "Since" part.

  • ölf (unregistered)

    Note: java.lang.Integer is final. (They made damn sure you can't even remotely get into operator overloading.)

    As to the predefined small number Integers: yes, ISTR there are internal constants for this purpose, but the API explicitly promises newly-allocated instances whenever you call the constructor. So there, obvious optimization, by docs not done by the compiler or the JVM.

  • magie (unregistered) in reply to Tom
    Tom:
    So TRWTF is here:
    if ( value == 0 ) {
      result = DomainConstants.INTEGER_0;
    should be:
    if ( value == DomainConstants.INTEGER_0 ) {
      result = DomainConstants.INTEGER_0;

    Can I have another constant?

    if ( value == 42 ) {
      result = DomainConstants.INTEGER_42;
  • _TrXtR_ (unregistered)

    2nd one is a WTF. But I see worse... all the time

  • Jesper (unregistered)
    public static Integer getIntegerValueOf(int value) {
            Integer result;
            if ( value == 0 ) {
                result = DomainConstants.INTEGER_0;
            } else if ( value == 1 ) {
                result = DomainConstants.INTEGER_1;
            } else {
                result = new Integer(value);
            }
            return result; 
        }

    This should have been written like this:

    result = Integer.valueOf(value);

    The author of the code obviously did not know that class Integer itself has a cache for commonly used values. Creating a new Integer object with "new Integer(...)" is bad, because it circumvents that cache, unnecessarily creating new objects.

  • Your Name (unregistered) in reply to Jesper

    Or maybe, just maybe, the code was written on Java 1.4.x which some of us are still stuck with.

  • AdT (unregistered) in reply to Lingo the Dingo
    Lingo the Dingo:
    I think you forgot Ruby in your list of 'I'm so cool' languages. All the cool kids are now claiming perl, python and Ruby as their languages of choice!

    All the cool kids also know that, when referring to the language as opposed to the runtime, Perl is capitalized.

  • sota (unregistered) in reply to Eternal Density
    Eternal Density:
    Sleepyhead:
    public class CommentType { Comment c;
    public static void Post()
    {
        c.Post();
    }
    

    }

    CommentType.Post();

    c is null. What now?

    The type or namespace name 'Comment' could not be found (are you missing a using directive or an assembly reference?)

  • anonymous (unregistered) in reply to java.lang.Chris;
    java.lang.Chris;:
    The first one is a mistaken attempt to prevent heap fragmentation by preventing the creation of many Integer objects for the integral values 0 and 1. It's a mistake because Integer.valueOf() already does this, and has done since the earliest days of Java.

    Yeah, the earliest days... back in JDK 1.5. This method didn't even exist prior to that release.

  • James (unregistered) in reply to Anonymous
    Anonymous:
    java.lang.Chris;:
    It's still a WTF, as the Integer class has static instances for Integer(0) and Integer(1) already.

    Show me where!

        public static Integer valueOf(int i) {
    	final int offset = 128;
    	if (i >= -128 && i <= 127) { // must cache 
    	    return IntegerCache.cache[i + offset];
    	}
            return new Integer(i);
        }
    
  • James (unregistered) in reply to mongolito404
    mongolito404:
    Since Java 1.5 has been released 4 years ago, it is time to replace to CommentUtils.getIntegerValueOf(int) by java.lang.Integer.valueOf(int i).

    The real WTF is to link to the Java SE 1.3.1 Javadoc which is eight years old... If you want to use a language that didn't change during the past decade use C, C++ or MUMPS.

    I sure hope they remembered to file a bug/task to make this fix in some future version of java where the implementation was improved, otherwise some random maintainer is doomed to stumble across it and think it was a WTF.

  • Al (unregistered) in reply to The Doubtful Guest

    He's absolutely right. Before JDK 1.4 which added Integer.valueOf, this was the only way to prevent multiple instantiation of the same integer.

  • Hans (unregistered)

    Well, if there's one thing you certainly can't trust a computer with, it's 1's and 0's. lol

  • J (unregistered) in reply to java.lang.Chris;

    What if the user wanted to cache just 0 and 1 and want a new instance for all other cases?

  • eulbobo (unregistered) in reply to Andy Goth

    Yes..... But in fact not.

    I submitted this one, and in fact, when they use 0 or 1, they use new Integer(1) and new Integer(0).

    They only use it for OTHER values.

    And when they want to get a 1 or 0 vaule, they use this one :

    public static final Integer parseInteger( String str ) { Integer result = null; if ( CommonUtils.isNotEmptyString(str)) { result = new Integer( Integer.parseInt( str ) ); } return result; }

  • eulbobo (unregistered) in reply to eulbobo

    Found a good illustration of what I was talking about

    public static Integer getArchiveYearDestruct(Typologie typologie, int yearEnd) { int year = typologie.getYearDestruct(yearEnd).intValue(); Integer result = CommonUtils.getIntegerValueOf(year); return result; }

    guess what? typologie.getYearDestruct(yearEnd) already returns an Integer

  • eulbobo (unregistered) in reply to eulbobo

    I really should stop looking in that class.... I found something worth a peek. For all those talking about optimisation and boolean use, here you are !

    public static Integer booleanToInteger(boolean bool){ Integer result = null; if(bool == true){ result = new Integer(1); } else { result = new Integer(0); } return result; }

    or the mighty

    public static final int parseInt(String integer) { return Integer.parseInt(integer); }

  • SuperJer (unregistered)

    I haven't actually used Java but after reading these comments I'm highly motivated to stay as far away from it as possible!

    Sweet scary Moses!

  • Jaden (unregistered)

    one word: FUTUREPROOFING

    PS. I wrote that code!

    Cheers, Jaden (Dominoes Chef)

  • The Guest Who is Doubtful of the Doubtful Guest (unregistered) in reply to The Doubtful Guest

    Except that Integer.getValueOf ALREADY caches the numbers from -128 to 127, so this is not only pointless but you also add in additional tests which are unnecessary.

    Not a big WTF, but still, this is not useful.

  • Holger (unregistered)

    The "implementation" of equals is severely broken:

    1. Fail
      IntegerType one = new IntegerType(1);
      if (!one.equals(one)) {
        throw new EqualsContractViolation("identity");
      }
    
    1. Fail
      Integer realOne = new Integer(1);
      if (one.equals(realOne) && ! realOne.equals(one)) {
        throw new EqualsContractViolation("reflexivity");
      }
    

Leave a comment on “CommonUtils and the Inadequate java.lang.*”

Log In or post as a guest

Replying to comment #:

« Return to Article