Java Module-1
Java Module-1
Eg:
Step1: get two input
Step 2: compute two numbers
Step 3: display output
Step 4: stop
History of Java
Source: https://thetapacademy.com/java-programming-language/
Java Name History
GreenTalk
James Gosling was leading a team named as 'Green' team. Target of this team
was to create a new project which can. Initially C++ was the original choice to
develop the project. James Gosling wanted to enhance C++ to achieve the target
but due to high memory usage, that idea was rejected and team started with a
new language initially named as GreenTalk. The file extension used as .gt. Later
this language was termed as Oak and finally to Java.
Oak
James Gosling renamed language to Oak. There was an Oak tree in front of his
office. James Gosling used this name as Oak represents solidarity and Oak tree
is the national tree of multiple countries like USA, France, Romania etc. But
Oak technologies already had Oak as a trademark and James team had to
brainstrom another title for the language.
Finally Java
Team put multiple names like DNA, Silk, Ruby and Java. Java was finalized by
the team. James Gosling tabled Java title based on type of espresso coffee bean.
Java is an island in Indonesia where new coffee was discovered termed as Java
coffee. As per James Gosling, Java was among the top choice along with Silk.
Finally Java was selected as it was quite unique and represented the essence of
being dynamic,revolutionary and fun to say.
Sun released the first public implementation as Java 1.0 in 1995. It
promised Write Once, Run Anywhere (WORA), providing no-cost run-times on
popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source
software under the terms of the GNU General Public License (GPL).
On 8 May, 2007, Sun finished the process, making all of Java's core code free
and open-source, aside from a small portion of code to which Sun did not hold
the copyright.
The latest release of the Java Standard Edition is Java SE 21. With the
advancement of Java and its widespread popularity, multiple configurations
were built to suit various types of platforms. For example: J2EE for Enterprise
Applications, J2ME for Mobile Applications.
Java Versions History
Over the period of nearly 30 years, Java has seen many minor and major
versions.
JDK
1 1995 Initial Draft version
Beta
23
2 JDK 1.0 Jan A stable variant JDK 1.0.2 was termed as JDK 1
1996
19
Major features like JavaBeans, RMI, JDBC, inner
3 JDK 1.1 Feb
classes were added in this release.
1997
8
HotSpot JVM, JNDI, JPDA, JavaSound and support for
5 JDK 1.3 May
Synthetic proxy classes were added.
2000
JDK 1.5 30
Various new features were added to the language like
7 or J2SE Sep
foreach, var-args, generics etc.
5 2004
11
JAVA SE 1. notation was dropped to SE and upgrades done to JAXB
8 Dec
6 2.0, JSR 269 support and JDBC 4.0 support added.
2006
21
JAVA SE Module system introduced which can be applied to JVM
11 Sep
9 platform.
2017
17
JAVA SE Feature added - Text Blocks (Multiline strings), Enhanced
15 Sep
13 Thread-local handshakes.
2019
15
JAVA SE Feature added - Sealed Classes, Hidden Classes, Foreign
17 Sep
15 Function and Memory API (Incubator).
2020
16
JAVA SE Feature added as preview - Records, Pattern Matching for
18 Mar
16 switch, Unix Domain Socket Channel (Incubator) etc.
2021
14 Feature added as finalized - Sealed Classes, Pattern
JAVA SE
19 Sep Matching for instanceof, Strong encapsulation of JDK
17
2021 internals by default. New macOS rendering pipeline etc.
20
JAVA SE Feature added - Record pattern, Vector API (Fourth
21 Sep
19 incubator), Structured Concurrency (Incubator) etc.
2022
Comments:
Single-line comments: //
Multiline comments: /* */
Documentation comments
The documentation comments are used for writing the documentation of the source code.
The documentation comments start with a forward slash followed by the two asterisks
(/**), end with an asterisk followed by a backward slash (*/), and all lines between the
start and end must start with an asterisk (*).
The documentation comments are understood by the Javadoc tool and can be used to
create HTML-based documentation.
Java Tokens: it is a smallest individual element in the program
Java tokens has five types: Reserved keywords, identifiers, literals, operators
and separators
Character set: Unicode character set [A-Z, a-z, 0-9 and special character which
are available in keyboard]
Keyword: 50 keywords
Identifiers:
Literals: constant values to be stored in variable
Integral literal, floating point literal, character literal, string and Boolean
‘A’ character, it require 1 byte of memory
“Apple” string it require no of character + 1 byte of memory (i.e 5 character + 1
byte, last bit is used for null character which represent end of string.)
Operators:
Arithmetic: +, -, *, /, %
Relational: <,<=,>,>=,!=,==
Logical: && (AND), || (OR is a pipe symbole), !( not)
Assignment: =
Increment or decrement: ++, --
Conditional: ? : ( exp1?exp2: exp3)
(marks>35? “Pass”: “Try again”;)
Bitwise : &, |, ^ (Exclusive OR), ~ (one complement), << (shift left), >>
(Shift right), >>> (Shift right with zero fill)
Special operators: to indicate a group of code, [] arrays, () functions
Data types: The data type tells the compiler about the type of data to be stored
and the required memory. To store and manipulate different types of data, all
variables must have specified data types.
Primitive Data Types
Reference/Object Data Types
Primitive data types are predefined by the language and named by a keyword.
There are eight primitive data types supported by Java
Type Siz Range Min Max
e in
bits
Byte 8 -128 to -128 127
127
Short 16 -32,768 -32768 32767
to
32,767
Int 32 -231 to -2147483648 2147483647
231-1
Long 64 -263 to - 9223372036854775807
263-1 922337203685477580
8
Float 32 1.4e-045 1.4E-45 3.4028235E38
to
3.4e+03 Sufficient for storing 6 to
8 7 decimal digits
Char 16 0 to
65,535
Boolea 1 True or
n false
Double 8 4.9E-324 1.7976931348623157E30
8
Sufficient for storing 15
decimal digits
Operator Hierarchy
The operator precedence represents how two expressions are bind together. In
an expression, it determines the grouping of operators with operands and
decides how an expression will evaluate.
While solving an expression two things must be kept in mind the first is
a precedence and the second is associativity.
Expressions
Type conversions and Casting
Enumerated Types
Control Flow Statements
Jump Statements
Arrays
Input and Output
Java brings various Streams with its I/O package that helps the user to perform all the input-
output operations. These streams support all the types of objects, data-types, characters, files
etc to fully execute the I/O operations.
3 standard or default streams that Java has to provide which are also most
common in use:
1. System.in: This is the standard input stream that is used to read characters
from the keyboard or any other standard input device.
2. System.out: This is the standard output stream that is used to produce the
result of a program on an output device like the computer screen.
Here is a list of the various print functions that we use to output statements:
print(): This method in Java is used to display a text on the console.
This text is passed as the parameter to this method in the form of
String. This method prints the text on the console and the cursor
remains at the end of the text at the console. The next printing takes
place from just here.
Syntax:
System.out.print(parameter);
println(): This method in Java is also used to display a text on the console. It
prints the text on the console and the cursor moves to the start of the next line at
the console. The next printing takes place from the next line.
Syntax:
System.out.println(parameter);
printf(): This is the easiest of all methods as this is similar to printf in C. Note
that System.out.print() and System.out.println() take a single argument, but
printf() may take multiple arguments. This is used to format the output in Java.
1. System.err: This is the standard error stream that is used to output
all the error data that a program might throw, on a computer screen
or any standard output device.
This stream also uses all the 3 above-mentioned functions to output the
error data:
print()
println()
printf()
Types of Streams:
Depending on the type of operations, streams can be divided into two
primary classes:
1. Input Stream: These streams are used to read data that must
be taken as an input from a source array or file or any
peripheral device. For eg., FileInputStream,
BufferedInputStream, ByteArrayInputStream etc.
PrintStream This contains the most used print() and println() method
BufferedOutputStrea
This is used for Buffered Output Stream.
m
DataOutputStream This contains method for writing java standard data types.
%b Any type " true " if non-null, " false " if null
Constructors
In Java, a Constructor is a block of codes similar to the method. It is called when an instance
of the class is created. At the time of calling the constructor, memory for the object is
allocated in the memory. It is a special type of method that is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
Note: It is not necessary to write a constructor for a class. It is because the java compiler
creates a default constructor (constructor with no arguments) if your class doesn’t have any.
The first line of a constructor is a call to super() or this(), (a call to a constructor of a super-
class or an overloaded constructor), if you don’t type in the call to super in your constructor
the compiler will provide you with a non-argument call to super at the first line of your code,
the super constructor must be called to create an object:
If you think your class is not a subclass it actually is, every class in Java is the subclass of a
class object even if you don’t say extends object in your class definition.
class Ge {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Ge(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Ge(Ge obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class CopyConstructor{
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Ge ge1 = new Ge("Raj", 24);
System.out.println("Name :" + ge1.name + " and Id :" + ge1.id);
System.out.println();
Methods
Parameter passing
Static Field and Methods
The static keyword is used to construct methods that will exist regardless of whether or not
any instances of the class are generated. Any method that uses the static keyword is referred
to as a static method.
Features of static method:
A static method in Java is a method that is part of a class rather than an instance of
that class.
Every instance of a class has access to the method.
Static methods have access to class variables (static variables) without using the
class’s object (instance).
Only static data may be accessed by a static method. It is unable to access data that is
not static (instance variables).
In both static and non-static methods, static methods can be accessed directly.
Syntax to declare the static method:
Access_modifier static void methodName()
{
// Method body.
}
The name of the class can be used to invoke or access static methods.
Syntax to call a static method:
className.methodName();
The methods can be accessed only using The method is only accessed by class
object reference. name.
Access control
This reference
Overloading methods and constructors
Recursion
Garbage collection
Garbage collection in Java is the process by which Java programs perform automatic memory
management. Java programs compile to bytecode that can be run on a Java Virtual Machine,
or JVM for short. When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program. Eventually, some objects will no
longer be needed. The garbage collector finds these unused objects and deletes them to free
up memory.
20 int indexOf(int ch, int fromIndex) It returns the specified char value
index starting with given index.