0% found this document useful (0 votes)
2 views13 pages

Java Basics

Uploaded by

kishankumar22770
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Java Basics

Uploaded by

kishankumar22770
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Why Do We Need Programming Language :-

Programming languages are used to communicate instructions to a computer in a


form that humans can easily understand and write. These instructions are
processed by the Central Processing Unit (CPU) and bridge the gap between human
logic and machine-level operations, forming the foundation of all computational
task.

Writing Programs for the CPU

The CPU understands instructions only in binary form, made up of 0s and 1s.

• Writing programs directly in binary is very difficult for humans.

• High-level programming languages are easier to read and write.

• These languages are converted into binary so the CPU can execute them.

Programming Languages

Programming languages like C, C++, Java, Python, etc, allow humans to give
instructions to the CPU in a readable and structured way.

• Each programming language follows strict syntax rules.

• Even small syntax errors can stop a program from running.

• Programs must follow the correct structure to execute properly.

Compilers and Interpreters

High-level programming code must be converted into machine-readable binary


instructions before execution. This conversion is handled by tools known as
compilers and interpreters.

• Compiler: A compiler takes the entire code that you've written and converts
it into machine language all at once, creating an executable file that the
computer can run.

• Interpreter: An interpreter converts and executes the code one line at a time,
rather than processing the entire program at once
What is Java Doc tool and how to use it?

The Java Doc tool is a built-in documentation generator in Java that creates API
documentation in HTML format. It helps developers easily maintain and share structured
information about classes, methods, constructors, and fields. It helps developers:

• Create structured and professional documentation for classes, methods, and


fields.

• Maintain and share readable code explanations across teams or projects.

JavaDoc Comments

Before using the JavaDoc tool, you must include JavaDoc comments in your source code.
These comments provide descriptive information about your classes, methods,
constructors, and fields.

Syntax:

/**

* This is a JavaDoc comment.

*/

Structure of a JavaDoc Comment

A typical JavaDoc comment consists of two parts:

• Description: General explanation of the code element.

• Block tags: Provide structured metadata such as @param, @return, @author,


etc.

Generating JavaDoc Documentation

You can generate documentation from the command line or through an IDE.

Command Line Method: You don’t need to compile the .java file before generating
JavaDoc.

Use the following command:

javadoc file_name.java

or for an entire package:

javadoc package_name

After execution, a set of HTML files will be created. Open the [Link] file to view your
documentation.
Generating JavaDoc in Eclipse

• Go to Project -> Generate Javadoc.

• Choose the destination folder (default is in your workspace or C drive).

• Select the project or package to document.

• Choose specific classes and visibility level (public, protected, etc.).

• Click Next (optional: specify document title and other options).

• Click Finish to generate the documentation.

Generate JavaDoc:
To generate documentation for the above package, run: javadoc exa
This command will create a folder with HTML documentation files. Open
[Link] to view the API documentation in your browser.

Common JavaDoc Tags:

Below is a list of commonly used tags with their descriptions and syntax:

Tag Description Syntax

Adds the author of a


@author @author name-text
class.

Displays text in code font


without interpreting the
{@code} {@code text}
text as HTML markup or
nested javadoc tags.

Represents the relative


path to the generated
{@docRoot} {@docRoot}
document's root directory
from any generated page.

Adds a comment
@deprecated
@deprecated indicating that this API
deprecatedtext
should no longer be used.
Tag Description Syntax

Adds
a Throws subheading to
the generated @exception class-name
@exception
documentation, with the description
classname and
description text.

Inherits a comment from


Inherits a comment
the nearest inheritable
{@inheritDoc} from the immediate
class or implementable
surperclass.
interface.

Inserts an in-line link with


the visible text label that
points to the {@link
{@link} documentation for the [Link]#member
specified package, class, label}
or member name of a
referenced class.

Identical to {@link},
{@linkplain
except the link's label is
{@linkplain} [Link]#member
displayed in plain text
label}
than code font.

Adds a parameter with


the specified parameter-
@param parameter-
@param name followed by the
name description
specified description to
the "Parameters" section.

Adds a "Returns" section


@return @return description
with the description text.
Tag Description Syntax

Adds a "See Also"


heading with a link or text
@see @see reference
entry that points to
reference.

Used in the doc comment @serial field-


@serial for a default serializable description | include |
field. exclude

Documents the data


written by the @serialData data-
@serialData
writeObject( ) or description
writeExternal( ) methods.

Documents an @serialField field-name


@serialField ObjectStreamField field-type field-
component. description

Adds a "Since" heading


with the specified since-
@since @since release
text to the generated
documentation.

The @throws and


@throws class-name
@throws @exception tags are
description
synonyms.

When {@value} is used in


the doc comment of a {@value
{@value}
static field, it displays the [Link]#field}
value of that constant.
Tag Description Syntax

Adds a "Version"
subheading with the
specified version-text to
@version @version version-text
the generated docs when
the -version option is
used.

Introduction to JAVA :-
• Java is a popular programming language released in 1995, known for its platform
independence.

• A platform is defined as a combination of an operating system and architecture


(e.g., Windows + Intel).

• Before Java, programming required compiling code for specific platforms, making
it less flexible.

• Java compiles code into an intermediate form called Java bytecode, which can run
on any platform with a Java Virtual Machine (JVM) installed.

• JVM is platform-specific software that converts bytecode into machine-specific


instructions.

• Java is simpler than C/C++ as it has clear rules, no pointers, and does not allow
operator overloading.

• Java emphasizes security, automatic garbage collection, and better exception


handling.

• It is statically typed, requiring variable declarations, which can lead to faster


execution.

• Java is widely used, with around 3 billion devices running it and a large developer
community.

• Java is a high-level, object-oriented programming language developed by Sun


Microsystems in 1995. It is mostly used for building desktop applications, web
applications, Android apps and enterprise systems.
Features of Java :-

• Object-Oriented Programming (OOP): Java supports OOP concepts to create


modular and reusable code.
• Platform Independence: Java programs can run on any operating system with a
JVM.
• Robust and Secure: Java ensures reliability and security through strong
memory management and exception handling.
• Multithreading and Concurrency: Java allows concurrent execution of multiple
tasks for efficiency.
• Rich API and Standard Libraries: Java provides extensive built-in libraries for
various programming needs.
• Frameworks for Enterprise and Web Development: Java supports
frameworks that simplify enterprise and web application development.
• Open-Source Libraries: Java has a wide range of libraries to extend
functionality and speed up development.
• Maintainability and Scalability: Java’s structured design allows easy
maintenance and growth of applications.

Famous Applications Built Using Java

• Android Apps: Most of the Android mobile apps are built using Java.
• Netflix: This uses Java for content delivery and backend services.
• Amazon: Java language is used for its backend systems.
• LinkedIn: This uses Java for handling high traffic and scalability.
• Minecraft: This is one of the world’s most popular games that is built in Java.
• Spotify: This uses Java in parts of its server-side infrastructure.
• Uber: Java is used for backend services like trip management.
• NASA WorldWind: This is a virtual globe software built using Java.

What Can We Do with Java?

Java is used for:

• Mobile App Development: Android development using Android Studio.


• Web Development: Using frameworks like Spring, Spring
Boot, Struts, Hibernate
• Desktop GUI Applications: With libraries like JavaFX and Swing.
• Enterprise Applications: Backbone of banking, ERP and large-scale business
software.
• Game Development: With engines like LibGDX and jMonkeyEngine.
• Big Data Technologies: Like Hadoop and Apache Kafka.
• Internet of Things (IoT): Java can run on embedded systems and devices.
• Cloud-based Applications: Java is used in services on AWS, Azure and Google
Cloud.
• Scientific Applications: Java is used in tools that process large amounts of
scientific data.

Java is one of the most popular and widely used programming languages and platforms.
In this article, we will learn how to write a simple Java Program. This article will guide
you on how to write, compile and run your first Java program. With the help of Java, we
can develop web and mobile applications.

Implementation of Java Hello World

The below-given program is the most simple program of Java printing "Hello World" to
the screen. Let us try to understand every bit of code step by step.

public class HelloWorld {

// Your program begins with a call to main()

public static void main(String[] args)

// Prints "Hello, World" to the terminal window.

[Link]("Hello, World");
}

Understanding the Java Hello World Program

1. Class Definition

Every Java program must have at least one class. Here, the class is defined using
the class keyword:

public class HelloWorld {


// Statements go here
}

Note:

If the class is public, the filename must match the class name [Link]

2. main Method

In the Java programming language, every application must contain a main method as it
is the entry point of the application:

public static void main(String[] args)

• public: Allows JVM to access the method from anywhere.


• static: Method can run without creating an object.
• void: It doesn’t return any value.
• String[] args: Accepts command-line arguments.

3. [Link]()

This prints output to the console.

[Link]("Hello, World");

• System: Built-in class from [Link] package.


• out: Static member (PrintStream object) of System.
• println(): Method that prints to console and moves to the next line.

Steps to Implement a Java Program

Java is a platform-independent language that follows a two-step execution process:

• Compilation (source code -> bytecode)


• Execution (bytecode -> machine code via JVM)
1. Compilation in Java

Java source code (.java files) is compiled by the Java Compiler (javac) into Bytecode,
stored in .class files. This bytecode is platform-independent and ready to run on any
system with a JVM.

• Internally, compilation involves:


• Parsing: Converts code into syntax trees (AST).
• Entering: Populates symbol tables.
• Annotation Processing: Handles annotations.
• Attribution: Performs type checking and name resolution.
• Flow Analysis: Checks for variable use and reachability.
• Desugaring: Removes syntactic shortcuts.
• Generation: Produces .class files.

2. Execution via JVM

The .class files are executed by the Java Virtual Machine (JVM), which includes the
following stages:

2.1 Class Loader

Loads the main class and other dependencies into memory.

• Primordial Loader: Default system loader.


• Non-primordial Loader: Custom loaders for advanced control.

Class r = loadClass(String className, boolean resolveIt);

2.2 Bytecode Verifier

Checks that the loaded bytecode is safe to execute. It ensures:

• Variables are initialized


• Method signatures are correct
• Private access rules aren’t violated
• No stack overflows

2.3 Just-In-Time (JIT) Compiler

Converts bytecode into native machine code at runtime for faster execution.

You might also like