Kotlin post- and pre-increment and operator overload (comparison with C, Java, C ++)

Overview

--Post-increment operator like a ++: Incremented after the value is passed to the function -Prefix increment operator like ++ b: Incremented before the value is passed to the function

[Increment -Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%82%AF%E3%83%AA%E3%83%A1% E3% 83% B3% E3% 83% 88)

In C language, C ++, Java, JavaScript, etc., the increment operator (increasing quantum) "++" is prepared. There are two types, pre-increment and post-increment. The lexical word is the same "++", but the meaning is different depending on whether it is used as a prefix operator (example: ++ x) or as a suffix operator (example: x ++). If the value of the operand is an integer, it will be 1, and if it is a pointer, it will change by one target, but the value as an expression will be the value after incrementing if it is a prefix (this means +). = 1), in the case of postfix, it is the value before incrementing.

environment

Post- and pre-increment with Kotlin

Kotlin source code.

var a = 1
var b = 1
println(a++)
println(++b)

Execution result. For a ++, 1 is output because it is passed to the function before the value is incremented by +1. In the case of ++ b, 2 is output because the value is incremented by +1 and then passed to the function.

$ kotlinc -script hello.kts
1
2

Kotlin's increment operator behaves like C and Java.

C source code.

#include <stdio.h>

int main(int argc, char *args[]) {
  int a = 1;
  int b = 1;
  printf("%d\n", a++);
  printf("%d\n", ++b);
  return 0;
}

Execution result.

$ gcc hello.c -o hello

$ ./hello 
1
2

Java source code.

public class Hello {
  public static void main(String[] args) {
    int a = 1;
    int b = 1;
    System.out.println(a++);
    System.out.println(++b);
  }
}

Execution result.

$ javac Hello.java 

$ java Hello
1
2

Operator overload and increment in Kotlin

Kotlin's increment operator overload is easy to write. You can overload the increment operator with operator fun inc.

data class Counter(val num: Int) {
  operator fun inc(): Counter {
    return Counter(num + 1)
  }
}

var a = Counter(1) 
var b = Counter(1)
println(a++)
println(++b)

Execution result. In the case of a ++, the value of num is passed to the function before it is incremented by 1, so the state of num = 1 is output. In the case of ++ b, the value of num is incremented by +1 and then passed to the function, so the state of num = 2 is output.

$ kotlinc -script counter.kts
Counter(num=1)
Counter(num=2)

Let's write the behavior of a similar increment operator in C ++. In C ++ you need to write separate functions for the suffix and the prefix.

#include <iostream>

class Counter {

private:
  int num;

public:
  Counter(int num) : num(num) {}

  // Prefix increment operator
  Counter& operator ++() {
    this->num++;
    return *this;
  }

  // Postfix increment operator
  Counter operator ++(int) {
    Counter c = *this;
    this->num++;
    return c;
  }

  friend std::ostream& operator<<(std::ostream& os, const Counter& c) {
    os << c.num;
    return os;
  }
};

int main(int argc, char *argv[]) {
  Counter a(1);
  Counter b(1);
  std::cout << a++ << std::endl;
  std::cout << ++b << std::endl;
  return 0;
}

Execution result.

$ g++ hello.cpp -o hellocpp
$ ./hellocpp 
1
2

Reference material

Recommended Posts

Kotlin post- and pre-increment and operator overload (comparison with C, Java, C ++)
Encrypt with Java and decrypt with C #
Link Java and C ++ code with SWIG
[Java] String comparison and && and ||
[Java] Overload and override
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Java language from the perspective of Kotlin and C #
I want to transition screens with kotlin and java!
Study Java # 2 (\ mark and operator)
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to implement various functions with kotlin and java!
C # and Java Overrides Story
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Comparison of WEB application development with Rails and Java Servlet + JSP
I want to return to the previous screen with kotlin and java!
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Differences between "beginner" Java and Kotlin
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Use JDBC with Java and Scala.
Hello world with Kotlin and JavaFX
Hello World with Docker and C
[Java beginner] == operator and equals method
Output PDF and TIFF with Java 8
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
How to make an app with a plugin mechanism [C # and Java]
Conversion between Kotlin nullable and Java Optional
Relationship between kotlin and java access modifiers
Monitor Java applications with jolokia and hawtio
Overload method with Int and Integer arguments
Call Java library from C with JNI
java core: HotSpot compiler and C heap
Let's try WebSocket with Java and javascript!
[For beginners] Difference between Java and Kotlin
[Java] Reading and writing files with OpenCSV
A Java engineer compared Swift, Kotlin, and Java.
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Collection and StringBuilder operation method comparison
[Kotlin] Delete files with duplicate contents [Java]
Hello world with Kotlin and Tornado FX
Interoperability tips with Kotlin for Java developers
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio