In this post, I will cover 7 basics of Java Programming to help you start coding in Java today. Before going ahead, I would recommend you go through our blog on How to Learn Java from Scratch, which ensures you have a clear learning roadmap in your mind.

I probably don’t need to tell you that without mastering basics in Java or any programming language, you cannot do coding efficiently.

Whether you are a recent graduate who is looking to learn a major programming language like Java or an experienced programmer who wants to increase the potential for future career growth, having a solid knowledge of basic concepts of Java is must.  Java is an easy programming language for one who has paid attention to the basics, however, it can be equally frustrating for one who tries to take the shortcut.

If any of the above points sounds familiar, I have a great news: There are 7 basics concepts of Java which can help you to start coding better right today. You can also find a list of Java online courses which will cover these basic concepts and their application in detail. Let’s get started with our discussion….

Fundamental concepts of Java

We will cover 7 Java Coding basics elements in the following manner:

  • Basic Program Syntax

  • Variables

  • Datatypes

  • Operators

  • Arrays

  • Loops

  • Conditional Statements

Let’s get started with the first basic. Let us consider a basic sample program in Java, “Hello World” program.

1. Basic Program Syntax:

public class MyfirstJavaProgram{
public static void main(String[]args)
{
System.out.println(“Hello World”);
}
}

Let’s see some basic syntax to write this program:

  • Case sensitivity: Java is case sensitive. Here, upper case letters and lower case letters are treated completely different.
  • Class name: For all class names the first letter should be in upper case.  If several words are used in class name, each inner word should start with upper case letter. For example, in the above program, the class name is MyFirstJavaProgram.
  • Method name: Each method name should start with a lower case letter. If more words are included in Method name, then each word should start with Upper case word. For example, method name: Public void myMethodName().
  • System.out.println(): It is used as a print statement. System is the class, out is the object PrintStream class, println() is the method of PrintStream class.

2. Variables:
A variable is used to store the data value. These are associated with data types. There are different types of variables as discussed below:

  • Local variable: A variable that is declared inside the body of the method is called as local variables.
  • Instance variable: A variable declared inside the class but outside the body of the method, is called instance variable.
  • Static variable: A variable which is declared as static is called static variable. A static variable cannot be local. It is not possible to create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variable is made only once when the class is loaded in the memory.

3. Data types:
Data types can specify different sizes and values that can be stored in the variable.

Different Data types present in Java

a. Primitive data types: There are totally eight primitive data types present in Java. These are:

  • Boolean: Boolean holds either true or false information. The default value is false.
  • Char: Char is used to store any character. The default value is ‘\0000’. The default size is 2 bytes.
  • Byte: Byte can hold the numbers in the range of -128 to 127. The default value is 0. The default size is 1 byte.
  • Short: Short can hold the numbers in the range of -32,768 to 32767. The default value is 0. The default size is 2 bytes.
  • Int: Int can hold the numbers in the range of -2,147,483,648 to 2,147,483,647. The default value is 0. The default size is 4 bytes.
  • Long: Long can hold the numbers in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The default value is 0L. The default size is 8 bytes.
  • Float: Float is mainly used to save memory in large arrays of floating point numbers. The default value is 0.0f. The default size is 4 bytes.
  • Double: Double is generally used as the default data type for decimal values, generally the default choice. The default value is 0.0d. The default size is 8 bytes.

b. Non-primitive data types: Non-primitive data types include Classes, Interfaces and Arrays.

4. Operators:
Java provides a rich set of operators. An operator is a character that represents an action. Let us see some of the different operators in Java:

  • Arithmetic operators
  • Assignment operators
  • Logical operators
  • Relational operators
  • Bitwise operators
  • Unary operators
  • Ternary operators.
Different Operators present in Java
  • Arithmetic operators: Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, and division.
Operator Description
+ Performs additional function
Performs subtraction function
* Performs multiplication function
/ Performs divisional function
% It acts as a reminder function
  • Assignment operator: Assignment operator assigns values to variables.
Operator Description
= It assigns values from right side operator to the left side operator
+= It is add AND assignment operator. It adds value of right side operand to the value of left operand and assigns the result to left operand
-= It is subtract AND assignment operator. It subtracts the value of the right side operator from left side operator and assigns the result to the left operand
*= It is multiple AND assignment operator. It multiplies the value of the right operator with the value of left operator and assigns the result to the left operand
/= Divide AND assignment operator. It divides the value of the left operator with the value of the right operator and assigns the result to the left operand
%= Modulus AND assignment operator. It takes the modulus using two operands and assigns the result to left operand
<<= It is left shift AND assignment operator
>>= It is right shift AND assignment operator
&= It is bitwise AND assignment operator
^= It is bitwise exclusive OR and assignment operator
|= It is a bitwise inclusive OR and assignment operator
  • Logical operator: Logical operators operate on boolean expressions. They are used in conditional statements and loops for evaluating conditions.

Logical operators in Java are &&, ||, !.

  • Relational operator: Relational operator determines the relationship between two operands. It checks whether the operands are greater than, less than, equal to and not equal to, etc.

Below table shows the relations between the two operands.

                       Operator                  Description
                            ==                     Equal to
                            !=                 Not equal to
                             >                 Greater than
                            <                    Less than
                           >=         Greater than or equal to
                           <=            Less than or equal to
  • Bitwise operators: Bitwise operators are used in Java that operates on bits and performs the bit-by-bit operation.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise compliment
<<  Left shift
>>  Right shift
>>>  Unsigned right shift
  • Unary operators: Unary operators are used to perform the operation on only one operand.
Operator Description
+ Unary plus
Unary minus
++ Increment operator
Decrement operator
! Logical complement operator
  • Ternary operators: Java ternary operator consists of three operands and it is used to evaluate Boolean expressions.
  • Shift operators: There are two shift operators in Java. They are:
    1. Left shift operators: The left shift operator << is used for shifting all the bits in a value to the left side of a specified number of times.
    2. Right shift operators: The Right shift operator >> is used for moving the left operands value to the right by the number of bits specified by the right operand.

5. Arrays: It is a common type of data structure which is a collection of similar type of elements that have a contiguous memory location.

Types of Arrays in Java:

  1. Single dimension array: It is a list of variables of the same type that can be accessed by a common name.
  2. Multi dimension array: It is a type of array where data is stored in matrix form.

6. Loops:

Different Loops statements present in Java

While writing programs, you sometimes need to execute a block of code multiple times in order to get the desired result. In such cases, loop statements allow to execute statements or group of statements multiple times which allows to build the software application efficiently. Java comes with several types of loop statements to handle looping requirements.

Loop Description
While loop While loop repeats a statement or a group of statements while a given condition is true. Before executing the loop body, it tests the condition
for loop For loop executes a sequence of statements several times which allow you to manage the loop variable
do while loop It works like do while loop, but it tests the condition at the end of the loop body.

7. Conditional Statements:

Conditionals Statements are used to execute a set of statements based on certain conditions. Some of the conditional statements are:

  • If statements: The if statements execute a certain section of code only if a test evaluates to true
  • Nested if statements: The nested if statements consist of an if statements inside another if statements.
  • If else statements: Here in if else statements if the condition is true, then the section that comes under if would execute else the section of code under else would execute
  • If else if statements: If else if statement is used to check when there is a need to check the multiple conditions
  • Switch statements: When the number of choices is more, and we may need to perform a different task for each choice then switch statements are used.

Conclusion

There are plenty of online Java courses right here at Simpliv which can help you to master the basics of Java programming language.

Question for you: Which basics from today’s post are you going to master first? Or, is there something you would like to add to the above list?

Either way, let me know by leaving a comment right below and do share the post on social media if you have enjoyed reading it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here