Review of Java Fundamentals:
Java programming language was originally developed by Sun Microsystems
which was initiated by James Gosling and released in 1995 as core
component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]).
The latest release of the Java Standard Edition is Java SE 8. 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.
The new J2 versions were renamed as Java SE, Java EE, and Java ME
respectively. Java is guaranteed to be Write Once, Run Anywhere.
Java is –
● Object Oriented – In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
● Platform Independent – Unlike many other programming languages
including C and C++, when Java is compiled, it is not compiled into platform
specific machine, rather into platform independent byte code. This byte code
is distributed over the web and interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
● Simple – Java is designed to be easy to learn. If you understand the
basic concept of OOP Java, it would be easy to master.
● Secure – With Java’s secure feature it enables to develop virus-free,
tamper-free systems. Authentication techniques are based on public-key
encryption.
● Architecture-neutral – Java compiler generates an architecture-neutral
object file format, which makes the compiled code executable on many
processors, with the presence of Java runtime system.
● Portable – Being architecture-neutral and having no implementation
dependent aspects of the specification makes Java portable. Compiler in Java
is written in ANSI C with a clean portability boundary, which is a POSIX
subset.
● Robust – Java makes an effort to eliminate error prone situations by
emphasizing mainly on compile time error checking and runtime checking.
● Multithreaded – With Java’s multithreaded feature it is possible to write
programs that can perform many tasks simultaneously. This design feature
allows the developers to construct interactive applications that can run
smoothly.
● Interpreted – Java byte code is translated on the fly to native machine
instructions and is not stored anywhere. The development process is more
rapid and analytical since the linking is an incremental and light-weight
process.
● High Performance – with the use of Just-In-Time compilers, Java enables
high performance.
● Distributed – Java is designed for the distributed environment of the
internet.
● Dynamic – Java is considered to be more dynamic than C or C++ since
it is designed to adapt to an evolving environment. Java programs can carry
extensive amount of run-time information that can be used to verify and
resolve accesses to objects on run-time.
Hello World using Java Programming.
Just to give you a little excitement about Java programming, I’m going to
give you a small conventional C Programming Hello World program, You can
try it using Demo link.
History of Java
James Gosling initiated Java language project in June 1991 for use in one of
his many set-top box projects. The language, initially called ‘Oak’ after an
oak tree that stood outside Gosling’s office, also went by the name ‘Green’
and ended up later being renamed as Java, from a list of random words.
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.
Tools You Will Need
For performing the examples discussed in this tutorial, you will need a
Pentium 200-MHz computer with a minimum of 64 MB of RAM (128 MB of
RAM recommended).
You will also need the following softwares –
● Linux 7.1 or Windows xp/7/8 operating system
● Java JDK 8
● Microsoft Notepad or any other text editor
Popular Java Editors
To write Java programs, you need a text editor. There are even more
sophisticated IDEs available in the market. The most popular ones are briefly
described below –
● Notepad – On Windows machine, you can use any simple text editor
like Notepad (recommended for this tutorial) or WordPad. Notepad++ is also
a free text editor which enhanced facilities.
● Netbeans – It is a Java IDE that is open-source and free which can be
downloaded from [Link]/[Link].
● Eclipse – It is also a Java IDE developed by the Eclipse open-source
community and can be downloaded from [Link].
IDE or Integrated Development Environment, provides all common tools and
facilities to aid in programming, such as source code editor, build tools and
debuggers etc.
Basics of Java Programming
Java Syntax
Every programming language has certain rules and regulations that a
programmer needs to follow while writing programs. The respective language
compiler checks your program for syntax rules and validation. Java also has
lots of constructs and components that make it easier for programmers to
write quality programs.
The “Hello, World” program in the previous tutorial gave you an idea about
the basic structure of a Java program in detail. Now let’s go to the other
constructs/components that a Java program will include.
Java Conventions
Given below are some of the conventions that a Java programmer needs to
follow while programming in Java.
(i) Class names: In Java, the first letter of class name for every class
should be uppercase. For example, a class salary will be named as
per the convention as “Salary”.
If you have a class name that is combined using more than one word then
each letter of the first word will be an uppercase letter.
For example, class MyHelloWorld.
(ii) Method name: All method names in Java start with a lowercase
letter. If the method name comprises more than one word, then the
first letter of each of these inner words will be uppercase. Example:
display (), myMethod () => these are valid method names in Java.
(iii) Program file name: The filename of the Java program should be the
same as the name of the public class with an extension “.java”.
For example, if the public class name is “MyFirstClass”, then you save this
code in the file named “[Link]”.
Note that a java source code may not have any public class. In that case, you
can have your name for the source file.
(iv) Case sensitive: Java programming language is case-sensitive. This
means ‘Hi’ and ‘hi’ are two different parameters.
(v) Main method: The method ‘main’ is the starting point of execution
and is a compulsory method in all Java programs.
Java Identifiers
Identifiers are the names given to various program components like
methods, classes, variables, objects, etc.
A Java programmer has to follow the below rules for identifiers:
• An identifier should always start with letters (A-Z/a-z) or an underscore
character (_) or currency character ($).
• The identifier cannot have the same name as a Java reserved Keyword.
• Beyond the first character, an identifier can have any combination of
characters.
• In Java, like other language syntax, identifiers are also case-sensitive.
Hence as per the above rules, the following identifiers are valid.
myVar, _salary, $sum
The following identifiers are not valid.
123var, *mult, int. Java Modifiers
Modifiers change the accessibility of variables, methods, etc.
There are two types of modifiers in Java:
• Access modifiers: There are four access modifiers in Java namely,
public, protected, private, and default. These are used to define
accessibility for packages, classes, class members, etc.
• Non-access modifiers: Java supports non-access modifiers namely –
final, abstract, and strictfp. These are mainly used to define
inheritance, polymorphism style, etc.
Data Types
Any variable that holds the value needs to have a type of the value which it
is going to hold i.e. whether a variable is going to hold a numeric or string or
character. This is called the data type of the variable.
Java has various data types as depicted in the below diagram.
Variables
Java supports the following three types of variables:
• Class or static variables: This type of variable can be accessed without
an object.
• Non-static or instance variables: These variables are member variables
which are accessed with the help of a class object.
• Local variables: Local variables are local to a particular block of code
and cease to exist out of this block.
Keywords
There are certain words reserved in Java language for its own use and cannot
be used as variable or identifier names.
The following table gives the list of these words known as “Keywords”.
Abstract double int super
Assert else interface switch
Boolean enum long synchronized
Break extends native this
Byte final new throw
Case finally package throws
Catch float private transient
Char for protected try
Class goto public void
Const if return volatile
Continue implementsshort while
Default import static
Do instance of strictfp
Comments
Comments are the statements that are ignored by the compiler. You can
provide comments for your code to make the code more readable and easy
to understand.
Java supports three types of comments:
• Single line comments denoted by ‘//’
• Multi-line comments represented by ‘/*… */’
• Documentation comments denoted by ‘/** ******/
You will learn more about these comments in our subsequent tutorials.
PRIMITIVE DATA TYPES
Operators
Operators are symbols that perform logical and mathematical operations on
variables or identifiers. These variables or identifiers are called Operands.
Java supports various operators as shown in the below diagram:
Decision Making
Also called as control statements. These statements change or control the
program execution based on a particular condition. If the condition is true, a
block of code following this condition is executed, else a different block is
executed.
Java has the following control/decision making statements.
Loops
In programming languages, looping is included to repeatedly execute a block
of code. The looping usually starts with a test and the block of code is
executed repeatedly for a fixed number of times called iterations or till a
condition is fulfilled.
In Java, you have the following loop constructs.
ARRAYS
Arrays are nothing but a data structure that is used to hold the data
elements of the same type sequentially.
Java arrays are also similar to arrays in C/C++ and other programming
languages.
Int[] array = new int[5]; // one-dimensional array it may also be declared as:
Int[] array = {1, 2, 3, 4, 5}; // it is initialized and declared at the same time.
Arrays can be of any primitive data type or object. Arrays have an instance
variable “length” that we can access to find out how big the array is.
Remember that an Array can throw an IndexOutOfBoundsException. Make
use of it to make your code safe.
VECTORS
Must import [Link].*;
Unlike arrays, vectors can only hold objects, no primitive data types are
allowed. Vectors can grow big and unbounded. You may want to specify the
capacity of the vector but you don’t need to. The default capacity is 10 and it
doubles in size every time the capacity is reached.
Just like arrays have the “length” instance variable, Vectors have a .size()
method that returns the current size of the vector.
• Void .addElement(Object obj)
• Void .setElementAt(Object obj, int index)
• Object .elementAt(int index)
• Void .insertElementAt(Object obj, int index)
• Void .removeElementAt(Object obj, int index)
ENUMERATION
This is an iterator and it is very important concept in data structures for
ICS211. An Enumeration lets us visit every element of a Vector one by one.
Vector v = new Vector(); Enumeration e = [Link]();
While([Link]()){ Object o = [Link];
// we do something with each element.