0% found this document useful (0 votes)
3 views31 pages

Java-Basics-1

Java is a programming language developed by Sun Microsystems, released in 1995, and is known for its platform independence and versatility across various applications including desktop, web, enterprise, and mobile. It features object-oriented programming, security, and multithreading capabilities, making it suitable for a wide range of environments. The document also outlines the history of Java, its programming phases, and basic syntax, along with guidelines for setting up a Java programming environment.

Uploaded by

Moshi Moshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views31 pages

Java-Basics-1

Java is a programming language developed by Sun Microsystems, released in 1995, and is known for its platform independence and versatility across various applications including desktop, web, enterprise, and mobile. It features object-oriented programming, security, and multithreading capabilities, making it suitable for a wide range of environments. The document also outlines the history of Java, its programming phases, and basic syntax, along with guidelines for setting up a Java programming environment.

Uploaded by

Moshi Moshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

Java

What is Java?
- programming language that 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 18.
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.
Applications of Java:
According to Sun, 3 billion devices run Java.
There are many devices where Java is currently
used. Some of them are as follows:
 Desktop Applications such as acrobat reader, media
player, antivirus, etc.
 Web Applications such as irctc.co.in, javatpoint.com, etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games, etc.
Types of Java Applications
There are mainly 4 types of applications that can be created using
Java programming:
 1) Standalone Application
Standalone applications are also known as desktop applications
or window-based applications. These are traditional software that
we need to install on every machine. Examples of standalone
application are Media player, antivirus, etc. AWT and Swing are
used in Java for creating standalone applications.
 2) Web Application
An application that runs on the server side and creates a dynamic
page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.
Types of Java Applications (2)
 3) Enterprise Application
An application that is distributed in nature, such as banking
applications, etc. is called an enterprise application. It has
advantages like high-level security, load balancing, and
clustering. In Java, EJB is used for creating enterprise applications.
 4) Mobile Application
An application which is created for mobile devices is called a
mobile application. Currently, Android and Java ME are used for
creating mobile applications.
Features of Java:

 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.
Features of Java:
 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.
Features of Java:
 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.
Features of Java:
 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.
Features of Java:
 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.
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.
History of Java
History of Java
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 opensource, aside from a small
portion of code to which Sun did not
hold the copyright.
Five Phases of JAVA Programs
Phase 1: Edit
- The programmer writes the program
(preferably on a notepad) and saves it as a
‘.java’ file, which is then further used for
compilation, by the compiler.
Phase 2: Compile javac (java compiler)
- The compiler here takes the ‘.java’ file,
compiles it and looks for any possible errors in
the scope of the program. If it finds any error,
it reports them to the programmer. If no error
is there, then the program is converted into
the bytecode and saved as a ‘.class’ file.
Five Phases of JAVA Programs
Phase 3: Load
- Now the major purpose of the component
called ‘Class Loader’ is to load the byte
code in the JVM. It doesn't execute the
code yet, but just loads it into the JVM’s
memory.
Phase 4: Verify
- After loading the code, the JVM’s subpart
called ‘Byte Code verifier’ checks the
bytecode and verifies it for its authenticity. It
also checks if the bytecode has any such
code which might lead to some malicious
outcome. This component of the JVM ensures
Five Phases of JAVA Programs
Phase 5: Execute
- The next component is the Execution
Engine. The execution engine interprets the
code line by line using the Just In Time (JIT)
compiler. The JIT compiler does the
execution pretty fast but consumes extra
cache memory.

After these processes we have the Machine


Code which is thereby run.
Tools You Will Need for Java
Programming
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
Java: Environmental
Setup
Local Environment Setup
If you are still willing to set up your environment for
Java programming language, following are the
steps to set up the environment.
Java SE is freely available from the link available
in Java webpage. You can download a version
based on your operating system.
Follow the instructions to download Java and run
the .exe to install Java on your machine. Once
you installed Java on your machine, you will
need to set environment variables to point to
correct installation directories:
Setting Up the Path for
Windows
Assuming you have installed Java in c:\Program Files\java\jdk
directory:
• Right-click on 'My Computer' and select 'Properties'.
• Click the 'Environment variables' button under the 'Advanced'
tab.
• Now, alter the 'Path' variable so that it also contains the path to
the Java executable. Example, if the path is currently set to
'C:\WINDOWS\SYSTEM32', then change your path to read
'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Popular Java Editors
To write your Java programs, you will need a text
editor. There are even more sophisticated IDEs
available in the market. But for now, you can
consider one of the following:
▪ Notepad
▪ Textpad
▪ Wordpad
▪ Netbeans
▪ Eclipse
▪ JCreator
Java: Basic Syntax
When we consider a Java program, it can be
defined as a collection of objects that
communicate via invoking each other's methods.
Let us now briefly look into what do class, object,
methods, and instance variables mean.

 Object – Unique programming entity that has


methods, has attributes and can react to
events.
 Method – Things which an object can do; the
“verbs” of objects. In code, usually can be
identified by an “action” word -- Hide, Show
 Attribute – Things which describe an
object; the “adjectives” of objects. In
code, usually can be identified by a
“descriptive” word – Enabled,
BackColor
 Events – Forces external to an object to
which that object can react. In code,
usually attached to an event
procedure
 Class – Provides a way to create new
objects based on a “meta-definition”
of an object (Example: The automobile
class)
 Constructors – Special methods used to
create new instances of a class
(Example: A Honda Civic is an instance
of the automobile class.)
First Java Program
Let's look at how to save the file, compile, and
run the program. Please follow the subsequent
steps:
1. Open notepad and add the code below.
First Java Program
2. Save the file as: MyFirstJavaProgram.java
3. Open a command prompt window and go to the
directory where you saved the class. Assume it's C:\.
3. Type 'javac MyFirstJavaProgram.java' and press
enter to compile your code. If there are no errors in
your code, the command prompt will take you to
the next line (Assumption : The path variable is set).
4. Now, type ' java MyFirstJavaProgram ' to run your
program.
5. You will be able to see ' Hello World ' printed on
the window.
First Java Program
2. Save the file as: MyFirstJavaProgram.java
3. Open a command prompt window and go to the
directory where you saved the class. Assume it's C:\.
3. Type 'javac MyFirstJavaProgram.java' and press
enter to compile your code. If there are no errors in
your code, the command prompt will take you to
the next line (Assumption : The path variable is set).
4. Now, type ' java MyFirstJavaProgram ' to run your
program.
5. You will be able to see ' Hello World ' printed on
the window.
About Java programs, it is very
important to keep in mind the
following points.
 Case Sensitivity - Java is case sensitive, which
means identifier Helloand hello would have
different meaning in Java.
 Class Names - For all class names the first letter
should be in Upper Case.

If several words are used to form a name of the class,


each inner word's first letter should be in Upper Case.
Example: class MyFirstJavaClass
About Java programs, it is very
important to keep in mind the
following points.
 Method Names - All method names should start
with a Lower Case letter.

If several words are used to form the name of the


method, then each inner word's first letter should be
in Upper Case.
Example: public void myMethodName()
About Java programs, it is very
important to keep in mind the
following points.
 Program File Name - Name of the program file should
exactly match the class name.
When saving the file, you should save it using the class name
(Remember Java is case sensitive) and append '.java' to the
end of the name (if the file name and the class name do
not match, your program will not compile).
Example: Assume 'MyFirstJavaProgram' is the class name.
Then the file should be saved as 'MyFirstJavaProgram.java‘

 public static void main(String args[]) - Java program


processing starts from the main() method which is a
mandatory part of every Java program.
Java Identifiers
All Java components require names. Names used for
classes, variables, and methods are called identifiers.
In Java, there are several points to remember about
identifiers. They are as follows:
 All identifiers should begin with a letter (A to Z or a to z),
currency character ($) or an underscore (_).
 After the first character, identifiers can have any
combination of characters.
 A key word cannot be used as an identifier.
 Most importantly, identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value.
Examples of illegal identifiers: 123abc, -salary.

You might also like