Monday, December 25, 2017

Kotlin for Android


 
                              

Kotlin is an opensource language with its own community and documentation, Kotlin is the better fit for developing Android applications, Kotlin does not provide any restriction while using in android platform.




Major reasons why Google introduces Kotlin to android


Compatibility : Kotline applications can run on older versions of android without  any restrictions, also kotlin tool is fully supported into Android studio and compatible with android build system

Performance : Kotlin also has bytecode structure as like Java. With kotlin support for inline functions code using lambads often runs even faster than the same code written in java


Interoperability (Able to switch) : Kotlin is 100% interoperable with java, allowing using the all android libraries in kotlin application, this includes annotation processingm, so databinding and dagger work too.

Compilation Time : Kotlin support incremental compilation, so some additional overheads for clean builds.


Installing the kotline plugin



kotlin plugin is bundled with Android Studio 3.0, if you're using the earlier version you have to install manually,

Goto Preferances > Plugin > Browse Repositories > type Kotlin > Install 

Safer Code 

Write safer code and avoid NullPointerExceptions in your app.

var output: String
output = null   // Compilation error
==================================

val name: String? = null    // Nullable type
println(name.length())      // Compilation error
Readable and Concise 

Focus on expressing your ideas and wirte less boilerplate code.

// Create a POJO with getters, setters, equals(), hashCode(), toString(), and copy() with a single line:
data class User(val name: String, val email: String)

Lambdas

Use lambdas to simplify your code.


button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        doSomething();
    }
});
          


button.setOnClickListener { doSomething() }
          
Default and named arguements

Reduce the number of overloaded functions by using default arguments.
Call functions using named arguments to make your code more readable.


fun format(str: String,
           normalizeCase: Boolean = true,
           upperCaseFirstLetter: Boolean = true,
           divideByCamelHumps: Boolean = false,
           wordSeparator: Char = ' ') {
        
    }
==================================
// Call function with named arguments.
format(str, normalizeCase = true, upperCaseFirstLetter = true)
Say Goodbye to findViewById

Avoid  findViewById() calls in your code. Focus on writing on your logic with less verbosity.


import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // No need to call findViewById(R.id.textView) as TextView
        textView.text = "Kotlin for Android rocks!"

    }
}
   

Extend functionality without inheritance


Extension functions and properties let you easily extends functionality of classes without inheriting from them. calling code is readable and natural.


// Extend ViewGroup class with inflate function
fun ViewGroup.inflate(layoutRes: Int): View {
    return LayoutInflater.from(context).inflate(layoutRes, this, false)
}
==================================
// Call inflate directly on the ViewGroup instance
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = parent.inflate(R.layout.view_item)
    return ViewHolder(v)
}
 
100% Interoperable with Java

Add as tittle or as much of Kotlin as you want. Kotlin is a JVM language that's completely interoperable with Java.


// Calling Java code from Kotlin
class KotlinClass {
    fun kotlinDoSomething() {
        val javaClass = JavaClass()
        javaClass.javaDoSomething()
        println(JavaClass().prop)
    }
}
==================================
// Calling Kotlin code from Java
public class JavaClass {
    public String getProp() { return "Hello"; }
    public void javaDoSomething() {
        new KotlinClass().kotlinDoSomething();
    }
}
     



Building and Publishing the Kotlin application for android

This works same wa as in java. You can make a release of the application and sign in similarly to way what you do applications written in java

Kotlin has a rather small runtime file size :  the library is approximately 932KB. this means kotlin adds just a little to .apk file size.

Kotlin compiler produces bytecode, thus there really no-difference in look and feel of kotlin applications verses those written in java


Saturday, December 23, 2017

Monday, December 11, 2017

Data Structure and Algorithms in Java


                               

Data Structures :


It is a container of data. It let us store data more effectively. Execution and manipulation of data become's more easy and fast with the help data structures. It provides a bunch of ways to store data.

Large projects contain complex requirements, those projects need to be faster and user-friendly. There are many requirements those can't be implemented without data structures. Data structures provide many algorithms to do so.

Topics

  • Stack
  • Queue
  • Linked List
  • Sorting
  • Hash tables 
  • Trees
  • Application Development

Stack :
Is the linear data structure which follows the order in which the operations 

are performed .
The major operations which performed in Stacks are
  • Push : Used to insert the data
  • Pop : Used to access the data
  • isEmpty : Used to check whether the stack is empty

There are two ways to implement stacks 
1. Array
2. LinkedList


Sample example on Stack using array:
package datastr;
public class InStack {
private int [] stack;
private int top;
private int size;
public InStack(){
top=-1;
size= 50;
stack= new int[50];
}
public InStack(int size){
top=-1;
this.size=size;
stack =new int[this.size];
}
public boolean push(int item){
if(!isFull()){
top++;
stack[top]=item;
return true;
}else{
return false;
}
}
public int pop(){
try {
return stack[top--];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public boolean isFull(){
return(top== stack.length-1);
}
public boolean isEmpty(){
return(top== -1);
}
} ********************************************
package datastr;
public class Main {
public static void main(String[] args){
InStack inStack = new InStack();
if(!inStack.isFull()){
inStack.push(2);
inStack.push(4);
inStack.push(6);
inStack.push(9);
}
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
}
}

Queue :

Queue is an another type of data structure, the way of storing and retrieving the data from queue first in and first out.

The major operations performed on Queue are :


  • Enqueue : Adds the item to queue
  • Dequeue : Removes the item from queue
  • Front  : Item at first index 
  • Rear : Item at last index