What is Java? Getting Started with a Core Programming Language's Basics

There are many programming languages available today, such as C, C++, Python, and others. However, Java remains one of the most widely used languages in software development due to its key features like simplicity, platform independence, high performance, and strong security. These qualities make Java a top choice for developers across various applications. Let's learn more about Java.

What is java?

Java is a high-level, object-oriented programming language that is platform-independent because Java code can run on any device that has a Java Virtual Machine (JVM). It is widely used for building applications such as websites, mobile apps and desktop software.

Structure of java language

Every programming language has a structure that the compiler can understand. Similarly, Java has a specific structure, which is as follows:

Syntax:

package packagename;
import packagename;
class ClassName{
    variables;
    methods;
    public static void main(String[] args){
    statemnts;
    }
}

Package Declaration:
If the class belongs to a package, we need to declare the package name on the first line of the code. This is optional, and only one package can be declared in a Java file.

Import Statement:
If we need to use predefined or user-defined packages, we can write import statements after the package declaration and before the class declaration. Writing import statements is optional, and we can write multiple import statements if needed.

Class Declaration:
Every Java program must have at least one class, which is declared using the class keyword followed by the class name. The class contains variables, methods, and the main method where the program execution begins.

main() Method:

The main method is the starting point of every Java program. The Java Virtual Machine (JVM) always looks for the main method to start program execution. It is declared as public static void main(String[] args){} and contains the statements that will be executed when the program runs. we can write any number of statements in main() method.

Variables

A variable is a reference to a memory location used to store values that will be used later in the program.

Declaration of variables:

Declaring a variable involves specifying its data type and giving it a name. Variables are used to store values that can be used and changed during program execution.

Variable initialization:

Variable initialization involves specifying its data type, giving it a name, and also assigning a value to it.

Variable assignment:

Variable assignment means changing the value of a variable, and we can do variable assignment any number of times in a program.

//variable declaration
int number;
String name;

//variable initialization
int number = 10;
String name = "Sai";

//variable assignment
int number = 10;
number = 20;
number = 21;

The difference between variable declaration, initialization, and assignment is that declaration gives a data type and name, initialization gives a data type, name, and value, assignment means changing or updating the value of the variable.

Data Types in Java

In Java, data types are used to define what kind of data a variable can store. In java we have 3 types of data types,

Primitive data types are the basic or fundamental data types used to store a single value. In Java, we have a total of 8 primitive data types, which are divided into 4 categories:

  1. Integer Category:
    In this category, we can store any number (positive or negative) without a decimal point. It has 4 different sizes:

    1. byte 1 Byte(8 bits)

    2. short 2 Byte(16 bits)

    3. int 4 Byte(32 bits)

    4. long 8 Byte(64 bits)

  1. Floating-Point Category:
    In this category, we can store any number (positive or negative) with a decimal point. It has 2 different sizes:

    1. float 4 Byte(32 bits)

    2. double 8 Byte (64 bits)

  2. Character Category:
    In this category, we can store a single character enclosed in single quotes.

    1. char 2 Byte(16 bits)
  3. Boolean Category:
    In this category, we can store logical values like true or false (in lowercase).

    1. boolean 1 bit(true or false)

Derived data types are the predefined data types used to store multiple values of the same data type. In Java, we have arrays as derived data types, which help us store a collection of elements in a single variable and make it easy to manage and work with data efficiently.

User-defined data types are created by the user to store multiple values of the same or different types. In Java, we can create user-defined types using classes.

First program in java

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

In our program, first, we need to create a class and give it a name. After that, we need to save the file using the class name with .java extension. Inside the class, we write the main method, which is the starting point of the program. Then we can write our statements inside the main method to perform the desired actions.

Taking Input from the User in Java

To take input from the user in Java, we use the BufferedReader class which allows us to read data like numbers, strings, or any other input from the keyboard.

Syntax:

import java.io.*;
class Demo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //string  input
        String str = br.readLine();
        // byte input
        byte b = Byte.parseByte(br.readLine());
        // short input
        short s = Short.parseShort(br.readLine());
        // int input
        int i = Integer.parseInt(br.readLine());
        // long input
        long l = Long.parseLong(br.readLine());
        // float input
        float f = Float.parseFloat(br.readLine());
        // double input
        double d = Double.parseDouble(br.readLine());
        // char input
        char ch = (char) br.read();
        // boolean input
        boolean bool = Boolean.parseBoolean(br.readLine());
    }
}

For taking input from the user, first, we need to import the packages of io (Input/Output) classes. Then we need to create a BufferedReader object which helps us to take input from the keyboard.

To take String input, we use br.readLine().

To take integer input, we use the parsing method that converts the string to integer using Integer.parseInt(br.readLine). In the same way, we can use parsing methods to convert and take inputs for byte, short, long, float, double, and boolean values.

For reading character input, we need to use the typecasting method along with (char)br.read() to convert the input to a character.

In Java, comments are used to explain the code in simple words so that anyone reading the code can understand it easily. Comments are ignored by the compiler and they don’t affect the program execution.

Types of Comments in Java:

Single-line comment: We use // to write a single-line comment.

Multi-line comment: We use /* */ to write comments that span multiple lines.

Documentation comment: We use /** */for writing comments to generate documentation.

Java is a simple, powerful, and widely used language that helps us build different types of applications easily. By understanding basic concepts like structure, data types, variables, input, and comments, we can start writing Java programs.

0
Subscribe to my newsletter

Read articles from PEMMANA VISWESHWAR REDDY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

PEMMANA VISWESHWAR REDDY
PEMMANA VISWESHWAR REDDY