-1

I'm relatively new to Java, but have some experience at Swift. In Swift, a function type and double type is treated exactly the same. I was just wondering is it the same with Java?

If not, what are some of the difference between function type and double type in Java? Does Java even have a function type?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    I am not a Swift expert, but I seriously doubt your statement that "a function type and double type is treated exactly the same". There are probably _some_ contexts where they are treated the same, but I think you need to be clear on what those are, and in particular which cases you're most interested in. Please update your question with some code examples and more specific information about what you're looking for. – ajb Jul 10 '16 at 23:51
  • 1
    Java 8+ has lambdas (and earlier versions could generally get away with using anonymous classes). A `double` is a primitive type in Java, so it is not at all related to functional types (in Java) - *unless* you are using a [`DoubleStream`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/DoubleStream.html) or perhaps calling a [`mapToDouble`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#mapToDouble-java.util.function.ToDoubleFunction-) *functor*. – Elliott Frisch Jul 11 '16 at 00:00
  • 1
    Coming from a literal sense, [absolutely](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html). – ChiefTwoPencils Jul 11 '16 at 00:56

2 Answers2

2

Functions are not first class citizens in Java, meaning that you cannot treat them as you would any regular data type such as a double. Java does however have a work-around using anonymous classes (or lambdas which are syntactic sugar) if you want to use a function as an argument to another function. See: What is a first class citizen function?

Community
  • 1
  • 1
mkzh
  • 196
  • 1
  • 8
0

I have no experience with swift; however I use Java fairly frequently (I am mainly a c and c++ guy now days), so I will try to take a stab at this question.

Java Variables

In Java there are types for variables such as int, double, long, and float. The reason you declare a variable type in Java is to tell the compiler how much memory to set aside to store the variable. Declaring variable type also tells the compiler how to manage the variable for instance adding two strings concatenates them where adding two numbers numerically adds them.

Java Functions

In Java when declaring a function you need to tell the compiler what type of value the function will return. For instance

int add(int num1, int num2){
   return num1 + num2;
}

returns an integer value, while

void doSomething(){
   ......
}

does not return anything. In general, functions can be declared of any type that a variable can be declared as. This concept applies to many languages (c and c++ are a couple major examples). I hope this answers your question.

Community
  • 1
  • 1
Dan
  • 78
  • 1
  • 4