Posts Tagged ‘java’

Java vs JavaScript- All You Should Know

Look around and you will find many web developers, but everyone would not tell you the difference between Java and JavaScript. A famous web developer quoted the difference between these two as- “Java is to JavaScript as Ham is to Hamster”. Basically, there are no links between both languages. Both the languages serve completely different roles in web development and programming. Let us move further and look for the differences and similarities between them-

WHAT IS JAVA?

Java is a general-purpose programming language. Applications built in Java are compiled to bytecode with the help of Java Virtual Machine (JVM). In web development, Java is a server-side language which is prominently used in developing mobile applications for Androids. Apart from this, it is present as a front-end language as Java applet but is not used due to security reasons. Java-vs-JavaScript--650x162

WHAT IS JAVASCRIPT?

It is a scripting language which is considered to be one of the three core components of the web that runs in the browser and makes web pages dynamic and interactive. It is employed by a majority of websites. It is rapidly growing as a server-side language along with Node.js.

JAVA VS. JAVASCRIPT: MAJOR SIMILARITIES

Though they are different languages, there are some differences worth considering-
  • Object-Oriented Programming (OOP)
OOP both languages access to techniques like inheritance, encapsulation, and polymorphism. Both languages work on the object and their relationship with other objects.
  • Front-End Development
Both languages are used for front-end development. JS can be embedded directly into HTML, implemented as a framework or library; Java can be used as a Java applet.
  • Back-End Development
These languages are used on the server-side. Java has been used to power back-end technologies like Apache, WebSphere, and etc; whereas JavaScript-powered servers use Node.js for the same purpose. java-vs-javascript

JAVA VS. JAVASCRIPT: MAJOR DIFFERENCES

Despite having some similarities, let us not forget that both the languages were developed to serve completely different purposes. Where Java was designed to develop standalone applications, JavaScript was built specifically to interface with technologies like HTML. Let’s check out the major differences between two-
  • Compiled vs. Interpreted
While Java is considered as compiled programming language, JavaScript is considered as interpreted scripting language. Java is compiled into bytecode as mentioned above, whereas JS can be interpreted directly by a browser.
  • Type checking
Java uses static type checking where variable type is checked at compile-time. While JavaScript uses dynamic type checking where type safety is verified at runtime. It is not required for the programmer to specify the type of any variable they create in the case of JavaScript. The advantage of type checking is that less time is required to execute a code using less memory if static type checking is chosen. The primary advantage of dynamic type checking is programmer productivity—you are free to assign types at your leisure.
  • Concurrency
It refers to the ability handle the execution of several instruction sequences at the same time. Java makes use of multiple threads to perform tasks in parallel, whereas JavaScript handles concurrency on one main thread of execution with an even loop, and a forking system called Node clustering.
  • Class-Based vs Prototype Based
Java follows class based inheritance i.e. top-down, hierarchical, a class-based relationship whereby properties are defined in a class and inherited by an instance of that class. While in JavaScript, inheritance is prototypical- all objects can inherit directly from other objects.

You should consider Java if your project involves-

  • Android Apps
  • Enterprise Software
  • Scientific Computing
  • Big Data Analytics
  • General Purpose Programming of Hardware
  • Server-Side Technologies like Apache, JBoss, Geronimo, GlassFish, etc.

You should consider JavaScript if your project involves-

  • Dynamic single page applications (SPAs)
  • Front-End technologies like jQuery, AngularJS, Backbone.js, Ember.js, ReactJS etc.
  • Server-Side technologies like Node.js, MongoDB, Express.js, etc.
  • Mobile App Development through PhoneGap, React Native, etc.ot
In a nutshell, both languages are meant for different purposes and there is no way you can use these terms interchangeably.

Kotlin- The Next-Gen Android App Development Language

Android app development is a major part of today’s technical and economical world. People use different languages for developing apps, though Java remains the most used language to develop logic. While running a Java application, the app is compiled into a set of instructions called Bytecode and runs in a virtual machine. In last few years, new programming languages have been introduced which also run on Java virtual machine. The reason behind using these languages is that the resulting app looks same for the virtual machine with simple code and some fixes that developers face while coding in Java. Image result for kotlin   Recently, one such language is introduced by JetBrains, known for IntelliJ IDEA (Android Studio is based on IntelliJ), called Kotlin. It is a statically typed programming language that runs on the JVM. It can be compiled to JavaScript source code. It has many other amazing features, and we will be knowing about them in this article.

Also Read:Swift 3.1: Swift Package Manager – Part 2

Why Kotlin instead of any other language?

Since Java is the only language most of the people use while developing the mobile apps, developers had no actual alternatives to Java app. Java comes with a lot of historical baggage and some issues. Java 8 solved some of those problems, and some were corrected in Java 10. But these two options also have loopholes. One of them is in order to use these two versions, one must have the minimum SDK to Android 24 to use Java 8 which is not an option for many developers. And Java 10 is even far from their reach. So, Kotlin was made to fill these gaps through model language. There are some core principles that Kotlin work with-
  1. Concise- Reduce amount of boilerplate code
  2. Safe- Avoid entire classes of error such as null pointer
  3. Smart- Make your code more readable and understandable
  4. Tool-friendly- One can choose any Java IDE or build from the command line
  5. Interoperable- Leverage existing frameworks and libraries of JVM, Android, and browser.

How to work on Kotlin?

Download the starter project. Extract and open the starter project in Android Studio. It is a simple app that allows users to search for books, book covers and share books with friends to explore Kotlin. Image result for kotlin vs java

It consist of three source code files, let us check it out-

MainActivity.java- an activity that displays the screen for searching and displaying a list of books. DetailActivity.java- an Activity that displays the book cover for the ID passed to it. JSONAdapter.java- a custom BaseAdapter that transforms a JSON object into a listview item. Build and run the project to see what you’re working with.

Also Read:How Swift 3.1 is different from earlier version? – Part 1

How to set up the environment of Kotlin?

Android Studio doesn’t have any idea of what to do with Kotlin. So the first thing you need to do is to install the Kotlin plugin and configure Kotlin in your project.

-Installing the Plugin

1. Go to Android Studio/ Preferences and Select the Plugins entry. kotlin for android 32. Click on Install JetBrains plugin on Plugins screen intro_to_kotlin_4 3. Search and select Kotlin from the list and click Install. intro_to_kotlin_5 4. When you are done with downloading and installing Kotlin, then you need to follow the prompts to restart the IDE. intro_to_kotlin_28

– Configure Kotlin in Project

Now the IDE knows what to do with Kotline, but your project app doesn’t, so you have to modify the project’s build configuration. How do modify the project’s build configuration? Let us check out- 1. Go to Tools-> Kotlin-> Configure Koltin in Project intro_to_kotlin_7 2. A Choose Configurator pop up will appear, select Android with Gradle from the drop down. intro_to_kotlin_8 3. On Configure Kotlin in Project popup, select the plugin version you want to use and click OK. Configure Kotlin in Project 4. These actions will make some changes to your build.gradle files.   build.gradle (Project: omg-android-starter): buildscript {  ext.kotlin_version = ‘1.0.3’ // 1  repositories {    jcenter()  }  dependencies {    classpath ‘com.android.tools.build:gradle:2.1.3’    classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version // 2    // NOTE: Do not place your application dependencies here; they belong    // in the individual module build.gradle files  } } allprojects {  repositories {    jcenter()  } }   build.gradle (Module: OMG Android):    apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ // 3 android {    compileSdkVersion 23    buildToolsVersion “24.0.2”    defaultConfig {        minSdkVersion 14        targetSdkVersion 23    }  sourceSets {    main.java.srcDirs += ‘src/main/kotlin’ // 4  } } dependencies {  compile ‘com.android.support:appcompat-v7:23.2.0’  compile ‘com.loopj.android:android-async-http:1.4.4’  compile ‘com.squareup.picasso:picasso:2.1.1’  compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version // 5 } repositories {  mavenCentral() }   Here is a summary of what this code is about-  
  1. Declares configured Kotlin version
  2. Declares classpath dependency artifact that contains the Kotlin Gradle plugin with the version declared earlier
  3. Specifies the use of Kotlin Android plugin via apply plugin command
  4. Defines that source files found in src/main/Kotlin will be compiled. Gradle will compile source Kotlin files found in src/main/java.
  5. Added the Kotlin Standard library as a compile-time dependency to the project.

Also Read:Ruby Metaprogramming

Click on Sync Now to build a project and run it! intro_to_kotlin_25 Nothing changed visually here, it just made your work bit easier!

Here are 10 reason why Kotlin is cool-

Image result for kotlin
  1. Kotlin is open source
  2. Full Java Interoperability
  3. Kotlin compiles to JVM bytecode or JS
  4. Data Classes in Kotlin
  5. Defaulted parameters
  6. Kotlin imposes no runtime overhead
  7. Null Safety in Kotlin
  8. Extension functions
  9. Swift is like Kotlin
  10. Kotlin wants you to write less code
Inputs & Source

Ruby on Rails vs Java Enterprise, Which one is Better?

Which is the best framework for your website? Have you ever given this thing a thought? Well, there are many languages which helps to design the framework for a website. There is Ruby on Rails, JEE, ASP.NET, Django and much more. But there are few languages that most of the programmers prefer. ASP.NET is a Microsoft platform which takes it straight to the risk zone. You cannot risk being locked into a Microsoft platform whereas other platforms are niche. So everything comes down to Java Enterprise (JEE) and Ruby on Rails (RoR).

Here are some differences through which you can figure out which one is better for your website:

#1 The focus of Ruby on Rails is on fast and agile development whereas Java Enterprise focus on flexibility and integration with enterprise IT.
#2 The common tasks in RoR are made very simple as compared to JEE. In JEE, even the simplest jobs need extraordinary efforts like to write XML configuration or auto-generate boilerplate code.
#3 RoR is used by start-up employees; JEE is used in the IT nerve-centres of investment banks. Image result for java vs ruby on rails #4 As a developer, RoR is pretty easy to learn, while the main problem a newcomer faces in JEE is to choose between huge number of different libraries and different ways to do the same thing. There is no particular libraries which are widely used.
#5 There are few people who work on Ruby on Rails, while every computer science student learns Java.
#6 In manageability of the code, JEE is better as it is easier to find out the things. Image result for java vs ruby on rails #7 Fortunately, both frameworks offer reasonable support for automated regression testing; Ruby on Rails probably a bit more so, because it relies primarily on automated tests (rather than a type system) to ensure things don’t fall apart horribly.

There are many aspects on which you can compare these two. Now the choice is yours. What do you think, which one would be the best for you? Tell us in comments!


Developing an android twitter client using twitter4j

Recently, we worked out an android twitter client for one for our esteemed clients. In this blog, I will try to explain on how to go about on building such an android application from scratch.
  1. Register your application on http://dev.twitter.com/apps/new.
  2. Please ensure that the application is of the browser type. Please fill anything in the callback url like http://twitter-test.com
  3. You will get the consumer key and the consumer secret once the application registration has been successful.
  4. Create an android project in eclipse(We use eclipse mostly, Please adjust the eclipse specific notes in this blog to adjust to your editor).
  5. Import twitter4j(http://twitter4j.org/en/index.html) as an external jar. This seems to be one of very few complete twitter libraries for java.
  6. We will try to post a status update to twitter. Lets get down to a bit of coding now. Place the following code where you want to trigger the twitter authentication process.
  7. 	Twitter twitter = new TwitterFactory().getInstance();
    	try
    	{
    
    	twitter.setOAuthConsumer(<consumer_key>, <consumer_secret>);
    	String callbackURL = "twitter-client:///";
    	rToken = twitter.getOAuthRequestToken(callbackURL);
    	startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(rToken.getAuthenticationURL())));
    	}
    	catch(IllegalStateException e)
    	{
    	// access token is already available, or consumer key/secret is not set.
    	if(!twitter.getAuthorization().isEnabled()){
    	System.out.println("OAuth consumer key/secret is not set.");
    	System.exit(-1);
    	}
    	}
    	catch(Exception e)
    	{
    	Toast.makeText(TwitterClient.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
    	}
    	
  8. Lets try to deciper the code here
  9. First we get the instance of the twitter factory and the set the oAuth consumer and secret with the keys that we got from the twitter site.
  10. Next we point the callbackURL to the intent filter that we would create in the application xml file. The intent filter is required so that the application catches the intent when the browser redirects back after successful twitter authentication. In my case i have added the following intent filter to my AndroidManifest.xml
  11. <intent-filter>
    <action android:name=”android.intent.action.VIEW” />
    <category android:name=”android.intent.category.DEFAULT” />
    <category android:name=”android.intent.category.BROWSABLE” />
    <data android:scheme=”twitter-client”/>
    </intent-filter>
    You can read more about intent filters on the android sdk site.
  12. Next we get the oauth request token and start the activity which will get the username/password and give authorization to our twitter application.
  13. When we return back from twitter, will place the following code on onResume activity callback:
    </li>
    @Override
    
    public void onResume() {
    
    super.onResume();
    
    Uri uri = getIntent().getData();
    
    if (uri != null) {
    
    oauthVerifier = uri.getQueryParameter("oauth_verifier");
    
    }
    
    try {
    
    Twitter tt = new TwitterFactory ().getInstance();
    tt.setOAuthConsumer(<consumer_key>, <consumer_secret>);
    AccessToken at = tt.getOAuthAccessToken(rToken, oauthVerifier);
    String token = at.getToken();
    String secret = at.getTokenSecret();
    // Post to twitter.
    Twitter t = new TwitterFactory().getOAuthAuthorizedInstance(<consumer_key>,
    <consumer_secret>,
    new AccessToken (token, secret));
    t.updateStatus("Test Android Twitter Client");
    
    } catch(Exception e) {
    Toast.makeText(TwitterClient.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
    }
    }
    
  14. Twitter has sent the verifier in the parameter of the return url, so we get it using getIntent().getData().getQueryParameter(“oauth_verifier”)
  15. Now when we have the verifier, we can get the access token and call the twitter4j method to update the status on twitter. twitter4j supports most of the twitter API and can be used for any operation which can be performed using twitter API.
Hope this helps someone. In case you have any queries, Please feel free to leave them in comments. We will be more than happy to help !! Edit: Here is the link to Sample Application. Please replace the variables OAUTH_CONSUMER and OAUTH_SECRET with the consumer and secret keys of the application that you have registered with twitter.