Skip to main content
Commonmark migration
Source Link

##Java

Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); //5 /**** Another ***/ field.set("\u0032\u0020\u002B\u0020\u0032", new char[] {'\u0035'}); System.out.println("2 + 2".equals("5")); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

##Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); //5 /**** Another ***/ field.set("\u0032\u0020\u002B\u0020\u0032", new char[] {'\u0035'}); System.out.println("2 + 2".equals("5")); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); //5 /**** Another ***/ field.set("\u0032\u0020\u002B\u0020\u0032", new char[] {'\u0035'}); System.out.println("2 + 2".equals("5")); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

added 184 characters in body
Source Link
c.P.u1
  • 1.1k
  • 6
  • 8

##Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); //5 /**** Another ***/ field.set("\u0032\u0020\u002B\u0020\u0032", new char[] {'\u0035'}); System.out.println("2 + 2".equals("5")); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

##Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

##Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); //5 /**** Another ***/ field.set("\u0032\u0020\u002B\u0020\u0032", new char[] {'\u0035'}); System.out.println("2 + 2".equals("5")); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.

Source Link
c.P.u1
  • 1.1k
  • 6
  • 8

##Java

Using reflection:

import java.lang.reflect.Field; public class TwoPlusTwoTest { public static void main(String[] args) throws Exception { Field field = String.class.getDeclaredField("value"); field.setAccessible(true); field.set("\u0032", new char[] {51}); int twoPlusTwo = Integer.parseInt("2") + 2; System.out.println(twoPlusTwo); } } 

Reflectively accesses the internal array field, named value, that String is backed by, and sets its value to 3. Because all constant Strings are interned and are accessed from the pool of constant Strings, this results in the String literal "2" actually refer to a char array with value {3}.

##JavaScript

Using an overloaded valueOf:

String.prototype.valueOf = function () { return 3; } Array.prototype.valueOf = function () { return 3; } console.log(2 + [2]); //5 console.log(2 + new String(2)); //5 Array.prototype.valueOf = function () { return 2.5; } console.log([2] + [2]); //5 

The valueOf method is called by the engine when an object is used in a numeric context.