google support for kotlin in android development [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
As we now Google announced in 2017 first-class support for Kotlin.
But what does it change? Is there more documentation for new features written in Kotlin language?
How does it affect Java?
My another question:
Are Kotlin written aps generally faster?(on Android device)
EDIT: Guys I though this question is properly asked.. Don't vote me down

Java Issues Addressed by Kotlin
NullSafety — The Billion Dollar Mistake is the name given to the danger of null references in code. Kotlin’s type system is aimed at eliminating the danger of these null references. This has been one of the most common pitfalls in Java — and many other programming languages as well.
No more Raw Types — Kotlin is designed with Java interoperability in mind. So now, existing Java code can be called from Kotlin in an effective way. This allows the calling of Java code from Kotlin. Alternatively, Kotlin code can be used in Java rather smoothly.
Invariant Arrays — The basic types used in Kotlin are Numbers, Arrays, Characters, and Strings. Unlike Java, the arrays in this programming language are invariant, meaning that Kotlin does not let a user assign an Array to an Array. This prevents a possible Run time Failure, which is one of the issues faced in Java.
Function Types — In Kotlin, a lambda expression or an anonymous function can access the variables declared in the outer scope. That is opposed to Java’s SAM-conversions — Kotlin has proper function types.
Use-site Variance — Wildcard Types are one of the trickiest parts of Java’s Type System. This issue does not occur in Kotlin — as it does not have any Wildcard Types, just Type Projections and declaration-site variances.
Exceptions — Kotlin does not have any checked exceptions, as all exception classes in this language are the descendants of the class Throwable. And every exception has a message, stack trace, and an optional cause.
Why Choose Kotlin
Smart Casts
Working with the mixed types requires knowing the type of an object at the Run time in order to safely cast the object to the desired type — and, further, to call methods or access properties on it. For class casting in Java, we first check the type of the variable using the ‘instance of’ operator and then cast it to the target type.
Whereas in Kotlin, when we perform an ‘!is’ or ‘is’ check on a variable, the compiler tracks this information and will automatically cast the variable to the target type where is the ‘!is’ or ‘is’ check is true in the scope.
Singletons
Once in a while, a user needs to create an object of a slight modification of some class but without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes, but Kotlin generalizes the same concept by using object expressions and declarations. Just like the anonymous inner classes in Java, the code in object expressions can access variables from the enclosing scope. But in Kotlin, this is not restricted to final variables like in Java.
Data Classes
The whole purpose of creating classes is to hold data and in some classes — standard functionality with utility functions can be mechanically derived from that data. This is known as a Data Class in Kotlin. These classes generally contain some old boilerplate code in the form of toString(), hashcode(), equals(), setters, and getters.
Basically, Kotlin’s Data Classes are like regular classes but with some additional functionality.
NOTE: There are more things that have in Kotlin which help developers to write faster, consize and clean code

Related

Why does Java's Dynamic Proxies need reflection?

Java's Dynamic Proxy Docs describe these constructors as the following:
A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface. Thus, a dynamic proxy class can be used to create a type-safe proxy object for a list of interfaces without requiring pre-generation of the proxy class.
Now, while everything in this sentence is accurate. All of this information is in fact present at compile time. In Java, when you are creating a proxy, you specify the exact interface you want to proxy in your code.
Now the first thing that really confuses me, is why the byte code here needs to be generated at run time? All of this information is present at the compile time... (and you don't even have to deal with type erasure)
P.S: I am not sure if this is still the case, basing this on a quite dated accepted answer here: How does Java's Dynamic Proxy actually work?)
The next step in getting this work is the type checking/type inference. I am not sure how Java actually handles this, but you need to be able to use Proxy<A> interchangeably with A. In order to pull this of you need the following:
∀ method m ∈ A, m ∈ Proxy<A>
Which means that
you need your proxy to have the same structure
you need some kind of delegation (i.e. dynamic dispatch).
Once you start writing out the inference rules, this gives us something very familiar. Structural Typing
Now Java doesn't have structural typing but one could easily add the few inference rules (i.e. Typescript) especially given Java has boxed primitives (Scala for example doesn't which makes it very difficult to introduce structural inference).
The Actual Question
Reflection is hard, not safe and not super performant. My question is why and how Java's proxies use reflection? It seems like most of this functionality could be implemented using other features already present in the language.

Is there any runtime benefit of using lambda expression in Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading couple of blogs and answers as well on lambda expression provided in Java 8.
I am not able to figure out, is there any run-time benefit of unit lambda expression or not?
I have copied following text from different sources which are too much confusing to me.
One answer is saying -
"lambdas do NOT create a new scope, they share the same scope as the
enclosing block/environment"
In the one blog -
"There are no runtime benefits of using lambda expressions, so I will
use it cautiously because I don’t mind writing few extra lines of
code."
From one other blog -
"one more benefit lambda expressions Sequential and Parallel Execution
Support by passing behavior in methods"
So there is lots off confusion for me.
Please help me to clear this so I can avoid keeping wrong approach in mind before digging more in to this stuff.
I have run following code and I can say the lambda expression in the following code is just replacement for the anonymous inner class and its running on main thread.
List<Integer> list = new ArrayList<>();
list.add(12);
list.forEach(V -> {
System.out.println(V);
});
Are we reducing any time complexity or space complexity?
“lambdas do NOT create a new scope, they share the same scope as the enclosing block/ environment” is an almost correct statement (they do create a new scope, but not the way inner classes do), but doesn’t have anything to do with runtime performance. This has to do with correctness of the code.
Within an anonymous inner class, identifiers may get resolved through the lexical scope, finding a match in the surrounding scope, or by inheritance, finding a match in the class hierarchy of the anonymous inner class. The rules for resolving identifiers in this scenario are complex and easy to confuse.
Further, the body of an anonymous class creates a new scope that allows to create variables having the same name as local variables of the surrounding context, shadowing these variables.
In contrast, a lambda expression works like other expressions within the context they are written in. They do not inherit any members from the functional interface they will get converted to, they can not create new variables shadowing existing local variables and even this and super have the same meaning as within the surrounding context:
JLS§15.27.2. Lambda Body
Unlike code appearing in anonymous class declarations, the meaning of names and the this and super keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).
So when you have the expressions x.foo(y) and () -> x.foo(y) within the same block, it will be obvious whether x and y will be the same x and y for both expressions and hence, it will be the same foo method in each case, which you can not say that simple for anonymous inner classes, as you have to analyze the entire inner class and its type hierarchy first.
This makes lambda expressions ideal for scenarios where you want to define a local function and, e.g. pass it to a method as a parameter, without even thinking about the actual interface being used. The interface itself does not influence the lambda expression beyond defining the functional signature.
But this also implies that there might be use cases of anonymous classes that can’t be covered by lambda expressions. But the purpose of lambda expressions isn’t to be a general replacement for anonymous inner classes.
When it comes to performance or easy of parallel processing, shmosel’s answer says it already. We can’t make such general statements without knowing which operation/problem we are looking at and which solutions we are actually comparing.
lambdas do NOT create a new scope, they share the same scope as the enclosing block/environment
I'm not sure what your point is with this quote.
There are no runtime benefits of using lambda expressions
Relative to what? Lambdas may perform better than anonymous classes, but neither will outperform simple control flow statements.
one more benefit lambda expressions Sequential and Parallel Execution Support by passing behavior in methods
Streams and lambdas enable declarative programming, which makes parallelization more accessible. So using lambdas may indirectly improve performance.
anonymous inner class is compiled into extra .class file, but lambda is translated dynamically. Here is a detailed post: How will Java lambda functions be compiled?

How do closures work in Java 7? [duplicate]

This question already has answers here:
Closure in Java 7 [closed]
(7 answers)
Closed 9 years ago.
I haven't worked with closures in java 7 as yet and was wondering how they work and what is their main advantage or best use case in utilizing them?
Update:
I should have done my homework better. Here is the Project Lambda site for JSR 335 : Lambda Expressions for the Java Programming Language. They are claiming clousures will be in Java 8. I'll have to look into it more to see if that is really the case.
Java 7 has no closures. They've been rumored for a long time, and they are apparently set to appear in Java 8. Of course, I have been promised a Ghostbusters reboot that has been rumored too.
However, you can fake closures with anonymous inner classes. But make no mistake, these aren't closures.
As for the benefits of closures, I can't put it any better than Stack Overflow legend #jaif from this post:
"You can see it as a generalization of a class.
Your class holds some state. It has some member variables that its methods can use.
A closure is simply a more convenient way to give a function access to local state.
Rather than having to create a class which knows about the local variable you want the function to use, you can simply define the function on the spot, and it can implicitly access every variable that is currently visible.
When you define a member method in a traditional OOP language, its closure is "all the members visible in this class".
Languages with "proper" closure support simply generalize this, so a function's closure is "all the variables visible here". If "here" is a class, then you have a traditional class method.
If "here" is inside another function, then you have what functional programmers think of as a closure. Your function can now access anything that was visible in the parent function.
So it's just a generalization, removing the silly restriction that "functions can only be defined inside classes", but keeping the idea that "functions can see whatever variables are visible at the point where they're declared". "

Java 8 lambda expression and first-class values

Are Java 8 closures really first-class values or are they only a syntactic sugar?
I would say that Java 8 closures ("Lambdas") are neither mere syntactic sugar nor are they first-class values.
I've addressed the issue of syntactic sugar in an answer to another StackExchange question.
As for whether lambdas are "first class" it really depends on your definition, but I'll make a case that lambdas aren't really first class.
In some sense a lambda wants to be a function, but Java 8 is not adding function types. Instead, a lambda expression is converted into an instance of a functional interface. This has allowed lambdas to be added to Java 8 with only minor changes to Java's type system. After conversion, the result is a reference just like that of any other reference type. In fact, using a Lambda -- for example, in a method that was passed a lambda expression as parameter -- is indistinguishable from calling a method through an interface. A method that receives a parameter of a functional interface type can't tell whether it was passed a lambda expression or an instance of some class that happens to implement that functional interface.
For more information about whether lambdas are objects, see the Lambda FAQ Answer to this question.
Given that lambdas are converted into objects, they inherit (literally) all the characteristics of objects. In particular, objects:
have various methods like equals, getClass, hashCode, notify, toString, and wait
have an identity hash code
can be locked by a synchronized block
can be compared using the == and != and instanceof operators
and so forth. In fact, all of these are irrelevant to the intended usage of lambdas. Their behavior is essentially undefined. You can write a program that uses any of these, and you will get some result, but the result may differ from release to release (or even run to run!).
Restating this more concisely, in Java, objects have identity, but values (particularly function values, if they were to exist) should not have any notion of identity. Java 8 does not have function types. Instead, lambda expressions are converted to objects, so they have a lot baggage that's irrelevant to functions, particularly identity. That doesn't seem like "first class" to me.
Update 2013-10-24
I've been thinking further on this topic since having posted my answer several months ago. From a technical standpoint everything I wrote above is correct. The conclusion is probably expressed more precisely as Java 8 lambdas not being pure (as opposed to first-class) values, because they carry a lot of object baggage along. However, just because they're impure doesn't mean they aren't first-class. Consider the Wikipedia definition of first-class function. Briefly, the criteria listed there for considering functions first-class are the abilities to:
pass functions as arguments to other functions
return functions from other functions
assign functions to variables
store functions in data structures
have functions be anonymous
Java 8 lambdas meet all of these criteria. So that does make them seem first-class.
The article also mentions function names not having special status, instead a function's name is simply a variable whose type is a function type. Java 8 lambdas do not meet this last criterion. Java 8 doesn't have function types; it has functional interfaces. These are used effectively like function types, but they aren't function types at all. If you have a reference whose type is a functional interface, you have no idea whether it's a lambda, an instance of an anonymous inner class, or an instance of a concrete class that happens to implement that interface.
In summary, Java 8 lambdas are more first-class functions than I had originally thought. They just aren't pure first-class functions.
Yes, they are first class values (or will be, once Java 8 is released...)
In the sense that you can pass them as arguments, compose them to make higher order functions, store them in data structures etc. You will be able to use them for a broad range of functional programming techniques.
See also for a bit more definition of what "first class" means in this context:
http://en.wikipedia.org/wiki/First-class_citizen
As I see it, it is syntactic sugar, but in addition with the type inference, a new package java.util.functions and semantic of inner classes it does appear as a first-class value.
A real closure with variable binding to the outside context has some overhead. I would consider the implementation of Java 8 optimal, sufficiently pure.
It is not merely syntactical sugar at least.
And I wouldn't know of any more optimal implementation.
For me Lambdas in Java 8 is just syntax sugar because you cannot use it as First class Citizen (http://en.wikipedia.org/wiki/First-class_function) each function should be wrapped into object it imposes many limitation when comparing to language with pure first class function like SCALA. Java 8 closures can only capture immutable ("effectively final") non-local variables.
Here is better explanation why it is syntax-sugar Java Lambdas and Closures

Why does java need type identifiers?

Since every class in java is a subclass of the Object, and variables in java are not objects themselves but instead are object references, why does java make type specification compulsory, when the Object type could be made implicit? The only time it seems necessary is when using the simple data types.
If a variable is of type Object, the compiler will not let you use the variable as any other type (unless you cast it).
This is called type safety.
For example:
Object str = "abc";
s.toUpppercase(); //Compiler error
Well. Java does it because.. this is how the language was defined.
this boils down to what was considered good practice when the language was designed (nearly 20 years ago), and also with complier ease of development.
Scala, a language which is closely related to Java (runs on the same JVM), does not require explicit type identifiers in most cases.
the downside is the scala compiler is much slower (for this among other reasons).
The answer is probably Java is an object oriented language used in large projects and created by reasonable company. Having such strong typing decrease amount of potential bugs, that you are able to remove on compilation level.
btw.
In the .NET product you have such thing as var x. But it can be used only locally in method body. But this is only a compiler sugar for developers. So Java is only a example of strong typed language.
Java is a strongly typed language. The types are needed to be able to compile code and validate types compatibility at compile time.
Weakly typed languages do not have types. For example when you say var x in JavaScript you just define variable. Then value of any type may be assigned there. This means that if for example your code has bug and you assign string to this variable and then try to divide this variable by 2 (y = x / 2) the script will just fail at runtime. Java will not allow you to compile such code.
There is the principle: the bug costs x during development, x*10 during QA and x*100 if it arrives to production. Compiler and strongly typed languages allow to decrease number of (stupid) bugs that arrive to QA and therefore make software development easier, faster and cheaper.

Categories

Resources