9319

Dynamic cast to generic type in Java

Here is a trivial example I have put together:

private static <T> T getValue(T defaultValue) { if (defaultValue instanceof Boolean) { return (T) true; } return defaultValue; }

Essentially, I wish to return "true" if T is of boolean type. However, I get a compile error that boolean cannot be cast to T.

How do I do it?

Also, is there a way to check if T is of type boolean? Regards.

Answer1:

Change

return (T) true;

To

return (T) Boolean.TRUE;

This will work as Boolean.True is an instance of class Boolean. The value "true" is of the primitive type boolean.

Answer2:

true is a primitive type and you want to return an Object. You should wrap true in an object.

This works:

private static <T> T getValue(T defaultValue) { if (defaultValue instanceof Boolean) { return (T)Boolean.valueOf(true); } return defaultValue; }

Recommend

  • C# 6 bool and new null check with ? what to return back
  • Swift where condition to check if a property is implemented
  • Outlook 365 add-in only appears in Outlook 2013 client
  • PHP: Overriding parent methods with __call
  • Best Way to create configuration file(config.php) php
  • onFocus and onBlur
  • How to prevent TreeItem selection?
  • Read stdin in chunks in Bash pipe
  • calling a fragment from fragment
  • Quick Question About Get and Set
  • Find VMID for running instance
  • to implement a spinner in angular2+
  • MVC - @Html.CheckBoxFor
  • Symfony2 - Doctrine - no changeset in post update
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • System.InvalidCastException: Specified cast is not valid
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Java: can you cast Class into a specific interface?
  • PHP - How to update data to MySQL when click a radio button
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Javascript + PHP Encryption with pidCrypt
  • How to disable jQuery.jplayer autoplay?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?