Basic Introduction to Java | simple introduction to java 2023

Introduction to Java



*Features of Java

The primary objective of Java programming language creation was to make it portable, simple and secure programming language.

  1. Simple

  2. Object-Oriented

  3. Platform independent

  4. Secured

  5. Robust

  6. Architecture neutral

  7. Interpreted

  8. Multithreaded

  9. Distributed

  10. Protable Simple

Java is very easy to learn, and its syntax is simple, clean and easy to understand. Java syntax is based on C++. Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading.

Object Oriented:

Java is an object-oriented programming language. Everything in Java is an object.

Platform Independent

Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language.

On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security.

Robust

Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.

Secure

With java secure features it enable us to develop virus free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.

Multi Threading

Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.

Architectural Neutral

Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.

Portable

Java Byte code can be carried to any platform. No implementation dependent features. Everything related to storage is predefined, example: size of primitive data types


*Differences between Java and C++


page2image1365385104

Comparison Index

page2image1365388464
page2image1365389440 page2image1365389856

C++ Java

page2image1365364144 page2image1365364448

Platform- independent

C++ is platform-dependent.

Java is platform-independent.


Mainly used for

Operator Overloading

Compiler and Interpreter

Structure and Union

C++ is mainly used for system programming.

Java is mainly used for application programming. It is widely used in Windows- based, web-based, enterprise, and mobile applications.

Java doesn't support operator overloading.

Java uses both compiler and interpreter. Java source code is converted into bytecode at compilation time. The interpreter executes this bytecode at runtime and produces output. Java is interpreted that is why it is platform-independent.

Java doesn't support structures and unions.


Multiple inheritance

C++ supports multiple inheritance.

Java doesn't support multiple inheritance through class. It can be achieved by using interfaces in java.

C++ overloading.

supports operator


Pointers

C++ supports pointers. You can write a pointer program in C++.

java has restricted pointer support in java.

C++ uses compiler only. C++ is compiled and run using the compiler which converts source code into machine code so, C++ is platform dependent.

C++ supports structures and unions.

Call by Value and Call by reference

C++ supports both call by value and call by reference.

Java supports call by value only. There is no call by reference in java.

Thread Support

C++ doesn't have built-in support for threads.

Java has built-in thread support.

Virtual Keyword

C++ supports virtual keyword so that we can decide whether or not to override a function.

Java has no virtual keyword. We can override all non-static methods by default. In other words, non-static methods are virtual by default.


*The method overloading:


If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

  1. By changing number of arguments

  2. By changing the data type

Example given in notebook with explanation


*Abstract classes and methods


In object oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and to focus on essential details (functionality). It increases the efficiency and thus reduces complexity. In Java, abstraction can be achieved using abstract classes and methods.

Abstract class

A class is declared abstract using the abstract keyword. It can have zero or more abstract and non- abstract methods. We need to extend the abstract class and implement its methods. It cannot be instantiated.

Syntax for abstract class:

abstract class class_name { //abstract or non-abstract methods }

Abstract Method

A method declared using the abstract keyword within an abstract class and does not have a definition (implementation) is called an abstract method. When we need just the method declaration in a super class, it can be achieved by declaring the methods as abstracts.
Abstract method is also called subclass responsibility as it doesn't have the implementation in the super class. Therefore a subclass must override it to provide the method definition.

Syntax for abstract method:

abstract return_type method_name( [ argument-list ] );
Here, the abstract method doesn't have a method body. It may have zero or more arguments.

Example given in notebook with explanation


*Creating and importing packages:


A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.

Creating a Package

While creating a package, you should choose a name for the package and include a package statement along with that name at the top of every source file that contains the classes, interfaces types that you want to include in the package. The package statement should be the first line in the source file.

To define a package in Java, you use the keyword package.

package packageName;

Importing package:

Java has an import statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package.

The general form of import statement is:

import package.name.ClassName; // To import a certain class only import package.name.* // To import the whole package

For example,

import java.util.Date; // imports only Date class
import java.io.*; // imports everything inside java.io package





Post a Comment

0 Comments