Skip to main content
43 events
when toggle format what by license comment
Mar 22, 2016 at 23:00 history rollback Dennis
Rollback to Revision 6
S Mar 22, 2016 at 22:10 history suggested CommunityBot CC BY-SA 3.0
Make it obvious why 132 is 4
Mar 22, 2016 at 21:18 review Suggested edits
S Mar 22, 2016 at 22:10
Jan 5, 2016 at 16:48 comment added Cruncher @Cyoce When there's noone else to vote? And other people that don't share my sentiment? lol. I do only count for one vote ya know ;)
Jan 5, 2016 at 16:05 comment added Cyoce @Cruncher so you are giving votes because he is receiving votes? When does it end?
Aug 29, 2014 at 19:03 comment added user12166 @DavisYoshida printf(String format, Object... args) takes an Object array while println(int x) takes an int. Thus, the value returned by 2+2 must be cast to an Object (an Integer in this case) by being auto boxed using valueOf(2+2). Look at valueOf and the cache.
Aug 29, 2014 at 18:58 comment added Davis Yoshida Interestingly, printf("%d",2+2) prints 5, but System.out.println(2+2) prints 4. Can anyone offer any insight into what's going on there?
Aug 21, 2014 at 15:00 comment added Kuba hasn't forgotten Monica So, sometimes Java lets you do crazier things than you could do in LISP...
Jun 12, 2014 at 21:33 comment added celtschk Changing the value of a constant? Java emulating old FORTRAN! This is the final proof that real programmers can write FORTRAN in any language ;-)
Jun 10, 2014 at 2:05 comment added user12166 @Mr.Mindor when cast to an Integer either with autoboxing or valueOf() - yes. I change the value of the entry where 4 sits in the IntegerCache class to be the same as 5 and anything that uses that will have the value of 5.
Jun 9, 2014 at 20:49 comment added Mr.Mindor With this, any calculation that would normally result in 4 will result in 5 correct? 1 + 3 = 5, 4 * 1 = 5, 2 * 2 = 5, 9 - 5 = 5...
Jun 6, 2014 at 15:34 comment added user12166 @JohnGaughan there was an answer that did that though I can't seem to find it (I thought it was a good one) and exposes some interesting bits on how String interning worked and where things where done when. That said, I'm still quite pleased with this approach because of its lack of use of the numbers 4 and 5 in the code.
Jun 6, 2014 at 15:32 comment added user18932 There are other Java reflection tricks, like changing immutable strings. The internal functionality of the String class has changed slightly over the years, but this should still be possible.
S Jun 3, 2014 at 20:02 history suggested MrLore CC BY-SA 3.0
Added syntax highlighting and changed URL to hyperlink
Jun 3, 2014 at 19:50 review Suggested edits
S Jun 3, 2014 at 20:02
Jun 3, 2014 at 19:34 review Suggested edits
Jun 3, 2014 at 19:45
Jun 3, 2014 at 9:06 comment added Richard Tingle @enderland The dailyWTF munger program does just that, constantly reconfigures Integers
Jun 2, 2014 at 16:20 comment added Cruncher zomg, java answers never get top votes! +1 for bringing java to the top :)
Jun 2, 2014 at 13:19 comment added enderland Anyone intending to do this should make sure to only have this happen a small fraction of the time. And then make sure to add a check in front of one of your statements something like "if 2+2= 5 then" with some comment about "for some reason sometimes 2+2=5, this accounts for these situations" so anyone reading it in the future is confused even more.
Jun 2, 2014 at 7:28 comment added Mohammad Banisaeid I just love Java's reflection. Way more freedom than .NET world.
Jun 1, 2014 at 12:51 comment added user12166 @SebastianGodelet there are a number of design choices that facilitate this, all for very good reasons. Metaprogramming that allows for advanced debuggers, introspection, and libraries. A Flyweight pattern in the Integer for performance when boxing and reduce memory usage. And a varargs structure allowing for variable length parameter lists (but then must be Objects rather than primitives). This combination of design choices can allow someone to poke in places they shouldn't but also allows for much more powerful programs and libraries.
Jun 1, 2014 at 10:22 comment added Léo Lam evil.js, evil.sh, evil.css and evil.java
Jun 1, 2014 at 8:14 comment added Sebastian Godelet It is very sad that one can do this.
Jun 1, 2014 at 0:58 comment added user12166 @qwr as an aside, I'm frequently in The Whiteboard chat room of Programmers.SE and would be more than happy to go through some of the uses of metaprogramming in Java (there are really neat things you can do with it if you use a 'safer' approach of annotations with libraries that understand them (rather than just poking around in the innards of a class)).
Jun 1, 2014 at 0:55 comment added user12166 @qwr reflection belongs to a type of programming known as metaprograming - writing programs about structures within the language. That type of thing exists in many languages and is a key part of how some things are done. You can see similar things in python and can be used to do some really neat things but that comes at the cost of being able to do dangerous stuff.
Jun 1, 2014 at 0:33 comment added qwr @MichaelT I'm new to Java, can you explain why this ability exists at all? Seems dangerous to leave in a language.
May 31, 2014 at 22:15 comment added Paŭlo Ebermann To make this underhanded, the manipulation has to be hidden a bit more.
May 31, 2014 at 10:16 comment added Alvin Wong @MichaelT Collections.shuffle(Arrays.asList(array));
May 30, 2014 at 23:59 comment added Voo @Michael Fair point, considering the goal of making it underhanded. So right you are, way less obvious this way
May 30, 2014 at 23:29 comment added user12166 @Voo true, and I did consider that a bit. However, its not quite as subtle/underhanded... the code above doesn't even use the digits 4 or 5.
May 30, 2014 at 21:33 comment added Songo Answers like this make me love this site more than SO.
May 30, 2014 at 20:27 comment added Voo Since you're already using reflection, you could easily make this independent of the cache size (which does depend on runtime flags after all): Field f = Integer.class.getDeclaredField("value"); f.setAccessible(true); f.set(4, 5) Could be made even more reliable by just grabbing the first non-static field - any reasonable implementation will only have a single field after all.
S May 30, 2014 at 19:32 history suggested durron597 CC BY-SA 3.0
Spoiler tags
May 30, 2014 at 19:19 review Suggested edits
S May 30, 2014 at 19:32
May 30, 2014 at 19:13 history edited user12166 CC BY-SA 3.0
Finish/fix/tweak
May 30, 2014 at 19:06 history edited user12166 CC BY-SA 3.0
Add ideone link, move to implicit boxing.
May 30, 2014 at 17:58 comment added John Dvorak @MichaelT this got me thinking about a new kind of russian roulette ;-)
May 30, 2014 at 17:57 comment added user12166 @JanDvorak in chat it was suggested Integer[] array = (Integer[]) c.get(cache); fisherYatesShuffle(array); Just imagine how many things would break...
May 30, 2014 at 17:45 comment added John Dvorak That. Is. Evil. Absolutely evil. +1
S May 30, 2014 at 17:37 history suggested CommunityBot CC BY-SA 3.0
fix typos/grammar; separate and label warning message; minus sign
May 30, 2014 at 17:33 review Suggested edits
S May 30, 2014 at 17:37
May 30, 2014 at 17:14 comment added Octavia Togami Thought of this one as soon as I saw the question :)
May 30, 2014 at 16:07 history answered user12166 CC BY-SA 3.0