86751

Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> Are Kotlin data types built off primitive or non-primitive Java data types? <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

    I'm new to Kotlin, and AFAICT its syntax only supports the object versions of Int, Float etc without the corresponding int and float primitives of Java. But does the compiler or JVM optimise to use the primitive types if possible? I'm concerned that if I use local variables in a function called from a game main loop it might cause GC stutter if the JVM has to create an object each time instead of using a primitive type.

    Answer1:

    Quoting the docs:

    Some of the types can have a special internal representation - for example, numbers, characters and booleans can be represented as primitive values at runtime - but to the user they look like ordinary classes. In this section we describe the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings.

    So yes, the compiler does optimise in a way that the JVM primitive types are used at runtime. There are certain exceptions of course:

    On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. Int?) or generics are involved. In the latter cases numbers are boxed.

    There's also a hint in the source documentation, e.g. Int:

    Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int.

Recommend

  • Can an empty virtual table exist?
  • Is it really necessary to provide all the different app icon sizes for an iOS app?
  • .NET Core RC2 - Consuming External WCF
  • Cancel current/active query in Ruby on Rails
  • jQuery’s css() lags when applied on scroll event
  • What does a crossed out static import in Eclipse mean? [duplicate]
  • Helgrind for Windows?
  • Implementation of monitors with semaphores
  • “undefined symbol: SQLAllocEnv” error in Java [duplicate]
  • Why == is different for Integer and String?
  • How can I use scipy optimization to find the minimum chi-squared for 3 parameters and a list of data
  • Find VMID for running instance
  • What is the correct way to synchronize a shared, static object in Java?
  • ThreadStatic in asynchronous ASP.NET Web API
  • Cast between interfaces whose interface signatures are same
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • How to use JavaScript to determine whether a file exists in a directory?
  • ADO and msqli connections very slow
  • Ensure fsync did its job
  • How to get Eclipse Oxygen to run on Java 9
  • PHP buffered output depending on server setting?
  • Google Custom Search with transparent background
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How to extract text from Word files using C#?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Why winpcap requires both .lib and .dll to run?
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Does armcc optimizes non-volatile variables with -O0?
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?