Java for Android Tutorial - Part 1. Introduction

Me and My Motivation Before you go on to learn Java, let me welcome to my first series about Java for Android Development. I have be...

Image Introduction Java for Android kosalgeek.com oum saokosal
Me and My Motivation

Before you go on to learn Java, let me welcome to my first series about Java for Android Development. I have been teaching in Computer Science for my whole career over 10 years now including Android, Java, OOP with Java, Design Patterns, C#, VB.NET, Database Design, Data Warehouse, System Analysis and Design and E-Commerce. I am holding a Master's Degree in Information Systems graduated from Jeonju University and I am SCJP certified. I always wanted to write a book about Java and other programming languages. I used to complete writing a book about Adobe Photoshop CS2 in Khmer (Cambodian language) in 2006 and I thought to publish it. Writing a book was difficult but publishing it was even harder. So I decided not to publish it and the book was ended up at my University library. Now I don't need such a publisher for this book; a blog is enough for me. I hope you enjoy reading it and learn it well. Support me by subscribing it and sharing it.

By Oum Saokosal
Author of KosalGeek.

About this Tutorial

This tutorial is designed for learners who might be new to Java but have already familiar to a programming language. It is for readers who have already learned a language like C, C++, C#, or PHP and want to migrate to Java. If you are already a Java programmer, you can benefit from this tutorial a lot because it is not just about Java but the Java for Android. Of course, we will begin with Java but the two third of this course will cover about the Android App development.

Part 1. Introduction

Why Java, you might ask. In 2015, Java is the most popular programming language in the world. I am saying this but IEEE is. IEEE ranges Java number 1 followed by C, C++, and Python. Java was also ranged number 1 in TIOBE. If you are not convinced yet, well, Java is the official programming language for Android, the dominant Operating System (OS) for over 2/3 of smart phones, tablets, TVs, smart watches and smart wear glasses. You can, of course, program it in C#, VB.NET, and HTML5 with JavaScript for Android but those are not official. I am not saying the latter is bad but it is a different approach about which I would write in the near future.

What is Java?

Java is a cross-platform programming language originally developed by James Gosling at Sun Microsystem in 1995. It is now belong to Oracle Corp. The original intention of Java was Write Once, Run Anywhere. Yes, it worked well at its time from Windows to Linux to Mac OS to Web although some of its technologies are now not popularly used, e.g. Java Applet. There are 3 official editions of Java: Java SE (Standard Edition), Java EE (Enterprise Edition) and Java ME (Mobile Edition). Since Android, Java ME is considered dead whilst some of you might argue it is not. Java API is so huge that one never masters it. You can do everything in Java from a desktop application to Web (JSP) to embedded system. However, you don't have to be expert in Java to learn Android. What you need is some of Java SE.

What else is Java? Java is an object-oriented programming language. Some considers Java a pure OOP but actually it is not. But it is just OOP-ed enough. A modern language like Ruby or Swift may be well regard as a pure OOP language. Just like I said before, Java runs on most platforms including Windows, Linux, and Mac OS. It can do this because of its powerful Java Virtual Machine (JVM).

The JVM have to be installed before you can run a Java app. The Java code is never compiled to machine code (Binary code), instead to bytecode. For example, you write Java on Windows, compile it, and then you run it on Windows, Mac OS X and even Linux, and vice versa. Of course, it is not always worked that way since some UI themes, for example, are built for Windows or Mac OS X.

Why I am not talking about Android here? Actually, I said the JVM can run on most platforms, not all. When it comes to Android, it is another story. Android surely needs Java but it does not run on the JVM, instead it runs on the Dalvik VM,  the Virtual Machine for Android. It does compile to bytecode but after that it was converted again to run in the Dalvik VM. The JVM was not built to run on the small device like today's smart phone. That is why you can run your Java application on Android or other smart phone OS including iOS, Windows Phone. We will discuss more about it in the Android section later on in this tutorial.

The Java syntax is very similar to C, C++, PHP, and C# (actually, PHP and C# follow the Java syntax). Those of you who have learned one of those languages can catch up Java very easily. Java is garbage collected, which means you don't have to care when to clear out an object from memory. Java has no pointer. Java has a rich API for a collection, database, user interface, concurrent programming, networking and many more. Please note that some of these, e.g. JFrame, cannot be used in Android, though. Therefore, in this tutorial, I am going to talk only about the necessary Java libraries for Android.

Installing Java

Before anything else even before compiling the HelloWorld code, we have to have Java Development Kits (JDK) installed on your machine. Java is free and open source. So you don't have spend a penny for that. You can go to this official JDK website to download a suitable version and platform for your machine, for example, if you are running Windows 64bits, choose a JDK for Windows 64bits. And you do the same to Mac OS X or Linux. You can visit another official Java website to see how to install it.

Assume you have installed Java correctly. If you are not sure it is already installed, type the code below in your Command Prompt on Windows or Terminal on Mac OS X or Linux:
java -version
Say you installed Java version 1.8, then you should see something like this java version "1.8.0_60". If you don't see anything but Command Not Found error, you might wanna check your PATH. Check this link to configure your PATH.

Installing IDE

If talking about Java IDE, there would be controversy. Since Java is open source, Java has no an official IDE. One might argue Netbeans is the official IDE due to the creator from the same company. Well, people use Eclipse more than Netbeans. Recently, another Java IDE becomes more interesting: IntelliJ IDEA from JetBrains. Android moved from Eclipse-based Android Development Toolkits (ADT) to IntelliJ IDEA-based Android Studio because Eclipse was not enough for Android to make different release versions and was not supported Package Management like Gradle. The movement made a lot of impact to Android and Java societies.

So what is your decision? Netbeans, Eclipse, or IntelliJ IDEA? Because this tutorial is all about Java for Android, we should start with IntelliJ IDEA. You then won't be surprised to use the IDE in Android.

The IDE has two versions, Ultimate and Community. The latter version is free. So why not go for the Community from here? The installation is strait forward. First download it, install it, and done. Check out its official page for installation.

The latest version of IntelliJ IDEA for today (September 19, 2015) is 14.1.4. Please note that you might see a different interface at the time you read this post.

Starting Java in IntelliJ IDEA

1. Open New Project

Click on Create New Project on the Welcome screen > Choose Java > leave all default option, click Next > Next > Enter your project name, e.g. Hello1, in the Project name textbox > Finish
Image IntelliJ IDEA KosalGeek.com Oum Saokosal


2. Open New Java Class

On the left panel of your screen, under Project perspective > unfold Hello1 > right click on it src > choose New > choose Java Class > Enter a Java class name, e.g. HelloWorld.
Image Java HelloWorld by kosalgeek.com


Finally, you will see a Java code starting with public class HelloWorld. In there, you can write your Java code.

HelloWorld in Java

Up to this point, you might wanna run your first Java. Well, followed the traditional rule, here is HelloWorld in java:
public class HelloWorld {
 public static void main(String[] args) {
  System.out.println("Hello World");
 }
}

To run it, go to Run menu > Choose Run... or press Alt+Shift+F10 on Windows or Ctrl+Option+R on Mac. After running, you shall see the output:
Hello World

Java has some strict rules you wanna follow. First, your program starts in a Java file (.java extension) where its name must be matched to the class name inside. In this example, the class is HelloWorld so the file name must be HelloWorld.java. The class modifier cannot be private, i.e.
private class HelloWorld will have a compile error. We will talk about the modifier later in this course.

To display the output to console, you need to call System.out.println() inside the public static void main(String[] args) declared method. In this case, it is the starting point. A Java program does not always start from this main method. It depends on which platform or API you are working with. In Android, for example, the code start from onCreate method.

COMMENTS

Name

Android Java for Android
false
ltr
item
Android Programming Language Step by Step | KosalGeek: Java for Android Tutorial - Part 1. Introduction
Java for Android Tutorial - Part 1. Introduction
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFr7POhsnAcnND0YUmp0JiakP6NAWuoZ5fIbxBIxYKf0NATl5TXk_Q3j-sgL2RvF7hUBvttIZx7CUKlnRxhy8bdExwskBLO_eS6wNbBybU-nX3GapXTsi9L542pLNB8sS9t3u7PnGutk4/s320/Slide1.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFr7POhsnAcnND0YUmp0JiakP6NAWuoZ5fIbxBIxYKf0NATl5TXk_Q3j-sgL2RvF7hUBvttIZx7CUKlnRxhy8bdExwskBLO_eS6wNbBybU-nX3GapXTsi9L542pLNB8sS9t3u7PnGutk4/s72-c/Slide1.png
Android Programming Language Step by Step | KosalGeek
https://kosalgeek.blogspot.com/2015/09/java-for-android-tutorial-part1.html
https://kosalgeek.blogspot.com/
http://kosalgeek.blogspot.com/
http://kosalgeek.blogspot.com/2015/09/java-for-android-tutorial-part1.html
true
1541123312426289231
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy