官术网_书友最值得收藏!

Configuring Kotlin in Eclipse

We can also use Eclipse to work with Kotlin. Eclipse supports Kotlin via a plugin. If we don't have Eclipse, it can be downloaded from https://www.eclipse.org/downloads/. Observe the following steps to configure Kotlin in Eclipse:

  1. To install the Kotlin plugin, click on Help, and then Eclipse Marketplace.
  1. Search for Kotlin in the Search field and install the plugin.
  1. Restart Eclipse after installing the plugin in order for the changes to take effect.
  1. After restarting, select Kotlin in Open Perspective:

We have now successfully configured Kotlin in the Eclipse IDE.

Let's now take a look at how to create a simple Kotlin project in Eclipse:

  1. Select File, then New, and then Kotlin Project. 
  2. Enter the project name, choose its location, and click on Finish.
  1. This creates the following project structure:           
  

The steps to create a Kotlin file are as follows:

              1. Right-click on the project, and then select New|Kotlin File:

  1. Enter the name of the file and click on Finish.
  2. Let's now write a Kotlin program to check whether a given number is prime or not. Consider the following code for PrimeNumber.kt:
fun isPrime(number:Int):Boolean{
for (i in 2..number / 2) {
if (number % i == 0) {
return true
}
}
return false;
}

fun main(args:Array<String>){
val number = 11
if(!isPrime(number)){
println("$number is a prime number")
}else {
println("$number is not a prime number")
}
}
  1. To run the program, right-click on the source file, choose Run As, and then click on Kotlin Application:

The output is as follows:

主站蜘蛛池模板: 墨江| 景德镇市| 礼泉县| 荆门市| 家居| 巴中市| 砚山县| 娄烦县| 锡林郭勒盟| 柯坪县| 静安区| 广州市| 泾阳县| 宜良县| 津南区| 长子县| 雅安市| 黑水县| 沅江市| 平武县| 德保县| 临湘市| 中阳县| 疏勒县| 自贡市| 肥城市| 苗栗县| 隆子县| 当雄县| 定陶县| 曲阳县| 鸡东县| 大港区| 巨野县| 新晃| 郑州市| 南溪县| 永康市| 丹凤县| 尚志市| 开远市|