Java

Basics of Java

If you’re looking for a programming language that can allow you to write complex programs, Java is a great option. Its class-oriented and object-oriented nature is a great option for software development as well as its structure is designed to have as few implementation dependencies as it is possible. One of the many advantages of Java is the fact that it’s easy to learn and use.

Java is popular in diverse industries, including medical, financial, and even web-based development. Although it was initially developed for television programming, its popularity has been a major factor in its selection for web-based programming. Java has gone through several changes, and more users have contributed to the libraries and classes.

This article provides the basics of Java that would help the novel programmers of Java that are starting their careers with Java.

Basics of Java programming

This core section of the write-up contains the fundamental concepts that are mandatory that form the basics of Java.

Datatypes in Java

The datatypes in Java are categorized into two types:

– Built-in types: The built-in datatype is also known as a primitive data type and it can be of the following: double, long, float, integer, character, short, and integer.

– User-defined types: The user-defined datatypes are object, class, string array, and interface.

Variables in Java

Variables are memory locations where data types are stored, the following statement declares a variable x, and value 5 is assigned to it.

int x=5;

Classes, Objects, and Methods in Java

As Java is object-oriented programming so the building block of a Java program comprises of classes, objects, and methods. These terminologies are described here:

Classes and Objects: Anything written as Java code is kept inside a class. A Java class contains several methods and objects. It can be declared using the syntax mentioned below:

Access-Specifier class Class-Name { methods; objects; }

The Access-Specifier in the above syntax may be public, private, protected, and package. The Class-Name is user-defined and can be any term.

For instance, the following code creates a class named student and the access modifier is set to public. Moreover, the Name, id, and Grade are the objects of a class student.

 public class student {
      String Name;
      Int id;
      String Grade;
}

Methods: On the other hand, Methods (also known as Functions) are used to perform a specific operation and save it for later use in the code. The syntax to practice functions is described below:

 Access-Specifier Return-Type Method-Name (arguments)
  {
 Statements of code;
 }

The Access-Specifiers are the same as discussed in the above section and can be public, protected, private, and packaged. The Return-Type is the datatype of the value that is returned by the method(it can be void which means the method will not return any value).

Operators in Java

The following list of operators’ types refers to Java:

Arithmetic operators: The Arithmetic operations are assisted by these operators and the supported operators are +, -, *, /, and %(modulus operator).

Logical Operators: These operators include &&(AND), ||(OR) and !(NOT), additionally, they are known as binary operators.

Increment/Decrement Operators: The value of a variable is incremented or decremented using these operators and it can be a post/pre-increment/decrement.

Assignment Operators: These types of operators allow you to assign values to variables. The assignment operators supported by Java include =, /=, *=, +=, -=, and %=.

Comparison Operators: These operators are used to build a comparison between two variables/values, and they include ==(equal to), !=(not equal) <(less than), >(greater than), >=(greater than or equal), <=(less than or equal).

Conditional Statements in Java

The conditional statements are exercised when you want to evaluate several expressions based on a condition. You may use one of the below-mentioned conditional statements in Java:

If-else condition: If you are familiar with programming, then you may have experienced this conditional statement. There are two blocks in the if-else statement:

The if-block checks for the condition and is executed if the condition is true, and the else-if block comes into action if the condition specified in the if-block is false. Lastly, the else block is executed if all the above-stated conditions (if and else-if) are not met.

if (condition)
{
statements;
}
else-if (condition)
statements;
else
 statements;

Switch-case statement

An expression is matched against the multiple statements using a switch statement. The syntax of the switch case statement in Java is provided below:

switch (expression)
case a:
 statement
break;
case b:
 statement
break;
case c:
 statement
break;
default;
 statement;

The break is used to terminate the execution of other cases if the expression matches any case and the default keyword is used to print the statement if none of the cases matches the expression (kind of else).

Loops in Java

It is quite impossible to get the hundreds of repetitive outputs with less execution time using loops. Like other famous programming languages, Java does support several loops as described below:

For loop: The working mechanism of For-loop relies on the syntax provided below:

Firstly, the loop variable is initialized, after that the condition is set and lastly the increment/decrement to the variable is performed.

 For (initialization, condition, increment/decrement)
  {
Statements;
  }

While-loop: The functioning of the While loop is quite different from the For loop. Although, the factors(initialization, condition, increment./decrement) of a While loop are the same as For loop but they are exercised as shown in the syntax below:

initialization;
  While (condition)
  {
Statements;
   Increment/Decrement;
  }

Do-While loop: It works on the basis of syntax provided below. When the Do keyword is used, the code is executed at least once before checking the condition:

initialization;
  Do
  {
Statements;
   Increment/decrement;
  } While(condition);

The above stated basic terms of the Java language would enable you to have a sound grip even if you are at initial stages of practising Java.

Conclusion

Java is an object-oriented programming language with cross-platform support and has lesser dependencies than other languages. This article provides the basic concepts that are necessary and recommended to learn before getting into the Java language. The main idea behind this article is to teach you the basic building blocks that you need to write Java code.

About the author

Adnan Shabbir