Increment behavior Try to make Java problem TypeScript 3-4

Select the execution result of the following program

java.java


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

A.7 is displayed B.32 C.33 D.43 E. A compile error occurs F. Exception is thrown at runtime

The answer is b. There is a problem with the behavior of the prefix and suffix increment operators.

In ++ a, a = a + 1 is set before the operation is performed. In a ++, a = a + 1 is added after the operation. Therefore a = 10; b = a++ + a + a-- - a-- + ++a;

a = 11; b = 10 + a + a-- - a-- + ++a;

a = 11; b = 10 + 11 + a-- - a-- + ++a;

a = 10; b = 10 + 11 + 11 - a-- + ++a;

a = 9; b = 10 + 11 + 11 - 10 + ++a;

a = 10; b = 10 + 11 + 11 - 10 +10;

The resulting answer is 32.

If you do this with TypeScript

TypeScript.ts


   var a:number = 10;
   var b:number = a++ + a + a-- - a-- + ++a;
   console.log(b);

It looks like this. The behavior of operators such as increment is roughly the same between languages, so you can use them in the same way.

Recommended Posts

Increment behavior Try to make Java problem TypeScript 3-4
Interface Try to make Java problem TypeScript 7-3
String operation Try to make Java problem TypeScript 9-3
Initialization of for Try to make Java problem TypeScript 5-4
[Java] Try to solve the Fizz Buzz problem
Try to solve a restricted FizzBuzz problem in Java
Java --How to make JTable
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4
[Beginner] Try to make a simple RPG game with Java ①
Try to make a simple callback
How to make a Java container
[Java] Try to implement using generics
Try to extract java public method
Try to implement Yubaba in Java
CompletableFuture Getting Started 2 (Try to make CompletableFuture)
Try to make a peepable iterator
How to make a Java array
[Java] Try to solve the Fizz Buzz problem using recursive processing
How to make a Java calendar Summary
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Try to implement n-ary addition in Java
Make something like Java Enum with Typescript
How to make a Discord bot (Java)
[Java] Beginners want to make dating. 1st
Let's migrate to make Java more comfortable
Try to build Java8 environment on Amazon Linux2
I tried to make Basic authentication with Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
Try to create a bulletin board in Java
Try to link Ruby and Java with Dapr
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Try to make a music player using Basic Player
Try to implement TCP / IP + NIO with JAVA
How to solve an Expression Problem in Java
Easy to make LINE BOT with Java Servlet
[Java] How to make multiple for loops single
Try Java 8 Stream
[Java] Problem No. 2
[Java] Problem No.3
[Java] Introduction to Java
[Java] Problem No.1
Introduction to java
Roughly try Java 9
[Java] I tried to solve Paiza's B rank problem
How to make Java unit tests (JUnit & Mockito & PowerMock)
Try to make an addition program in several languages
I tried to make a login function in Java
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try to build a Java development environment using Docker
I used to make nc (netcat) with JAVA normally