top of page
Writer's picturePushp Raj

Basic Introduction to Java Programming

Updated: Jan 30


Basic Introduction to Java Programming
Basic Introduction to Java Programming

Introduction To Java Programming


Firstly, Java is a class-based, object-oriented programming language with few implementation requirements. It is designed to allow application developers to write once and run anywhere (WORA), which means that compiled Java code can run on all systems that accept Java without requiring a recompilation.


Java was first introduced in 1995 and is widely used for developing desktop, online, and mobile applications. Java is popular for enterprise-level applications due to its simplicity, robustness, and security features.


Java was created by James Gosling at Sun Microsystems Inc in 1995 and was later purchased by Oracle Corporation. It is a straightforward programming language. Java makes it simple to write, compile, and debug code.


It helps developers in the development of reusable code and modular programs. Java is also a class-based, object-oriented programming language with few implementation requirements—a general-purpose programming language designed for developers to write once and execute anywhere.


Java code can be executed easily on any platform that supports Java. Java programs are compiled into byte code that any Java Virtual Machine may run. One can compare Java's syntax to C/C++ programming language.


Java Editions


Java editions differ in their capabilities. Java comes in three flavors:


  • Java Standard Editions (JSE) is used to construct desktop computer programs.

  • Java Enterprise Edition (JEE) is used to develop huge programs that operate on servers and handle high traffic and complex transactions.

  • Java Micro Edition (JME): This language is designed to create programs for tiny devices such as set-top boxes.


Java Data Types


The data types define the various sizes and values that can be stored in the variable. In Java, there are two kinds of data types:


  • Primitive data types include: In this section, Boolean, char, byte, short, int, long, float, and double are examples of primitive data types.

  • Non-primitive data types include: In this section, classes, Interfaces, and Arrays are examples of non-primitive data types.

Primitive Data Types in Java


Primitive data types are considered the building blocks of data manipulation in the Java programming language. These are the most fundamental data types in the Java programming language. Primitive data types are classified into eight types. They are as follows:

  • boolean

  • byte

  • char

  • short

  • int

  • long

  • float

  • double

Loops in Java


You can use a loop in Java to iterate over a code section numerous times. If the number of iterations is fixed, a for loop is preferred.


In Java, you will get three types of loops. They are as follows:

  • For Loop

  • For-each or Enhanced for Loop

  • Labeled for Loop


  • For loop


A simple “for” loop is equivalent to C/C++. We can set up the variable, check the condition, and increment/decrement the value. It is divided into four sections:


Initialization: Initialization is the first condition executed when the loop begins. We can either initialize the variable or use an already initialized variable here. It is a purely optional condition.


Condition: The second condition is executed each time the loop's condition is tested. It keeps running till the condition is met. It should return either true or false as a Boolean value. It is a purely optional condition.


Increment/Decrement: It either increases or decreases the variable value. It is a purely optional condition.


Statement: The loop's statement is executed each time the second condition is false.


Syntax:


for (statement 1; statement 2; statement 3) {


}


  • Nested For Loop


Nested for loops are when we have a for loop inside another loop. When the outer loop runs, the inner loop runs completely.

  • For Each Loop

In Java, the for-each loop explores an array or collection. It is simpler to use than a simple for loop because we don't have to increase the value or use subscript notation. It operates based on elements rather than the index. It returns each element in the defined variable one by one.


  • Labelled For Loop


Each Java for loop can be given a name. To accomplish this, we utilize the label before the for loop. It is useful when using nested for loops since it allows us to break/continue individual for loops.


  • While Loop

When the number of iterations is unknown but the ending condition is known, the while loop is employed. The loop is executed until the provided condition returns false. Because the condition is tested before entering the loop, the while loop is also an entry-controlled loop. The test condition is tested first, and then the control is looped.

Although the for loop is simple to use and create, there may be times when the programmer is uninformed of the number of iterations; this may be due to the user or the system. Thus, a while loop is utilized when the single iterating and/or terminating condition is known.

While loops are similar to if statements in that they are repeated.


Syntax:

Initialization;

while(Test Expression){

// Body of the Loop (Statement(s))

Updation;

}


  • Do-while Loop

The do-while loop is similar to the while loop, except that the condition is verified after the loop's body has been evaluated. As an example of an exit-controlled loop, consider the do-while loop.

The loop is run once, and the condition is checked for subsequent iterations. This loop is executed at least once, regardless of the test condition, and as many times as the test condition evaluates to true. It's the only loop with a semicolon.(;).

Do-While Loop: If the condition is false, the instructions inside the for and while loops are not evaluated. In some circumstances, regardless of the beginning condition of the test expression, it is desired that the loop-body execute at least once. This is when the do-while loop comes into play.


Syntax:


Initialization;

do {

// Body of the loop (Statement(s))

// Updation;

}

while(Test Expression);


  • Java Nested Loops

A nested loop is a loop statement that is included within another loop statement. In a nested loop, the complete inner loop is run for each iteration of the outer loop.

For example, if the outer loop is run m times and the inner loop is run n times, the total time complexity of the nested loop is m*n, which means that the inner loop is performed ‘n’ times for every ‘m’ iteration of the outer loop.

There are no restrictions on the order or type of loop that can be nested with the same type of loop or another type of loop. For example, a for loop can be included within a while loop and vice versa, and a do-while loop can be contained within another do-while loop.


Example:

while (condition) {

for (initialization; condition; update) {

do {

//statement of do-while loop

} while (condition);

//statement of for loop

}

//statement of while loop

}



Conditional Statements in Java


A conditional statement is a line of code that can be executed based on another condition. These clauses are known as decision or selection statements in Java. Java conditional statements are classified into three types:


1. If statement

2. If-Else statement

3. If-Else-If Ladder statement

4. Nested If statement

5. Switch statement


  • If Statement


Assume a condition is met if a statement is utilized to execute the program. A one-way selection statement is another name for it. When a condition is used, an argument is supplied, and if the condition is met, the appropriate code is performed; otherwise, nothing happens.

Syntax:

if(condition){

//code to be executed

}



  • If-Else Statement


An if-else statement is a control structure that chooses which assertions to select based on a set of criteria.


Syntax:

if(condition){

//code if condition is true

}else{

//code if condition is false

}



  • If-Else-If Ladder statement

An if-else statement is a control structure that chooses which assertions to select based on a set of criteria.


Syntax:


if(condition1){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

}

else if(condition3){

//code to be executed if condition3 is true

}

...

else{

//code to be executed if all the conditions are false

}


  • Nested If statement

Nested conditions are those that exist within another set of conditions—Java conditional statements.

Syntax:

if(condition){

//code to be executed

if(condition){

//code to be executed

}

}


  • Switch statement


Unlike the if-else expression, the switch statement can be executed in multiple ways. Furthermore, it compares the value of the expression to its cases by evaluating a few primitive or class types.


Syntax:

switch(expression){

case value1:

//code to be executed;

break; //optional

case value2:

//code to be executed;

break; //optional

......

default:

code to be executed if all cases are not matched;

}


Arrays in Java

An array in Java is an object that includes components of the same data type. Furthermore, array elements are kept in a continuous memory area. It is a data structure in which comparable elements are stored. A Java array can only hold a fixed number of entries.


In Java, arrays are index-based; the first element of the array is kept at the 0th index, the second element at the 1st index, and so on.


In contrast to C/C++, we may obtain the length of the array using the length member. The sizeof operator is required in C/C++.


In Java, an array is a dynamically produced class object. The Java array inherits the Object class, which implements the Serializable and Cloneable interfaces. In Java, we can store primitive values or objects in an array. Java, like C/C++, allows us to create single and multidimensional arrays.


Furthermore, anonymous arrays are available in Java, which is unavailable in C/C++.


Advantages


  • Code Optimization: It optimizes the code to obtain or sort data more efficiently.

  • Random Access: We can get any data placed at an index location using random access.


Disadvantages


  • Size Restriction: We can only store fixed-size elements in the array. It does not expand in size during operation. To address this issue, Java has a collection system that extends automatically. Instead of defining distinct variables for each item, arrays hold multiple values in a single variable.


To declare an array, use square brackets to define the variable type:


String[] bikes;


We've now declared a variable that holds a string array. To introduce values into it, put them in a comma-separated list inside the curly brace:

String[] bikes = {"TVS", "BMW", "Bajaj", "Honda"};

You could write: to make an array of integers:

int[] myNum = {10, 20, 30, 40};

Access an Array's Elements


You can get to an array element by using the index number.

This statement gets the first element in bikes' value:


Example

String[] bikes = {"TVS", "BMW", "Bajaj", "Honda"};

System.out.println(bikes[0]);

// Outputs TVS


Altering/Changing an Array Element


Refer to the index number to change the value of a certain element:


Example

bikes[0] = “Hero”;

Example:

String[] bikes= {“TVS”, “BMW”, “Bajaj”, “Honda”};

cars[0] = “Hero”;

System.out.println(cars[0]);

// Now outputs Hero instead of TVS

Length of an Array


The length property can be used to determine the number of elements in an array:

Example:

String[] bikes = {"TVS", "BMW", "Bajaj", "Honda"};

System.out.println(bikes.length);

// Outputs 4

Classes and Objects in Java


Java is an object-oriented computer language. In Java, everything is related to classes and objects, as well as their characteristics and methods. An automobile, for example, is an object in real life. The car contains qualities like weight and color and methods like drive and braking.


A Class functions similarly to an object function Object() { [native code] } or a "blueprint" for constructing objects.


Creating a Class


Use the term class to create a class. Here we are creating a main class with an x variable.

public class Main {

int x = 5;

}


Creating an Object


A class is used to build an object in Java. We've already created the Main class, so we can utilize it to create objects.

To create a Main object, first supply the class name, then the object name, and then use the keyword new. Here, we will make a "myObj" object and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

}

}

Classes and objects are one of the most important aspects of any programming language. Do go through the tutorials - Define Class and object in Java


Java Application Types


Java programming allows you to construct four different sorts of Java applications:


  • Standalone Applications: Java standalone apps use GUI components such as AWT, Swing, and JavaFX. These components include buttons, a list, a menu, a scroll panel, etc. Desktop alienations are another term for it.

  • Enterprise Applications: An enterprise application is distributed in nature.

  • Web Applications: Web applications are applications that operate on a server. We build web applications with JSP, Servlet, Spring, and Hibernate technologies.

  • Mobile Applications: Java ME is a cross-platform development environment for creating mobile applications that run on smartphones. Java is an Android app development platform.


Java Features


Java is considered a simple language because its syntax is straightforward, clear, and simple to grasp. Complex and unclear C++ concepts are either removed or re-implemented in Java. Java, for example, does not support pointer and operator overloading.


  • Object-Oriented: Everything in Java takes the form of an object. It implies that it contains some data and behavior. At least one class and object must be present in a program.

  • Robust: Java makes an effort to check for errors during runtime and compilation. It employs a powerful memory management technique known as a garbage collector. Its exceptional handling and garbage collection capabilities make it powerful.

  • Secure: Java is a secure programming language since it does not utilize explicit pointers, and all programs run on a virtual machine. Java includes a security manager that governs Java class access.

  • Platform-independent: Java guarantees that code may be written once and run anywhere. This byte code is platform-agnostic and can execute on any system.

  • Java is portable: Byte code is portable to any platform. There are no implementation-dependent features. Everything about storage is predetermined, including the size of primitive data types.

  • Java is a high-performance interpreted language: The Just-In-Time compiler in Java offers excellent performance.

  • Distributed: Java includes networking capabilities: It is intended for the Internet's dispersed environment because it supports the TCP/IP protocol. It can be accessed via the Internet. A distributed system is built using EJB and RMI.

  • Multi-threading: multi-threading is also supported by Java. It entails handling multiple tasks at once.


Conclusion


To summarize, Java is a popular programming language extensively used for constructing various applications, such as web and mobile applications, desktop software, games, and more. Understanding Java fundamentals such as data types, conditions, loops, arrays, and classes is critical for designing strong and efficient Java applications.


In Java, data types include raw data types like integers, floating-point numbers, and characters and reference data types like arrays and classes. Developers can use conditions and loops to build control structures that can execute certain code blocks based on certain conditions or iterate over code blocks numerous times.


Arrays hold collections of the same data type's elements, whereas classes are used to construct bespoke data types with their properties and methods. Developers may design efficient Java programs that fulfill the needs of their consumers and organizations by grasping these essential ideas.


41 views

Comments


Subscribe to Our Newsletter

Thanks for submitting!

bottom of page