I tried to summarize the basics of kotlin and java

Introduction ...

This article was written in Previous article,

** 2nd day of article posting every day for 7 days **

It has become

The code to use is pasted below, but see the previous article for more details on the features of this app!

--java version: https://github.com/sato-na/guruwake_java

--kotlin version: https://github.com/sato-na/guruwake_kotlin

↓ This is the main subject of this article ↓

Comparing the basics of each language ...

-- table of contents --

  1. How to define variables

  2. How to write an if statement

  3. How to write a for statement

  4. How to change from String to Int

Variable definition method

--For java

Type variable name=initial value;

Type must be specified

Example)

ResultActivty.java


String rTxt = "";                //31st line
int memberNum = memberL.size();  //Line 33

--For kotlin

val/var variable name=initial value

It is necessary to specify val (immutable) or var (variable)

Example)

ResultActivity.kt


var rTxt = ""                 //Line 22
val memberNum = memberL.size  //24th line

How to write an if statement

--For java

if (conditions) {
    //Processing when the condition is true
}

Example)

ResultActivity.java


if (n <= memberNum % groupNumI) {  //39th line
    p++;
}

--For kotlin

if (conditions) {
    //Processing when the condition is true
}

Example)

ResultActivity.kt


if (n <= memberNum % groupNumI) {  //30th line
    p++
}

Same way of writing

How to write a for statement

--For java

for (Variables used when turning the for statement;Variable condition;Processing of variables after processing) {
    //Processing when entering a for statement
}

Example)

ResultActivity.java


for (int q = 0; q < p; q++) {              //Line 43
    rTxt += "  " + memberL.get(0) + "\n";
    memberL.remove(0);
}

--For kotlin

for (Variable used in for In Initial value of variable..Final value){
     //Processing when entering a for statement
}

Example)

ResultActivity.kt


for (q in 0..p-1) {                    //Line 34
    rTxt += "   " + memberL[0] + "\n"
    memberL.remove(memberL[0])
}

The method of specifying the number of for statements is different

How to change from String to Int

--For java

int variable= Integer.parseInt(String to change)

Example)

ResultActivity.java


final String groupNum = intent.getStringExtra("GROUP_NUM");  //26th line
int groupNumI = Integer.parseInt(groupNum);                  //32nd line

--For kotlin

val/var variable name: Int =Value to change.toInt()

Example)

ResultActivity.kt


val groupNum = intent.getStringExtra("GROUP_NUM")  //18th line
val groupNumI : Int = groupNum.toInt()             //Line 23

The syntax is very different

Finally…

This time I tried to summarize the basics of java and kotlin. I'm only using what's in the code on github, so Variable names are difficult to understand, and there are many functions not mentioned in this article. Maybe it was hard to understand ... However, I hope it's a little easier to understand than just the code.

I will post an article like this tomorrow, so please keep an eye on me.

Recommended Posts

I tried to summarize the basics of kotlin and java
I tried to summarize the methods of Java String and StringBuilder
I tried to summarize the key points of gRPC design and development
I summarized the types and basics of Java exceptions
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize the basic grammar of Ruby briefly
I tried to summarize Java lambda expressions
[day: 5] I summarized the basics of Java
I tried to summarize the Stream API
I want to return to the previous screen with kotlin and java!
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I compared the characteristics of Java and .NET
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I translated the grammar of R and Java [Updated from time to time]
I tried to measure and compare the speed of GraalVM with JMH
Please note the division (division) of java kotlin Int and Int
05. I tried to stub the source of Spring Boot
Java language from the perspective of Kotlin and C #
I tried to reduce the capacity of Spring Boot
I want to transition screens with kotlin and java!
I tried to implement the Euclidean algorithm in Java
I didn't understand the behavior of Java Scanner and .nextLine ().
[JDBC] I tried to access the SQLite3 database from Java.
Command to check the number and status of Java threads
I tried to summarize iOS 14 support
I tried to verify this and that of Spring @ Transactional
[Swift] I tried to implement the function of the vending machine
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried to interact with Java
I tried to explain the method
I tried JAX-RS and made a note of the procedure
I tried to make Java Optional and guard clause coexist
I tried the Java framework "Quarkus"
I tried to summarize personally useful apps and development tools (development tools)
I tried to build the environment of WSL2 + Docker + VSCode
I tried to summarize personally useful apps and development tools (Apps)
I tried to make a client of RESAS-API in Java
I want to implement various functions with kotlin and java!
[Java] I thought about the merits and uses of "interface"
I tried using GoogleHttpClient of Java
I finished watching The Rose of Versailles, so I tried to reproduce the ending song in Java
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to summarize the words that I often see in docker-compose.yml
I tried to summarize what was asked at the site-java edition-
I tried to build the environment of PlantUML Server with Docker
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
Basics of Java development ~ How to write programs (variables and types) ~
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried the input / output type of Java Lambda ~ Map edition ~
I tried to translate the error message when executing Eclipse (Java)
Collection of programming selection tasks to make and remember (Java basics)
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to check the operation of gRPC server with grpcurl
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] I tried to make a maze by the digging method ♪
I tried to move the Java compatible FaaS form "Fn Project"
I tried to display the calendar on the Eclipse console using Java.
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th