Other Parts of This Series:
- Part 1: Java Crash Course - Part 1 (Before Start)
- Part 3: Java Crash Course - Part 3 (Object Oriented Programming)

Java Programming Language (Photo Credit: Orient Software)
N.B: Please read the following concepts for better understanding before continue reading this article.
☛ Programming & Fundamental Concepts of Programming
☛ Data Types, Variable, Operator
☛ Setting up the environment: Read this link and follow the instruction
Basic Syntax of Java
I try to keep the things simple enough as much as possible. Without any outside discussion I only mention the main focus point. So lets start..
Skeleton:
As java is a object oriented programming language so everything here is considered as a combination of class and object. In the above code snippet we can see the basic skeleton of a java program. Here in -
- Mark (0) - This is the importing of package or library.
- Mark (1) - This is the starter point of the program.
- Mark (2) - This is main function and the entry point of program execution.
- Mark (3) - A basic statement which print Hello Java in console.
*** Java 25 don’t need the whole class and static void main() method as starting point. We can just write the statement and Java consider them as part of main() ***
| |
Comments:
Like other programming language java also supports single line and multi line comments.
Single line comments starts with “//” and multi line comments “/* comments */”.
Example:
Data Type

Java Programming Language (Photo Credit: javaTpoint)
There are two types of data types in Java:
- Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
- Non-primitive data types: The non-primitive data types include classes, interfaces and arrays.
Variable:
In programming variable is like a container which can holds data of different types. As java is a strongly typed language so we need to specify the type when declare a variable.
Example:
| |
Operators:
There are many types of operators in Java which are given below:
- Unary Operator, (++, –)
- Arithmetic Operator, (+, -, *, /, %)
- Shift Operator, (», «)
- Relational Operator, (<, >, <=, >=, !=, ==)
- Bitwise Operator, (|, &, ^)
- Logical Operator, (||, &&)
- Ternary Operator and (? :)
- Assignment Operator. (=)
Example:
| |
Let’s Practice:
The best way of practicing programing is solving programming problem. Lets solve the problem LeetCode 371. Sum of Two Integers by using the knowledge that we learn till now.
Problem says that given two integer a and b, calculate their sum.
Solution:
Condition (if-else and if-else if-else):
Java if-else and and if-else if-else or if-else ladder syntax is similar to C++/C#.
Example:
Please note: infinite level of nesting of if else or else if ladder is support by the java program.
Example:
| |
Condition (switch case):
Java switch case syntax is also similar to C++. The difference between if else and switch condition is, if condition can check the all relational condition but, switch can only check equal with case value. You can write the nested switch statements also.
Example:
| |
Java support switch expression in its latest release.
Example:
Break statements: The switch case example has break statements. Like its name suggests, a break statement breaks an execution block so that the below code of that block will be ignored. So if the program encounters a break statement, then it immediately breaks that block.
Array:
Arrays are non-primitive data types in Java. An array is nothing but a collection of similar types of data. That means when we need more than one similar and related data, then we should use the array. Arrays are being declared using [ ]. Also, it can be one-dimensional and multi-dimensional. Arrays can be declared in 3 ways.
Example:
| |
Loop:
Java supports generally 4 types of loop. They are:
- While loop
Example:
- Do…While loop
Example:
- For loop
Example:
- For…Each loop
Example:
Continue statements: Continue also to ignore the execution of the existing below code of a block, but unlike break, continue its executions for the next iterations.
Function or Method:
A Java function, also known as a method, is a block of code or a collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the reusability of code. The declaration signature of a method is as below:
Lets write a method for finding maximum value from a array.
Example:
| |
Lets Practice:
The best way of practicing programing is solving programming problem. Lets solve the problem LeetCode 1704. Determine if String Halves Are Alike by using the knowledge that we learn till now.
Here problem says, return true if first half and the second half of the string contains equal number of vowel otherwise return false.
Solution:
| |
Insha Allah, in the upcoming part, I will try to share the Object Oriented Programming of Java. Until than, may Allah keep you healthy and happy.