1. Java Introduction

1. Java Introduction

Core Java

Core Java Programming

??Basics of programming

  • JDK component and installation
  • Sample program – compilation and execution
  • Variables?
  • Methods/functions
  • Control statements
  • Pattern Programming

Object Oriented Programming

  • Class & Object
  • Blocks
  • Constructor
  • Composition
  • Inheritance
  • Method overloading & and method overriding
  • Type casting
  • Polymorphism
  • Abstract class
  • Interface
  • Abstraction
  • Encapsulation
  • Packages
  • Java bean class
  • Singleton pattern

Java Library

  • Object class
  • String class
  • Arrays
  • Collection API (Data structure)
  • Exception handling
  • File handling
  • Threads
  • Reflections?

Introduction

What is Java?

  • Java is a programming language and a platform.
  • Java is a general-purpose, high-level programming language developed by Sun Microsystems.
  • (Java is a computer programming language. It enables programmers to write computer instructions using English-based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute.)

What is a programming language?

???- A programming language is a formally constructed language designed to communicate instructions to a machine, particularly to a computer

??- Programming language is used to control the behavior of a machine.

?? Program- A program is a set of instructions given to a computer to control behaviors.

A program is made up of statements.

Programming languages are classified into 3 types

Low-level language?

  • Language which is understood by the machine
  • Low-level language is a programming language that deals with a computer's hardware components and constraints. It has no or a minute level of abstraction in reference to a computer and works to manage a computer's operational semantics.?
  • Low-level language may also be referred to as a computer’s native language.?
  • Low-level languages are designed to operate and handle the entire hardware and instructions set architecture of a computer directly.?
  • Low-level languages are considered to be closer to computers. In other words, their prime function is to operate, manage, and manipulate the computing hardware and components. Programs and applications written in low-level language are directly executable on the computing hardware without any interpretation or translation. Machine language and assembly language are popular examples of low-level languages.?

High-level language

  • Programming languages are more understandable by humans and it is a more natural language, elements, easier, making the process of developing programming simpler.
  • High-level languages are designed to be used by the human operator or the programmer. They are referred to as "closer to humans." In other words, their programming style and context is easier to learn and implement, and the entire code generally focuses on the specific program to be created.
  • High-level language doesn’t require addressing hardware constraints to a greater extent when developing a program. However, every single program written in a high-level language must be interpreted into machine language before being executed by the computer.?
  • BASIC, C/C++, and Java are popular examples of high-level languages.?

Assembly level language:-

  • It is also called low-level language.?
  • Assembly language is the most basic programming language available for any processor. With assembly language, a programmer works only with operations that are implemented directly on the physical CPU.?
  • Assembly languages generally lack high-level conveniences such as variables and functions, and they are not portable between various families of processors. It has the same structures and set of commands as machine language, but it allows a programmer to use names instead of numbers. This language is still useful for programmers when speed is necessary or when they need to carry out an operation that is not possible in high-level languages.

Machine Language:-

  • This is the only language a microprocessor can process without any previous transformation.

Platform-?

Any hardware or software environment in which a program runs is known as a platform.

Since Java has its own runtime environment (JRE) and API. It is called a platform.

Where it is used?

There are many devices where Java is currently used. Some of them are as follows:

  1. Desktop applications such as Acrobat Reader, media player, antivirus, etc.
  2. Web applications like irctc.co.in etc.
  3. Enterprise applications like banking applications.
  4. Mobile.
  5. Embedded systems.
  6. Smart card.
  7. Robotics.
  8. Games etc.?

Types of Java Applications

There are mainly 4 types of applications that can be created using Java programming.

  1. Standalone applications

  • It is also known as a desktop application or window-based application. An application that we need to install on every machine such as a media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.?

  1. Web applications

  • An application that runs on the server side and creates dynamic pages, is called a web application. Currently, servlet, jsp, struts; jsf etc. technologies are used for creating web applications in Java.??

  1. Enterprise application

  • An application that is distributed in nature, such as banking applications, etc. It has the advantage of high-level security, load balancing, and clustering. In Java, EJB is used for creating enterprise applications.

  1. Mobile application

  • An application that is created for mobile devices. Currently, Android and Java ME are used for creating mobile applications.

History of java

The history of Java starts with the Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions, etc.

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers is called the Green Team.

Originally designed for small, embedded systems in electronic appliances like set-top boxes.

Firstly, it was called "Green Talk" by James Gosling and the file extension was .gt.

After that, it was called Oak and was developed as a part of the Green project.

Why Oak? Oak is a symbol of strength and is chosen as a national tree of many countries like the U.S.A., France, Germany, Romania, etc.

In 1995, Oak was renamed "Java" because it was already a trademark by Oak Technologies.

Java Versions

There are many Java versions that have been released. The current stable release of Java is Java SE 8.

  1. JDK Alpha and Beta (1995)
  2. JDK 1.0 (23rd Jan, 1996)
  3. JDK 1.1 (19th Feb, 1997)
  4. J2SE 1.2 (8th Dec, 1998)
  5. J2SE 1.3 (8th May, 2000)
  6. J2SE 1.4 (6th Feb 2002)
  7. J2SE 5.0 (30th Sep, 2004)
  8. Java SE 6 (11th Dec, 2006)
  9. Java SE 7 (28th July, 2011)
  10. Java SE 8 (18th March, 2014)

Features of java?

There are many features of Java. The Java Features given below are simple and easy to understand.

  1. Simple
  2. Object-Oriented
  3. Platform independent
  4. Secured
  5. Robust
  6. Architecture neutral
  7. Portable
  8. Dynamic
  9. Interpreted
  10. High Performance
  11. Multithreaded
  12. Distributed

Simple

According to Sun, Java language is simple because:

??????Syntax is based on C++ (so easier for programmers to learn it after C++).

??????removed many confusing and/or rarely-used features e.g., explicit pointers, operator overloading, etc.

??????No need to remove unreferenced objects because there is an Automatic Garbage Collection in Java.

Object-oriented

Object-oriented means we organize our software as a combination of different types of objects that incorporate both data and behavior.

Object-oriented programming (OOP) is a methodology that simplifies software development and maintenance by providing some rules.

The basic concepts of OOPs are:

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Platform Independent

A platform is the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides a software-based platform. The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms. It has two components:?

  1. Runtime Environment
  2. API(Application Programming Interface)

Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, and Mac/OS etc. Java code is compiled by the compiler and converted into bytecode. This byte code is a platform-independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere (WORA)


Secured

Java is secured because:

  • No explicit pointer
  • Programs run inside a virtual machine sandbox.

Robust

Robust simply means strong. Java uses strong memory management. There is a lack of pointers that avoid security problems. There is automatic garbage collection in Java. There is an exception-handling and type-checking mechanism in Java. All these points make Java robust.?

Architecture-neutral

There are no implementation-dependent features e.g. size of primitive types is set.

Portable

We may carry the Java byte code to any platform.?

High-performance

Java is faster than traditional interpretation since byte code is "close" to native code but still somewhat slower than a compiled language (e.g., C++)?

Distributed

We can create distributed applications in Java. RMI (Remote method invocation) and EJB (Enterprise Java Bean) are used for creating distributed applications. We may access files by calling the methods from any machine on the internet.?

Multi-threaded

A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it shares the same memory. Threads are important for multi-media, Web applications, etc.?

JDK installation

Download the latest JDK version from the Oracle website https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and install it on the system.

Java Installation:-

  • Double-click on the “JDK.exe “executable file.
  • Click next, next, and install the java.

Set a path:-

  • After installation, go to the Bin folder of the JDK path, and copy the path.
  • Open My computer
  • Go to C driver
  • Click on the program files folder
  • Click on the Java folder
  • Click on the JDK folder
  • Click on bin
  • Copy the path
  • Ex: C:\Program Files\Java\jdk1.7.0_67\bin

Paste the copied path in the system environment variable

  • Right-click on my computer
  • Click on Properties
  • Click on advanced system settings
  • Click on the environment variable button on the system properties button
  • Click on the new button in the system variables
  • Paste the path give the name as a Java_path and click on ok.

How to check whether Java is installed or not:-

Open command prompt

  • Type java –version and click on enter
  • It will show the currently installed version of Java

Why do we need to set the path in Java?

  • We need to set the path to tell the OS where our Java commands are present and to recognize them.
  • The path is required to be set for using tools such as javac, java, etc. If you are saving the Java source file inside the JDK/bin directory, the path is not required to be set because all the tools will be available in the current directory. But if you are having your Java file outside the JDK/bin folder, it is necessary to set the path of JDK.

Java programs will be written in high-level language but CPU can’t understand the high-level language.

To convert high-level language into machine-understandable language we use a translator.?

Translator

Translators are classified into two types.

  1. Compiler
  2. Interpreter

Compiler: It takes an entire program and converts at a shot.

Interpreter: it converts line by line of the code.


  • The compiler will convert the .java file into a .class file.
  • .class file contains the byte code.
  • Byte code is made up of its own instructions that either human or CPU can’t understand.
  • JVM is an interpreter that will convert byte code into machine language that the CPU can understand.

Java is a compiler and interpreter-dependent language.

JVM can’t understand the high-level language.

The compiler will convert the high-level language into byte code.

JVM converts that byte code to CPU-understandable language, then the CPU will generate the results.

Simple Program of Java

On this page, we will learn how to write a simple program in Java. We can write a simple Hello Java program easily after installing the JDK.

To create a simple Java program, you need to create a class that contains the main method. Let's understand the requirements first.

File name: Simple.java?

public class Simple?

{

public static void main(String[] args)?

{

System.out.println("Hello, Java");

}

}??

Save this file as Simple.java

On command prompt

To compile:

javac Simple.java

To execute:

java Simple

Output: Hello Java

  • What is JDK?

A Java Development Kit (JDK) is a program development environment for writing Java applets and applications. It consists of a runtime environment that "sits on top" of the operating system layer as well as the tools and programming that developers need to compile, debug, and run applets and applications written in the Java language.


  • What is JRE?

The Java Runtime Environment (JRE), also known as Java Runtime, is part of the Java Development Kit (JDK), a set of programming tools for developing Java applications. The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files.


  • What is JVM?

The short form of JVM is Java Virtual Machine.

An abstract computing machine, or virtual machine, JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most programming languages compile source code directly into machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX.

A Machine within a Machine

JVM -- a machine within a machine -- mimics a real Java processor, enabling Java byte code to be executed as actions or operating system calls on any processor regardless of the operating system. For example, establishing a socket connection from a workstation to a remote machine involves an operating system call. Since different operating systems handle sockets in different ways, the JVM translates the programming code so that the two machines that may be on different platforms are able to connect.

JVM (Java Virtual Machine)

  1. Java Virtual Machine
  2. Internal Architecture of JVM

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which Java byte code can be executed.?

JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).

What is JVM?

It is:

  1. A specification where the working of Java Virtual Machine is specified. But implementation provider is independent in choosing the algorithm. Its implementation has been provided by Sun and other companies.
  2. An implementation its implementation is known as JRE (Java Runtime Environment).
  3. Runtime Instance Whenever you write a Java command on the command prompt to run the Java class, an instance of JVM is created.

What does it do?

The JVM performs the following operation:

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment

JVM provides definitions for the:

  • Memory area
  • Class file format
  • Register set
  • Garbage-collected heap
  • Fatal error reporting etc.

Internal Architecture of JVM

Let's understand the internal architecture of JVM. It contains a class loader, memory area, execution engine, etc.?


1) Class loader:

The class loader is a subsystem of JVM that is used to load class files.

2) Class (Method) Area:

Class (Method) Area stores per-class structures such as the runtime constant pool, field, and method data, and the code for methods.

3) Heap:

It is the runtime data area in which objects are allocated.

4) Stack:

Java Stack stores frames. It holds local variables and partial results and plays a part in method invocation and return.

Each thread has a private JVM stack, created at the same time as the thread.

A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

5) Program Counter Register:

PC (program counter) registers. It contains the address of the Java virtual machine instruction currently being executed.

6) Native Method Stack:

It contains all the native methods used in the application.

7) Execution Engine:

It contains:

1) A virtual processor

2) Interpreter: Read byte code stream then execute the instructions.

3) Just-In-Time(JIT) compiler: It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time and hence reduces the amount of time needed for compilation. Here is the term. Compiler? Refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Internal Details of Hello Java Program

public class Simple?

{

public static void main(String[] args)?

{

System.out.println("Hello, Java");

}

}

Internal Details of Hello Java

In the previous page, we have learned about the first program, how to compile, and how to run the first Java program. Here, we are going to learn, what happens while compiling and running the Java program. Moreover, we will see some questions based on the first program.

What happens at compile time?

At compile time, a Java file is compiled by Java Compiler (It does not interact with OS) and converts the Java code into byte code.


What happens at runtime?

At runtime, the following steps are performed:


Class loader: is the subsystem of JVM that is used to load class files.

Bytecode Verifier: checks the code fragments for illegal code that can violate access rights to objects.

Interpreter: read bytecode stream then execute the instructions.

Q) Can you save a Java source file by a other name than the class name?

Yes, if the class is not public. It is explained in the figure given below:


To compile:

javac Hard.java

To execute:

java Simple

Q) Can you have multiple classes in a Java source file?

Yes, as the figure given below illustrates:


Simple Program of Java

  1. Software Requirements
  2. Creating Hello Java Example
  3. Resolving Javac is not a recognized problem

On this page, we will learn how to write a simple program in Java. We can write a simple Hello Java program easily after installing the JDK.

To create a simple Java program, you need to create a class that contains the main method. Let's understand the requirements first.?

Requirement for Hello Java Example

For executing any Java program, you need to?

Creating a Hello Java example

Let's create the Hello Java program:

public class Simple?

{

public static void main(String[] args)?

{

System.out.println("Hello, Java");

}

}

Test it now?

and save this file as Simple.java

To compile:

javac Simple.java

To execute:

java Simple

Output: Hello Java

To write the simple program, open Notepad by start menu -> All Programs -> Accessories -> Notepad and write the simple program as displayed below:


As displayed in the above diagram, write the simple program of Java in Notepad and save it as Simple.java. To compile and run this program, you need to open the command prompt by starting the menu -> All Programs -> Accessories -> command prompt.


To compile and run the above program, go to your current directory first; my current directory is c:\new. Write here:

To compile:

javac Simple.java

To execute:

java Simple

Resolving an error "javac is not recognized as an internal or external command"?

If there is a problem like displayed in the below figure, you need to set a path. Since DOS doesn't know Javac or Java, we need to set a path. The path is not required in such a case if you save your program inside the JDK/bin folder. But it's a good approach to set a path.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了