2+2=5 in Java
amXXYgu0eFY • 2020-08-11
Transcript preview
Open
Kind: captions
Language: en
this video is about how we can hack java
by using reflection of its own source
code
to make this two plus two statement
output five
just as george orwell one of my favorite
writers warned us about in 1984
about propaganda machines that sublimate
the nature of truth
this video is not about politics
philosophy
nor is it about the apparent as i have
just learned
woke twitter madness around two plus two
equals five
though perhaps if we're living in the
simulation and it's written in java
this might be a way to make the
simulation just a bit more dystopian
so here's what the full source code
looks like and it uses
java's ability to do reflection which is
the
ability of a programming language to
inspect itself so if we look at the code
it actually dives into the
implementation of the integer
class pulls out the integer cache class
from that implementation
makes it accessible and writable
pulls it into an array of integer object
of size
256 and modifies that array
now what does this array contain so
interestingly if we look at the
integer cache class inside the integer
object implementation in java
it defines a hard-coded low of negative
128 and the height as passing as a
parameter
that's 127 as a default and what that
does is create a cache of integer
objects from
negative 128 to 127
and then reuses this cache every time an
integer object
with a value in this range is used now
this is exactly the cache
with reflection that we pull out and
modify
it so happens that the 132nd
element in the cache is where the four
resides and so by way of obfuscation
it takes the 133rd element which has the
number five in it
and the size into 132nd but you can just
assign
value five here and then the result
anywhere else in the code
if you use integer objects and the
number four comes up
it will instead output the number five
there you go
two plus two equals five check out the
link
in the description that points to the
stack exchange code golf has a bunch of
interesting discussions around this
including the possibility of taking the
entire 256
element array and shuffling it thereby
not only making 2 plus 2 equals 5
but messing with the entirety of low
value
arithmetic in java so there you go
that's how you hack the simulation
let me quickly thank the sponsors that
somehow amazingly support the podcast
and the videos i make
this time is a sleep mattress click the
link to get a discount in the
description
and by the way i have a conversation
with james gosling the creator of java
coming up
on the podcast so check that out and
remember try to learn something new
every day
you
Resume
Read
file updated 2026-02-13 13:25:04 UTC
Categories
Manage