Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Follow publication

Kotlin vs Java Example: Methods Or Functions | Erselan Khan

--

Today I will show you the Syntax Differences between Kotlin and Java programming languages for Methods and Functions. But before moving forward, I would like to ask you to please follow my medium account to get the latest updates about Android and other tech-related topics and also check out my previous part of this series here Link

Methods or Functions:

A Method or Function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). The difference between Method or Function is that the Method is a piece of code that is called by a name that is associated with an object but Function doesn't associate with any object.

We are covering four types of methods in this example:

  1. A method without return type and with no parameter.
  2. A method with return type and with no parameter.
  3. A method without return type but with parameter.
  4. A method with return type with parameter.

Let’s write some methods in the Java programming language:

public class MethodInJava {
public static void main(String[] args) {
SomeContent someContent = new SomeContent();
someContent.methodWithoutReturnTypeAndNoParameter();
String first = someContent.methodWithReturnTypeAndNoParameter();
System.out.println("return type of methodWithReturnTypeAndNoParameter: " + first);
someContent.methodWithoutReturnTypeWithParameter("Erselan Khan");
String second = someContent.methodWithReturnTypeWithParameter("Erselan Khan");
System.out.println("return type of methodWithReturnTypeWithParameter: " + second);
}
}

class SomeContent {
public void methodWithoutReturnTypeAndNoParameter() {
System.out.println("called method methodWithoutReturnTypeAndNoParameter");
}

public String methodWithReturnTypeAndNoParameter() {
return "Erselan Khan";
}

public void methodWithoutReturnTypeWithParameter(String name) {
System.out.println("called method methodWithoutReturnTypeAndNoParameter, Print my name:" + name);
}

public String methodWithReturnTypeWithParameter(String name) {
return name;
}
}

In our above examples, we are using void that is used in Java programming for no return type. As we already know that the methods are associated with an object, that’s why we are using the object of SomeContent class to access the methods of that class.

Now it’s time for the Kotlin programming language:

fun main() {
val methodInKotlin = MethodInKotlin()
methodInKotlin.methodWithoutReturnTypeAndNoParameter()
val first = methodInKotlin.methodWithReturnTypeAndNoParameter()
println("return type of methodWithReturnTypeAndNoParameter: $first")
methodInKotlin.methodWithoutReturnTypeWithParameter("Erselan Khan")
val second = methodInKotlin.methodWithReturnTypeWithParameter("Erselan Khan")
println("return type of methodWithReturnTypeWithParameter: $second")
}

class MethodInKotlin {
fun methodWithoutReturnTypeAndNoParameter() {
println("called method methodWithoutReturnTypeAndNoParameter")
}

fun methodWithReturnTypeAndNoParameter(): String {
return "Erselan Khan"
}

fun methodWithoutReturnTypeWithParameter(name: String) {
println("called method methodWithoutReturnTypeAndNoParameter, Print my name: $name")
}

fun methodWithReturnTypeWithParameter(name: String): String {
return name
}
}

In our above examples, we are not defining our return type in case of no return type, but we can also use Unit (which is used for no return type in Kotlin). As we already know that the methods are associated with an object, that’s why we are using the object of MethodInKotlin class to access the methods of that class.

That’s it for now. I will cover more topics on Android in my upcoming articles.

Next Tutorials:

  1. https://erselankhan.medium.com/android-kotlin-vs-java-series-part-3-handle-null-objects-dd8354021e38

Show your love by sharing this article with your fellow developers and also following my Medium account.

(Again, the source for this demo is on https://github.com/arsalankhan994/kotlin-vs-java/tree/master/src/main. Follow me for more content about Android, Kotlin, and other technologies. If you have any questions, go ahead and ask me here or email me at arsalankhan994@gmail.com and I’ll do my best to respond.)

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Written by Erselan Khan

Software Engineer | Android Developer | Kotlin | Springboot | Java | Technical Content Writer | Motivational Speaker

No responses yet

Write a response