Posts

Showing posts from January, 2017

Check If table exist or not

​To check if table exists or not , below is the code snippet-    import java.sql.Connection;    import java.sql.DatabaseMetaData;    import java.sql.ResultSet;     public class Main {        public static void main(String[] argv) throws Exception {          Connection c = null;          DatabaseMetaData dbm = c.getMetaData();          ResultSet rs = dbm.ge​​tTables(null, null, "employee", null);          if (rs.next()) {            System.out.println("Table exists");           } else {            System.out.println("Table does not exist");           }        }     } For Android internal memory, SQLLite -      public boolean doesTableExists(String tableName) {         int tableCount = 0;         Cursor cursor = db.rawQuery( "SELECT name FROM sqlite_master WHERE name='" +  tableName+ "' AND type='table'; ", null );         while(cursor.moveToNext()) {             tableCount++;             brea

Difference between dp, dip, sp, px

Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi). Screen density is grouped as low, medium, high and extra high. Resolution is the total number of pixels in the screen. dp: Density Independent Pixel , it varies based on screen density. In 160 dpi screen, 1 dp = 1 pixel. Except for font size, use dp always. dip: dip == dp. In earlier Android versions dip was used and later changed to dp. sp: Scale Independent Pixel , scaled based on user’s font size preference. Fonts should use sp. Always use dp and sp only. sp for font sizes and dp for everything else.

Read/Write Images

To read Image from Gallery- I worked on ImageButton to get Image from gallery: private void uploadImageClicked() {     new Thread(new Runnable() {         Intent intent = new Intent(Intent.ACTION_PICK);         public void run() {             openGallery(intent);         }     }).start(); } private void openGallery(Intent intent) {     intent.setType("image/*");       startActivityForResult(intent, 0); } The above piece of code open the Gallery to select the Image as desired. Here, we create an INTENT in order to access Gallery and hence, set the path as "image/*" and call startActivityForResult(intent, 0) so as to start the activity that utilize intent. To write Image from Gallery-     @Override     public void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK && null != data) {             uploadImageButton.setBackgroundResource(0);             Uri selectedImage = data.ge

Picasso Vs Glide

Below link gives great explaination between them- https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Multiple Inheritance is not supported in Java

Below diagram explains is called Diamond Problem because of the inheritance structure.       A foo()           / \          /   \ foo() B C foo()          \   /           \ /         D foo() Multiple inheritance does cause casting, constructor chaining,etc problems. Also, multiple inheritance leads to complexity. Hence, Java do not support multiple inheritance. But recently, in JAVA 8, this problem is handled in different way, that is, the compiler will throw exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.

Java 8 Features

Java 8 has really come out to be powerful where it has improved the performance with boilerplate code with respect to collections, interface. Below are some key points- forEach() method in Iterable interface default and static methods in Interfaces Functional Interfaces and Lambda Expressions Java Stream API for Bulk Data Operations on Collections Java Time API Collection API improvements Concurrency API improvements Java IO improvements Miscellaneous Core API improvements You can have a deeper understanding in below mentioned link- http://www.journaldev.com/2389/java-8-features-with-examples https://leanpub.com/whatsnewinjava8/read  (Along with Github link) https://www.javacodegeeks.com/2014/03/8-new-features-for-java-8.html

Parcelable Object - passing data between intents

It is explained in a simple and elaborated way under below link- https://guides.codepath.com/android/using-parcelable#passing-data-between-intents

Can main() Method be overloaded or overridden

Overloading- Any Method can be overloaded. And hence, you can overloading main() method. But JVM will always call the original main method, it will never call your overloaded main method. Overridding- Main is a static method and static method cannot be overridden in Java. Hence, its not possible to override main() method.

Can static method be overridden?

Static methods are bonded during compile time (static binding). And hence, it resolves to reference variable and not Object. And hence, you cannot override static method.     For e.g. -           Parent p = new Child();          p.callStaticMethod();    This method will invoke Parent class method and not Child class. 

Remove Duplicates from ArrayList

ArrayList implements List Interface and hence, allows duplicates. 1. The simplest approach to remove repeated objects from ArrayList is to copy them to a Set e.g. HashSet and then copy it back to ArrayList. This will remove all duplicates without writing any more code. 2. If original order of elements in ArrayList is important for you, as List maintains insertion order, you should use LinkedHashSet because HashSet doesn't provide any ordering guarantee. 3. Iterate over the ArrayList to check for duplicate and remove() it. Please Note- If you are using deleting duplicates while iterating, make sure you use Iterator's remove() method and not the ArrayList one to avoid ConcurrentModificationException.  In this tutorial we will see this approach to remove duplicates. Also, If you don't prefer converting List to Set than you can still go with copying data from one ArrayList to other ArrayList and removing duplicates by checking with ArrayList.contains() method.