Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

527
Vistas
In Kotlin, Why can the value of a val integer be reassigned by the inc() method?

Consider the following,

val x: Int = 0

val variables cannot be changed so doing x += 1 wouldn't work

The compiler says Val cannot be reassigned

why then does x.inc() work fine

doesn't x.inc() reassign the value from 0 to 1

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

x.inc() does not increment the variable x. Instead, it returns a value that is one more than the value of x. It does not change x.

val x = 0
x.inc() // 1 is retuned, but discarded here
print(x) // still 0!

As its documentation says:

Returns this value incremented by one.

That might seem like a very useless method. Well, this is what Kotlin uses to implement operator overloading for the postfix/prefix ++ operator.

When you do a++, for example, the following happens:

  • Store the initial value of a to a temporary storage a0.
  • Assign the result of a0.inc() to a.
  • Return a0 as the result of the expression.

Here you can see how inc's return value is used.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda