module1
module1
INTRODUCTION TO JAVA
What is Java?
It is used for:
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming language in the world
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has a huge community support (tens of millions of developers)
Java is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or
vice versa
https://www.w3schools.com/java/java_intro.asp
FEATURES OF JAVA
The prime reason behind creation of Java was to bring portability and security feature into a
computer language. Beside these two major features, there were many other features that
played an important role in moulding out the final form of this outstanding language. Those
features are :
1) Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The
confusing and ambiguous concepts of C++ are either left out in Java or they have been re-
implemented in a cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C+
+.
2) Object Oriented
In java, everything is an object which has some data and behaviour. Java can be easily
extended as it is based on Object Model. Following are some basic concept of OOP's.
i. Object
ii. Class
iii. Inheritance
iv. Polymorphism
v. Abstraction
vi. Encapsulation
3) 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.
4) 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.
Any machine with Java Runtime Environment can run Java Programs.
5) Secure
When it comes to security, Java is always the first choice. With java secure features it enable
us to develop virus free, temper free system. Java program always runs in Java runtime
environment with almost null interaction with system OS, hence it is more secure.
6) 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.
7) Architectural Neutral
Compiler generates byte codes, which have nothing to do with a particular computer
architecture, hence a Java program is easy to interpret on any machine.
8) 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
9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C+
+. But, Java enables high performance with the use of just-in-time compiler.
10) Distributed
Java is also a distributed language. Programs can be designed to run on computer networks.
Java has a special class library for communicating using TCP/IP protocols. Creating network
connections is very much easy in Java as compared to C/C++.
https://www.studytonight.com/java/features-of-java.php
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides 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:
What it does
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
JVM Architecture
Let's understand the internal architecture of JVM. It contains class loader, memory area,
execution engine etc.
1) Class loader
Class loader is a subsystem of JVM which is used to load class files. Whenever we run the
java program, it is loaded first by the class loader. There are three built-in class loaders in
Java.
1. Bootstrap ClassLoader: This is the first classloader which is the super class of
Extension classloader. It loads the rt.jar file which contains all class files of Java
Standard Edition like java.lang package classes, java.net package classes, java.util
package classes, java.io package classes, java.sql package classes etc.
2. Extension ClassLoader: This is the child classloader of Bootstrap and parent
classloader of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: This is the child classloader of Extension
classloader. It loads the classfiles from classpath. By default, classpath is set to
current directory. You can change the classpath using "-cp" or "-classpath" switch. It
is also known as Application classloader.
Output:
sun.misc.Launcher$AppClassLoader@4e0e2f2a
null
These are the internal classloaders provided by Java. If you want to create your own
classloader, you need to extend the ClassLoader class.
2) Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.
3) Heap
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 thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method
invocation completes.
PC (program counter) register contains the address of the Java virtual machine instruction
currently being executed.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter: Read bytecode 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, 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.
Java Native Interface (JNI) is a framework which provides an interface to communicate with
another application written in another language like C, C++, Assembly etc. Java uses JNI
framework to send output to the Console or interact with OS libraries.
https://www.javatpoint.com/jvm-java-virtual-machine
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
All Java programs are written in plain text — therefore you don't need any special software.
For your first program, open up the simplest text editor you have on your computer, likely
Notepad.
While you could cut and paste the above code into your text editor, it’s better to get into the
habit of typing it in. It will help you learn Java more quickly because you will get a feel for
how programs are written, and best of all, you will make mistakes! This may sound odd, but
each mistake you make helps you to become a better programmer in the long run. Just
remember that your program code must match the example code, and you’ll be fine.
Note the lines with "//" above. These are comments in Java, and the compiler ignores them.
Save your program file as "HelloWorld.java". You might consider creating a directory on
your computer just for your Java programs.
It’s very important that you save the text file as "HelloWorld.java". Java is picky about
filenames. The code has this statement:
This is an instruction to call the class "HelloWorld". The filename must match this class
name, hence the name "HelloWorld.java". The extension ".java" tells the computer that it’s
a Java code file.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
Most programs that you run on your computer are windowed applications; they work inside a
window that you can move around on your desktop. The HelloWorld program is an example
of a console program. It does not run in its own window; it has to be run through a terminal
window instead. A terminal window is just another way of running programs.
To open a terminal window, press the "Windows key" and the letter “R”.
You will see the "Run Dialog Box". Type "cmd" to open the command window, and press
"OK".
A terminal window opens on your screen. Think of it as a text version of Windows Explorer;
it will let you navigate to different directories on your computer, look at the files they
contain, and run programs. This is all done by typing commands into the window.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
Another example of a console program is the Java compiler called "javac." This is the
program that will read the code in the HelloWorld.java file, and translate it into a language
your computer can understand. This process is called compiling. Every Java program you
write will have to be compiled before it can be run.
To run javac from the terminal window, you first need to tell your computer where it is. For
example, it might be in a directory called "C:\Program Files\Java\jdk\1.6.0_06\bin". If you
don’t have this directory, then do a file search in Windows Explorer for "javac" to find out
where it lives.
Once you’ve found its location, type the following command into the terminal window:
E.g.,
Press Enter. The terminal window will just return to the command prompt. However, the path
to the compiler has now been set.
E.g.,
You can tell if you’re in the right directory by looking to the left of the cursor.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
We’re now ready to compile the program. To do so, enter the command:
Press Enter. The compiler will look at the code contained within the HelloWorld.java file,
and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.
Hopefully, you should have no errors. If you do, go back and check the code you’ve written.
Make sure it matches the example code and re-save the file.
Tip: Once your HelloWorld program has been successfully compiled, you will see a new file
in the same directory. It will be called “HelloWorld.class”. This is the compiled version of
your program.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
All that’s left to do is run the program. In the terminal window, type the command:
When you press Enter, the program runs and you will see "Hello World!" written to the
terminal window.
https://www.thoughtco.com/creating-your-first-java-program-2034124
A package is a group of related classes and interfaces. The JDK software is organized
through packages. It is equivalent to a header file of C-lang and module of Modula language
(Modula is a descendent of Pascal). Packages can be compressed into JAR files (refer xxxx)
so that they can be downloaded or sent across the network fast.
1. With a simple import statement, all the classes and interfaces can be imported. 2. Java
includes a provision to import only one class from a package. 3. It avoids namespace
problems (name conflicts). Two classes with the same name cannot be put in the same
package but can be put in two different packages because a package creates its own
namespace (folder). 4. Access for the classes can be controlled. 5. Classes and interfaces of
same functionality can be grouped together. 6. Because functionally all the classes are
related, later their identification and determining the location become easier. 7. Java packages
are used to group and organize the classes.
Java permits to import all the classes or only one class from a package. C-lang does not have
this facility of including only one function from a header file.
There are three ways to access a class or an interface that exits in a different package.
1. Using fully qualified name 2. Importing only one class 3. Importing all the classes
Fully qualified name includes writing the names of the packages along with the class name as
follows.
java.awt.event.ActionListenerjava.util.Stack
Objects can be created as follows.
This way of using may not look nice when we use the same class a number of times in the
code as readability becomes boredom. This will be a nice approach and justified when the
class is used only once.
Java permits to import only one class also from a package as follows.
This type of approach is the best and beneficial when only one class is required from the
same package. Importing one class (when other classes are not required) saves a lot of RAM
space usage on the client's machine.
One of the advantages of packages is to include all the classes and interfaces of a package
with a single import statement.
The asterisk (*) mark indicates all the classes and interfaces of the package. After importing,
the objects of the cl asses can be created straightaway.
This type of approach is beneficial when many classes and interfaces are required from a
package.
https://way2java.com/java-general/java-import-classes/
String provides format() method can be used to print formatted output in java.
System.out.printf() method can be used to print formatted output in java.
Following example returns a formatted string value by using a specific locale, format and
arguments in format() method
Example
importjava.util.*;
publicclassStringFormat{
publicstaticvoid main(String[]args){
double e =Math.E;
System.out.format("%f%n", e);
System.out.format(Locale.GERMANY,"%-10.4f%n%n", e);
Output
The above code sample will produce the following result.
2.718282
2,7183
publicclassHelloWorld{
publicstaticvoid main(String[]args){
System.out.print(s1);
System.out.print("\n");
System.out.print(s2);
System.out.print("\n");
System.out.print(s3);
System.out.print("\n");
Output
The above code sample will produce the following result.
https://www.tutorialspoint.com/Formatted-Output-in-Java