Most people who are new to programming were asking “I need to develop an Android App, But I don’t know Java. Can you teach me java quickly?”. I think most programmers heard these questions from new programmers. At least I had taught this to new developers multiple times. So, I’m writing this post for all the new programmers who have this problem. Also, others are welcome to check out. 馃槈
We will be discussing on following areas in this blog post. I will assume you have no experience in Java programming. But you have some experience in some kind of programming, I mean things like who a code can run (compiling/ running, etc.).

Topics

      1. What is JAVA?
      2. Getting Started…
      3. Hello World!
      4. Comments
      5. Data Types & Variables
      6. Casting
      7. Operators
      8. Math
      9. Boolean
      10. Conditional Blocks
      11. Loops
      12. Break/ Continue
      13. Arrays

What Is JAVA?

Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run JAVA. 
It is used in,
  • Mobile application development
  • Desktop application development
  • Web application development
  • Games
  • Database connections & much more!

So why do we need to learn JAVA?

  • Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)

  • It is one of the most popular programming languages in the world

  • It is easy to learn and simple to use

  • It is open-source and free

  • It is secure, fast, and powerful

  • It has huge community support (tens of millions of developers)

  • Java is an object-oriented language that gives a clear structure to programs and allows code to be reused, lowering development costs

  • As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa



Getting Started…

In this post, I am focusing on using Java with the following tools.
How to use Intellij IDEA & how to run the hello world program are explained in an early blog post. You can check it out at this link.

Hello World!

So let’s start with the Hello World program. To run this program, first, you need to create a java file name Hello.java. copy & run the following code to run the Hello World program.

Comments

Comments are used as references in your code, to identify what you did in code. Those comment lines will no run on your program. 
There are 2 types of comments. They are Single line comments & multiple-line comments. 
 
Single line comments are started with // mark. 
Multiple line comments start with /* mark end with */ mark. 
Always make sure you have commented correct lines or your code will not run properly if you accidentally commented a code line. Following is an example code for comments.

Data Types & Variables

Data Types

There are two main data types in JAVA. 
  • Primitive Data Types – data types byte, short, int, long, float, double, boolean, and char are known as primitive data types.
  • Non-Primitive Data Types – data types String, Array, and Classes are known as non-primitive data types.

What is a byte?

As you know, bit is known as 1 or 0 data. a byte is known as a data type with 8 bits. It can consider as a pack of 8 bits as shown below. 
So by getting some idea about byte data type, we can understand other data types with respect to byte data type.

Data types explained


Variables

Variables can consider as buckets that keep specific data of the above data types. You can create different types of variables as follows. Also, you can use them to save data/ modify data while running your program.


Casting

casting is most commonly used to convert one type of data into another type of data. It’s like exchanging money into a different currency. You can convert USD into Euro or Japan Yen or event for Indian Rupees. It doesn’t matter, even after you convert the money, you will have the same amount of the money(Consider we are not paying extra amount to money exchange bank). 

like that, we can convert different types of data into another type in java using casting. Keep in mind that, if you convert such data types, you may lose some data in some cases (such as casting double into Integer).


There are 2 types of castings.
  • Widening Casting (automatically) – converting a smaller type to a larger type size
    byte -> short -> char -> int -> long -> float -> double
  • Narrowing Casting (manually) – converting a larger type to a smaller size type
    double -> float -> long -> int -> char -> short -> byte

here is an example for Widening casting.

 

Operators

Operators are used to performing operations on variables & values such as given below.
following are the java operators. (this image was taken from the GeeksforGeeks website)
Basic Operators in Java - GeeksforGeeks 

Math

The Java Math class has many methods that allow you to perform mathematical tasks on your program.
Following are some examples of those tasks.

Booleans

When you are programming, sometimes you will need to use some binary data. For example, think about the following values.
  • On/ Off
  • Yes/ No
  • True/ Falls
  • 1/ 0
  • Available/ Not Available
To keep those kinds of data in your program, you can use boolean data types. Following is an example of how you can use boolean values.

Conditional Blocks

When running your program, you will have to check different conditions while running your program. Following are few conditions that commonly found in programs.

  • Less than: a < b

  • Less than or equal to a <= b

  • Greater than: a > b

  • Greater than or equal to a >= b

  • Equal to a == b

  • Not Equal to: a != b

For those kinds of conditions, we can use if/else if/ else condition or switch case conditional blocks.

If condition block

If/ else condition block

If/ else if/ else condition block

Shorthand If/ Else (Ternary Operator)

Switch Case

Loops

Loops are used to run repeated processors. There are While loop, Do/ While loop & For loops in Java. we will check individual loops separately.

While Loop

The while loop loops through a block of code as long as a specified condition is true.

Do/ While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Break/ Continue

Break

The break statement can also be used to jump out of a loop. Or break the switch-case.

Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Examples :

So those are the very basics things that you will need to know to write a simple java program. After understanding the above key components, you will be able to write java programs, and also a simple Android app.
But keep in mind that, knowing the above things does not mean you know everything about Java. There is some more stuff. I think with this blog post, you will be able to get a good initiative to Java or Android development.
Also, there are some good references for feather learning : 
(PS: I also used those books & site to making some examples for this blog post)

To catch this kind of small tutorials, and things I do for fun, don’t forget to subscribe to my BloggerYouTube & follow me on GitHub.
Do you face any issues while trying this? Let’s discuss in bellow…

***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *