Initialization of for Try to make Java problem TypeScript 5-4

Select the execution result of the following program

java.java


public class Main{
 public static void main(String[] args){
  for ( int i = 1,long j = 2; i < 5; i++){
   System.out.print(i * j);
  }
 }
}

Display as A.2468 B.246810 C. Compile error D. Exception is thrown at runtime

The answer is C When initializing two variables in java at the same time, the types must be the same. In other words

java.java


public class Main{
 public static void main(String[] args){
  for ( int i = 1,j = 2; i < 5; i++){
   System.out.print(i * j);
  }
 }
}

If so, it is the answer of A.

If you do this with TypeScript

TypeScript.ts


for(var i=1,j=2;i<5;i++){
 console.log(i*j);
}

It will be written as. Typescript has a type declaration, but you can't explicitly declare a type in for initialization. Therefore

TypeScript.ts


for(var i=1:number,j=2:number;i<5;i++){
 console.log(i*j);
}

Writing like this will result in a compile error. Type inference runs by entering an initial value and is defined with the appropriate type.

Also, on the Java side, it is divided into an integer int and a real number long. Type inference works fine even if the types are different in this way.

TypeScript.ts


for(var i=1,j="masao";i<5;i++){
 console.log(i*2);
}

There is nothing wrong with writing it this way.

Recommended Posts

Initialization of for Try to make Java problem TypeScript 5-4
Interface Try to make Java problem TypeScript 7-3
Increment behavior Try to make Java problem TypeScript 3-4
String operation Try to make Java problem TypeScript 9-3
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4
[Introduction to Java] Basics of java arithmetic (for beginners)
[Java] Try to solve the Fizz Buzz problem
[Java] How to make multiple for loops single
Introduction to Java for beginners Basic knowledge of Java language ①
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try to solve a restricted FizzBuzz problem in Java
How to execute WebCamCapture sample of NyARToolkit for Java
Java --How to make JTable
[Java] Summary of for statements
[Beginner] Try to make a simple RPG game with Java ①
Initialization with an empty string to an instance of Java String type
Try Spark Submit to EMR using AWS SDK for Java
I tried to make a client of RESAS-API in Java
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[For beginners] Summary of java constructor
Try to make a simple callback
How to make a Java container
[Java] Input to stdin of Process
Collection of programming selection tasks to make and remember (Java basics)
[Java] Try to implement using generics
Try to extract java public method
Try to implement Yubaba in Java
Generics of Kotlin for Java developers
CompletableFuture Getting Started 2 (Try to make CompletableFuture)
How to check for the contents of a java fixed-length string
Try to make a peepable iterator
How to write offline real-time Java implementation example of F01 problem
How to make a Java array
How to make a groundbreaking diamond using Java for statement wwww
Use static initialization block to initialize List / Set of static fields in Java
Practice of Java programming basics-I want to display triangles with for statements ①
Practice of Java programming basics-I want to display triangles with for statements ②
Implementation of clone method for Java Record
How to make a Java calendar Summary
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Introduction to java for the first time # 2
Try to implement n-ary addition in Java
[java] Summary of how to handle char
Make something like Java Enum with Typescript
About the procedure for java to work
How to make a Discord bot (Java)
List of download destinations for oracle java
[Java] [Maven3] Summary of how to use Maven3
[Java] Beginners want to make dating. 1st
[Java beginner] About initialization of multidimensional array
Output of the book "Introduction to Java"
Features of spring framework for java developers
Let's migrate to make Java more comfortable
Memo for migration from java to kotlin
NLP4J [006-034b] Try to make an Annotator of 100 language processing knock # 34 "A's B" with NLP4J
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)
Try to make a timeline report of method execution time using JFR API
Use jenv to enable multiple versions of Java
[Java] How to test for null with JUnit