Core Java

Core Java is a popular programming language that was first developed by Sun Microsystems and is now possessed by Oracle Corporation. It’s an object-acquainted language that’s used for developing a wide variety of operations, from mobile apps to enterprise-position systems.

One of the crucial benefits of Java is its platform independence. This means that law written in Java can be run on any system that has a Java Virtual Machine( JVM) installed, anyhow of the underpinning operating system.

core java
core java

Introduction

Java is a programming language and platform. Java is a high-level, powerful, object-oriented, and secure programming language.

Java was developed in 1995 by Sun Microsystems (now a subsidiary of Oracle). James Gosling is known as the father of Java. Prior to Java, it was called Oak. Because Oak was already a registered company, James Gosling and his team changed the name from Oak to Java.

Operators

Operators in Java are symbols used to perform actions. Example: , -, *, / etc.

Java has many types of operators:

  • Unary Operator,
  • Arithmetic Operator,
  • Shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.

Data Types

Data types define the different sizes and values ​​that a variable can store. There are two types of data types in Java.

  1. Primitive data types: Primitive data types include boolean, char, byte, short, int, long, float, and double.
  2. Non-default datatype. Non-primitive data types include classes, interfaces, and arrays.

Java Variables

Variables are containers that hold values ​​while a Java program is running. A data type is assigned to the variable.

A variable is the name of a memory location. There are three types of variables in Java: local, instance and static.

There are two types of data types in Java:

  • primitive data types
  • non-primitive data types.

Method in Java

Generally, a method is a way to do something. Similarly, a method in Java is a set of instructions that perform a specific task. It provides code reusability. You can also easily modify your code using methods. In this section, you will learn what a method is in Java, the types of methods, how to declare a method and how to call a method in Java.

Class in Java

In object-oriented programming, classes are the basic building blocks. They can be defined as templates that describe the data and behaviour associated with class instance creation. Creating an instance of a class consists of creating objects (variables) of this class, which can be used to access the class’s member variables and methods.

Classes are also referred to as logical templates for creating objects with common properties and methods.

Multithreading in Java

Multithreading in Java is the process of running multiple threads at the same time.

A thread is a lightweight subprocess that is the smallest unit of processing. Multiprocessing and multithreading are used to achieve multitasking.

However, we are using multithreading, not multiprocessing, because the threads share a common memory area. It saves memory by not allocating a separate memory area, and context switching between threads takes less time than processes.

I/O Java

Java input/output (input and output) is used to process input and receive output.

Java uses the concept of threads to speed up I/O operations. The java.io package contains all the classes needed to work with input and output.

You can perform file processing in Java using the Java I/O API.

Java Libraries

Java is one of the most widely used programming languages. Java provides a rich set of libraries, the Java standard library is very powerful and includes libraries such as Java. lang, java. util, java. math, etc. Java provides over a thousand libraries in addition to the standard library. Here are some more useful and popular libraries.

  1. Java Standard libraries
  2. Apache Commons
  3. Jackson
  4. Maven
  5. Google-json
  6. Log5j and Slf4j
  7. JUnit
  8. Google Guava
  9. JAXB
  10. HTTP Libraries

String Handling

String handling refers to the process of manipulating and working with strings, which are a sequence of characters in a specific order. String handling is a fundamental concept in computer programming and is used extensively in various programming languages.

Some common operations that can be performed on strings include:

Some common operations are performed on strings including.

  1. Concatenation.
  2. Comparison.
  3. Substring extraction.
  4. Searching:.
  5. Replacing.

I/O exploring JAVA

In Java, I/O (input/output) operations involve reading and writing data to and from files or other input/output streams. There are several classes and interfaces available in Java’s standard library for performing I/O operations.

The core classes for input and output operations are the following:

  1. InputStream.
  2. OutputStream.
  3. Reader.
  4. Writer.

Java Networking

Java networking is the concept of connecting two or more computing devices together so that they can share resources.

Java socket programming provides the ability to exchange data between different computing devices.

Advantages of Java Networking

  1. Sharing resources
  2. Centralize software management

Java Applet Classes

In Java, an applet is a small program that is intended to be executed within a web page by a Java-enabled web browser. Applets are written in Java and have a specific set of rules and restrictions to ensure they are safe to run within a web page. Applets are defined by Java classes that extend core Java. applet.Applet class.

Here are some of the most commonly used classes in Java applets:

  1. java. applet.Applet.
  2. java.awt.*.
  3. javax.swing.*.
  4. java.net.*.
  5. java.io.*.
  6. java.util.*.

Event Handling

A change in the state of an object is called an event. For example, clicking a button, dragging a mouse, etc. The core java. awt. event package provides many event classes and listener interfaces for handling events.

Introduction to AWT

AWT (Abstract Window Toolkit) is a set of Java classes that provides the foundation for creating graphical user interfaces (GUIs) in Java. It is a part of the core Java Foundation Classes (JFC) and is designed to provide a common set of tools for building GUIs across different platforms.

AWT provides a set of pre-built GUI components such as buttons, text fields, labels, panels, frames, and other graphical elements that can be used to create rich and interactive user interfaces. It also provides a powerful event-handling mechanism to respond to user interactions with the GUI components.

Java AWT Graphics

Graphics is an abstract class provided by Java AWT that is used to draw or draw components. It consists of various fields containing information such as the component to be drawn, font, colour, XOR mode, etc., and how to draw various shapes in the GUI component. Graphics is an abstract class and cannot be initialized directly. Objects of child classes can be obtained in two ways:

  1. Inside the paint() or update() method.
  2. The geographic() method

AWT Controls

Each user interface considers three main aspects:

  1. UI elements: These are the main visual elements that users will ultimately see and interact with. GWT provides a list of common elements that are widely used, from simple to complex, and will be covered in this guide.
  2. Layout: Defines how user interface elements are arranged on the screen and provides the final look and feel of a graphical user interface (GUI). We will cover this in the Layout chapter.
  3. Behavior: An event that is fired when a user interacts with a UI element. We will cover this in the event handling chapter.

Layout Managers

LayoutManagers are used to arrange components in a specific way. A Core Java Layout Manager helps control the positioning and sizing of components in a GUI form. LayoutManager is an interface implemented by all layout manager classes. I have the following class representing a layout manager.

1. java. awt.BorderLayout
2. java. awt.FlowLayout
3. java. awt.GridLayout
4. java. awt.CardLayout
5. java. awt.GridBagLayout
6. javax. swing.BoxLayout
7. javax. swing.GroupLayout
8. javax. swing.ScrollPaneLayout
9. javax. swing.SpringLayout etc.

Additional Packages

In Core Java’s AWT (Abstract Window Toolkit), there are several additional packages that provide additional functionality beyond the core classes and interfaces provided by the AWT package. Here are some of the additional packages in AWT Java:

  1. java. awt. colour.
  2. java.awt.font.
  3. java.awt.geom.
  4. java.awt.image.
  5. java.awt.print.
  6. java.awt.datatransfer.
  7. java.awt.event.

Software Development Using Java

Java is a popular language to use for software development. Then are some introductory ways to follow when developing software using Java

  1. Set up your development environment.
  2. Define your requirements.
  3. Design your application.
  4. Write your code.
  5. Test your code.
  6. Debug your code.
  7. Deploy your application.
  8. Maintain and update your application.

Core Java Bean

A core JavaBean is a Java class that should follow the following conventions:

  1. It should have a no-arg constructor.
  2. It should be Serializable.
  3. It should provide methods to set and get the values of the properties, known as getter and setter methods.

Why use JavaBean?

According to the official Java documentation, it is a reusable software component. A component encapsulates many objects into a single object so that you can access those objects from multiple places. It also provides ease of maintenance.

Java Swing

Java tutorial Swing is part of the Java Foundation Classes (JFC) used to create windowed applications. It is based on the Abstract Windowing Toolkit (AWT) API and written entirely in core Java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

Java Servlets

It uses servlet technology to create web applications (located on the server side and generating dynamic web pages).

Servlet technology is powerful and extensible thanks to the Java language. Prior to Servlets, the Common Gateway Interface (CGI) scripting language was a common server-side programming language. However, this technique had many drawbacks. We have discussed these drawbacks below.

Migrating from C++ to Java

Migrating from C++ to Java can be a challenging but worthwhile process. Java is a high-level, object-oriented language that is popular in enterprise applications, web development, and mobile app development. Here are some key points to consider when migrating from C++ to Java:

Application of core JAVA

Core Java is a versatile programming language that can be used for a variety of applications. Here are some common Java applications.

  1. Web development.
  2. Mobile development.
  3. Desktop applications.
  4. Enterprise applications.
  5. Games.
  6. Embedded systems.

Java Programs

get Graphics() method

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
  
public class MyFrame extends Frame {
    public MyFrame()
    {
        setVisible(true);
        setSize(300, 200);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
    public void paint(Graphics g)
    {
        System.out.println("painting...");
    }
  
    public static void main(String[] args)
    {
        MyFrame f = new MyFrame();
        Graphics g = f.getGraphics();
        System.out.println("drawing...");
        g.drawRect(100, 100, 10, 10);
        System.out.println("drawn...");
    }
}

Inside paint() or update() method

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
  
public class MyFrame extends Frame {
    public MyFrame()
    {
        setVisible(true);
        setSize(300, 200);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
    public void paint(Graphics g)
    {
        g.drawRect(100, 100, 100, 50);
    }
  
    public static void main(String[] args)
    {
        new MyFrame();
    }
}