Difference between Groovy def and Java Object? - java

I'm trying to figure out the difference between
Groovy:
def name = "stephanie"
Java:
Object name = "stephanie"
as both seem to act as objects in that to interact with them i have to cast them to their original intended type.
I was originally on a search for a java equivalent of C#'s dynamic class ( Java equivalent to C# dynamic class type? ) and it was suggested to look at Groovy's def
for example my impression of groovy's def is that I could do the following:
def DOB = new Date(1998,5,23);
int x = DOB.getYear();
however this wont build
thanks,steph
Solution edit:
Turns out the mistake iw as making is I had a groovy class wtih public properties (in my example above DOB) defined with def but then was attemping to access them from a .java class(in my example above calling .getYear() on it). Its a rookie mistake but the problem is once the object leaves a Groovy file it is simply treated as a Object. Thanks for all your help!

Per se, there is not much difference between those two statements; but since Groovy is a dynamic language, you can write
def name = "Stephanie"
println name.toUpperCase() // no cast required
while you would need an explicit cast in the Java version
Object name = "Stephanie";
System.out.println(((String) name).toUpperCase());
For that reason, def makes much more sense in Groovy than unfounded use of Object in Java.

You can experiment with groovy in the groovy web console http://groovyconsole.appspot.com/
Your initial groovy date example works.

Related

Convert Java object to python in py4j

I have a spark Scala library and I am building a python wrapper on top of it. One class of my library provides the following method
package com.example
class F {
def transform(df: DataFrame): DataFrame
}
and I am using py4j in the following way to create a wrapper for F
def F():
return SparkContext.getOrCreate()._jvm.com.example.F()
which allows me to call the method transform
The problem is that the python Dataframe object is obviously different from the Java Dataframe object. For this purpose, I need a way to convert a python df to a java one, for which I use the following code from py4j docs
class DataframeConverter(object):
def can_convert(self, object):
from pyspark.sql.dataframe import DataFrame
return isinstance(object, DataFrame)
def convert(self, object, gateway_client):
from pyspark.ml.common import _py2java
return _py2java(SparkContext.getOrCreate(), object)
protocol.register_input_converter(DataframeConverter())
My problem is that now I want to do the inverse: getting a java dataframe from the transform and continue to use it in python. I tried to use protocol.register_output_converter but I couldn't find any useful example, apart for code dealing with java collections.
How can I do that? An obvious solution would be to create a python class F which defines all methods present in java F, forwards all the python calls to the jvm, get back the result and convert it accordingly. This approach works but it implies that I have to redefine all methods of F thus generating code duplication and a lot more maintainance

Can I use java library of commons math in kotlin language?

I had seen forums and questions that can be used kotlin in java but, with respect to my question is that I want to use the apache math commons library ("which is only available in java") within kotlin. My project is in intellij idea and I have imported the library correctly, I show you how it is written in java
import org.apache.commons.math3.distribution
NormalDistribution normalDistribution = new NormalDistribution(10, 3);
double randomValue = normalDistribution.sample();
```
A class is a class, regardless of if it's defined in Java or Kotlin. For the most part, this means you just do the Kotlin thing in Kotlin and the Java thing in Java, regardless of where the class you're using is defined. There are exceptions, like for static methods, but most stuff "just works".
I expect, knowing nothing about the NormalDistribution class, that this will work:
val normalDistribution = NormalDistribution(10.0, 3.0);
val randomValue = normalDistribution.sample();
Ok, so I was wrong initially. I had to change my literals above from (10, 3) to (10.0, 3.0). Here's a difference between Java and Kotlin. Kotlin doesn't do automatic numeric type promotion. So while I could use Integer literals for the equivalent Java code, in Kotlin, I had to use Double literals. But my IDE showed me this right away, including a tooltip message that told me just what was wrong. And this is a Kotlin thing, not a Java thing. The same thing would happen if I tried to call a method defined in Kotlin taking doubles as parameters, and I tried to pass it integers. This had nothing to do with which language NormalDistribution is defined in. After that exercise, I can say for sure that this Kotlin code works fine.
Maybe the issue is more that you just don't know Kotlin very well yet. Part of learning Kotlin is realizing how much of a non-issue it is to use Java classes in Kotlin code.

Best way to wrap Java classes into Python

I have a Java library and I have to build a Python wrapper to it.
I am using py4j, and it's pretty easy to get any instance and any class, complete with method.
The problem is that the type of an object doesn't correspond to its class.
From python:
>>> gw = JavaGateway()
>>> a_circle = gw.jvm.com.geometry.Circle(15)
>>> a_circle.getDiameter()
30
>>> type(a_circle)
<class 'py4j.java_gateway.JavaObject'>
This is almost ok for basic usage, but what if I want to create custom constructors? Should I subclass the JavaObject class? Or it's better to create my classes from scratch and for every method and constructor invoke the corresponding Java method?
Any suggestion for a good way to achieve it? Should i try something different than py4j?
Thank you!
EDIT: for example, I have to wrap a Java class that has 3 methods, one of those methods takes an array as parameter, so I have to inject some code in order to convert.
class Service:
def __init__(self, javaService):
'''
Create a new instance of Service, assigning Java methods as attributes.
Accepts a working instance of Service from Java
This constructor is not meant to be called by the user.
'''
self.subscribe = javaService.subscribe
self.unsubscribe = javaService.unsubscribe
def publish(values):
'''
Wraps the java implementation of this method, converting the list of value from a Python iterable to a Java list.
'''
global java_gateway
parameterValues = ListConverter().convert(values, java_gateway._gateway_client)
return javaService.publish(values)
self.publish = publish
This works, but I am doing only when it is necessary. If the class just works directly, I am not writing anything to wrap it.

How to cast variables in python with jcc

In java it is possible to cast an object onto a class.
An good example is found here
Object aSentenceObject = "This is just a regular sentence";
String aSentenceString = (String)aSentenceObject;
I have a program that needs to integrate some java with python. I am trying to do this via the JCC library. The problem that I am encountering is that with JCC, all of the java classes are loaded into the imported library that I created with JCC. So I can create an instance of the base class by passing the necessary argument to the constructor of the java class.
obj = javaLibrary.BaseClass('foo')
However, in my code I need to be able to cast this object onto a a more “specific” type of Object.
How can I accomplish this in python with JCC? It seems like it may be impossible because python is dynamically typed, but that is why I am asking this question.
All comments above valid, but to be specific for your case:
casted_obj = Object.cast_(obj)

Is Javascript constructor function equivalent/similar to a class or interface in Java

I am trying to pick up the basics of Java and I am more familiar with JavaScript.
Is the following statement accurate (I just need high level understanding):
Javascript constructor function or factory function is the equivalent (i am using this word loosely here) of a class or interface in Java.
EDIT:
This is what I am reading in a Java book:
A Java program is mostly a collection objects talking to other
objects by invoking each other's methods. Every object is of a
certain type, and that type is defined by a class or an
interface. Most Java programs use a collection of many different types.
Coming from Javascript, above sounds very much like JS constructor function is similar to a class in Java where the objects properties and methods would be defined.
I know Java and JavaScript are two separate languages.
Thanks
I'd say you're close. Constructor functions (and prototypes) in JavaScript are the closest thing to Java classes that we have in JS; but they're certainly not "equivalent".
You can dynamically add or remove properties and methods to the prototype of a JavaScript constructor; you can't add or remove things from a Java class at runtime.
Example:
function Foo() {}
Foo.prototype.hello = function() { alert('hello'); };
var f = new Foo();
f.hello(); // alerts 'hello'
delete Foo.prototype.hello;
f.hello(); // throws an error
You can achieve "inheritance" at runtime in JavaScript simply by assigning the prototypes of constructor functions to arbitrary objects. In Java you declare inheritance at compile-time and it cannot be changed at runtime.
Example:
function EnglishSpeaker() {}
EnglishSpeaker.prototype.greet = function() { return 'hello'; };
function SpanishSpeaker() {}
SpanishSpeaker.prototype.greet = function() { return 'hola'; };
function Me() {}
Me.prototype = EnglishSpeaker.prototype;
var me = new Me();
me instanceof EnglishSpeaker; // true
me.greet(); // 'hello'
Me.prototype = SpanishSpeaker.prototype;
me = new Me();
me instanceof EnglishSpeaker; // false
me instanceof SpanishSpeaker; // true
me.greet(); // 'hola'
In JavaScript a prototype is simply an object. So a "class" (constructor function) can "inherit" from any plain object; thus there is a much looser distinction between "types" and "values".
Example:
function Thing() {}
var randomObject = { foo: 1, bar: 2 };
Thing.prototype = randomObject;
var thing = new Thing();
thing.foo; // 1
In Java you can define an interface which some class must implement. JavaScript doesn't really provide any such mechanism.
These are just some of the differences off the top of my head. Point is: they're similar, and you're right to draw a connection. But they are definitely not the same.
JavaScript:
function Cat(name) {
this.name = name;
this.talk = function() {
alert( this.name + " says meeow!" );
};
}
var cat1 = new Cat("Felix");
in Java:
public class Cat {
private String name;
public Cat(String name) {
this.name = name;
this.talk();
}
public void talk() {
System.out.println( this.name + " says meeow!" );
}
}
Cat cat1 = new Cat("Felix");
From source :Does JavaScript have the interface type (such as Java's 'interface')?
JavaScript inheritance is based on objects, not classes. That's not a big deal until you realize:
JavaScript is an extremely dynamically typed language -- you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. It'd be so easy to subvert the type system -- even accidentally! that it wouldn't be worth it to try and make a type system in the first place.
If you're coming from JavaScript and learning Java, there's one huge difference that I don't think anyone else has mentioned yet.
In JavaScript, a function is an object. An object can contain properties whose values can be function objects, or they can be other objects or primitive values. When you call obj.method(arguments), the semantics are to look for a method property of the object (or a prototype), and if it's a function object, to call it.
In Java and other compiled languages with OOP features, functions are not objects. If you create a new object of a particular type, the "type" information in the object refers to a list of polymorphic functions for that type. That's how polymorphism works in these languages. If you call obj.method(arguments), and method is a method that can be overridden for derived types, then the program looks up obj's type, and then looks in the function list for the type to determine which function to call. What this means, to me, is that the object's type is a key piece of information in these languages, and OOP revolves around it; while in JavaScript, the "type" of an object is quite a bit less important, since the function itself is looked up by name in the object. One can use JavaScript in a way to make it look like it's emulating the way other languages handle OOP, but it's not required, and it's not a built-in part of the language.
I think you have to keep that in mind when going from one to the other. If you try too hard to relate Java features to JavaScript features you're already familiar with, it will be confusing.
If you are new to Java, I suggest you go to amazon.com and look for a beginning book on java that has good reviews and read it cover to cover. I think this will be the best use of your time rather than reading varoius articles on Java and picking it up piece-meal. I also suggest you don't try to port your knowledge of Javascript to Java, least you make learning Java that much harder. Note there are many books you can read for Java (web) development: Java, products built on Java (JSF, Tomcat, etc), and supporting technologies (HTML, CSS, XML, etc).

Categories

Resources