Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin have pointers?

Does Kotlin have pointers? If yes,

  1. How to increment a Pointer?

  2. How to decrement a Pointer?

  3. How to do Pointer Comparisons?

like image 294
Anisuzzaman Babla Avatar asked Oct 15 '25 07:10

Anisuzzaman Babla


2 Answers

It has references, and it doesn't support pointer arithmetic (so you can't increment or decrement).

Note that the only thing that "having pointers" allows you is the ability to create a pointer and to dereference it.

The closest thing to a "pointer comparison" is referential equality, which is performed with the === operator.

like image 57
cha0site Avatar answered Oct 17 '25 16:10

cha0site


There is no pointers in Kotlin for low-level processing as C.

However, it's possible emulate pointers in high-level programming.

For low-level programming it is necessary using special system APIs to simulate arrays in memories, that exists in Windows, Linux, etc. Read about memory mapped files here and here. Java has library to read and write directly in memory.

Single types (numeric, string and boolean) are values, however, other types are references (high level pointers) in Kotlin, that one can compare, assign, etc.

If one needs increment or decrement pointers, just encapsulate the desired data package into a array

For simulate pointers to simple values it just wrap the value in a class:

data class pStr (   // Pointer to a String
  var s:String=""
)

fun main() {
 var st=pStr("banana")
 var tt=st
 tt.s = "melon"
 println(st.s) // display "melon"

 var s:String = "banana"
 var t:String = s
 t.s = "melon"
 println(s.s) // display "banana"
}
like image 23
Paulo Buchsbaum Avatar answered Oct 17 '25 17:10

Paulo Buchsbaum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!