What’s the point of new String(“x”) in JavaScript?

There’s very little practical use for String objects as created by new String(“foo”). The only advantage a String object has over a primitive string value is that as an object it can store properties: var str = “foo”; str.prop = “bar”; alert(str.prop); // undefined var str = new String(“foo”); str.prop = “bar”; alert(str.prop); // “bar” … Read more

How to determine the primitive type of a primitive variable?

Try the following: int i = 20; float f = 20.2f; System.out.println(((Object)i).getClass().getName()); System.out.println(((Object)f).getClass().getName()); It will print: java.lang.Integer java.lang.Float As for instanceof, you could use its dynamic counterpart Class#isInstance: Integer.class.isInstance(20); // true Integer.class.isInstance(20f); // false Integer.class.isInstance(“s”); // false

Better way to get type of a Javascript variable?

Angus Croll recently wrote an interesting blog post about this – http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ He goes through the pros and cons of the various methods then defines a new method ‘toType’ – var toType = function(obj) { return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() }

typeof operator in C

Since typeof is a compiler extension, there is not really a definition for it, but in the tradition of C it would be an operator, e.g sizeof and _Alignof are also seen as an operators. And you are mistaken, C has dynamic types that are only determined at run time: variable modified (VM) types. size_t … Read more

What is the difference between typeof and instanceof and when should one be used vs. the other?

Use instanceof for custom types: var ClassFirst = function () {}; var ClassSecond = function () {}; var instance = new ClassFirst(); typeof instance; // object typeof instance == ‘ClassFirst’; // false instance instanceof Object; // true instance instanceof ClassFirst; // true instance instanceof ClassSecond; // false Use typeof for simple built in types: ‘example … Read more

C# switch on type [duplicate]

See another answer; this feature now exists in C# I usually use a dictionary of types and delegates. var @switch = new Dictionary<Type, Action> { { typeof(Type1), () => … }, { typeof(Type2), () => … }, { typeof(Type3), () => … }, }; @switch[typeof(MyType)](); It’s a little less flexible as you can’t fall through … Read more