Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java interoperability #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kotlinplayground;

public class CourseJava {

private Integer id;
private String name;
private String author;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public CourseJava(Integer id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}

@Override
public String toString() {
return "CourseJava{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}

public void printCourse(){
System.out.println(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.kotlinplayground;

import com.kotlinplayground.classes.Authenticate;
import com.kotlinplayground.classes.Course;
import com.kotlinplayground.classes.CourseCategory;
import com.kotlinplayground.classes.CourseKt;

public class InvokeKoltinFromJava {

public static void main(String[] args) {

var course = new
Course(1,
"Reactive Programming in Modern Java using Project Reactor",
"Dilip"
, CourseCategory.DEVELOPEMENT
);

CourseKt.printName1("dilip");
Course.Companion.printName2("abc");
var courseName = Course.courseName;
Course.printName2("abc");
//CourseUtils.printName1("dilip");

course.noOfCourses= 10;

//Authenticate.INSTANCE.authenticate("abc", "password");
Authenticate.authenticate("abc", "password");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ fun checkType(type: Any) {

when (type) {
is Course -> {
println("The type is Course")
//val course = type as Course // This is redundant
println("The type is CourseJava")
//val course = type as CourseJava // This is redundant
println(type.copy())
}
is String -> {
println("The type is Course ")
println("The type is CourseJava ")
println(type.lowercase())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kotlinplayground.classes

object Authenticate {
@JvmStatic
fun authenticate(userName : String, password: String){
println("User Authenticated for userName : $userName")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
//@file:JvmName("CourseUtils")

package com.kotlinplayground.classes

data class Course(
import com.kotlinplayground.CourseJava

data class Course (
val id: Int,
val name: String,
val author: String,
var courseCategory: CourseCategory = CourseCategory.DEVELOPEMENT
){

@JvmField
var noOfCourses = 10

companion object {

const val courseName = "Kotlin SpringBoot"

@JvmStatic
fun printName2(name : String) {
println("Name is $name")
}
}

}


@JvmName("printName1")
@JvmOverloads
fun printName(name : String = "default"){
println("name : $name")
}


/*
data class Course @JvmOverloads constructor(
val id: Int,
val name: String,
val author: String,
var courseCategory: CourseCategory = CourseCategory.DEVELOPEMENT
)
*/

enum class CourseCategory{
DEVELOPEMENT,
Expand All @@ -28,4 +64,11 @@ fun main() {
val marketingCourse = Course(1, "FaceBook Marketing", "Dilip", CourseCategory.MARKETING)
println("marketingCourse : $marketingCourse")

val courseJava = CourseJava(1, "Reactive Programming in Modern Java using Project Reactor", "Dilip")
courseJava.id = 1
courseJava.name = "ABC"
courseJava.author = "Dilip"

courseJava.printCourse()

}