0% found this document useful (0 votes)
10 views19 pages

Java Notes

java notes
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)
10 views19 pages

Java Notes

java notes
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/ 19

What is Java?

Java is a programming language and a platform. Java is a high level, robust,


object-oriented and secure programming language.

Platform: Any hardware or software environment in which a program runs, is known as a


platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

Simple.java

1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }

Application
According to Sun, 3 billion devices run Java. 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 such as irctc.co.in, javatpoint.com, etc.

3. Enterprise Applications such as banking applications.

4. Mobile

5. Embedded System

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 Application
Standalone applications are also known as desktop applications or window-based
applications.

2) Web Application
An application that runs on the server side and creates a dynamic page is called a web
application.

3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called
an enterprise application.

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.

History of Java
Java's history is intriguing. Designed by James Gosling's Green Team for interactive TV,
it was too advanced for digital cable. Starting in the early '90s, it excelled in internet
programming and was adopted by Netscape.

Released in 1995, Java's principles include simplicity, robustness, portability, and


platform independence. Today, it's used in internet programming, mobile devices,
games, and e-business solutions.

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

2) Initially it was designed for small, embedded systems in electronic appliances like
set-top boxes.

3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.

4) After that, it was called Oak and was developed as a part of the Green project.
Java Version History
Many java versions have been released till now. The current stable release of Java is
Java SE 10.

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 Mar 2014)

11. Java SE 9 (21st Sep 2017)

12. Java SE 10 (20th Mar 2018)

13. Java SE 11 (September 2018)

14. Java SE 12 (March 2019)

15. Java SE 13 (September 2019)

16. Java SE 14 (Mar 2020)

17. Java SE 15 (September 2020)

18. Java SE 16 (Mar 2021)

19. Java SE 17 (September 2021)

20. Java SE 18 (to be released by March 2022)


First Java Program | Hello World Example
The requirement for Java Hello World Example

For executing any Java program, the following software or application must be properly
installed.

○ Install the JDK if you don't have installed it, download the JDK and install it.

○ Set path of the jdk/bin directory.


http://www.javatpoint.com/how-to-set-path-in-java

○ Create the Java program

○ Compile and run the Java program

Creating Hello World Example


Let's create the hello java program:

1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }

Compilation Flow:

When we compile Java program using javac tool, the Java compiler converts the source
code into byte code.
Parameters used in First Java Program
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().

○ class keyword is used to declare a class in Java.

○ public keyword is an access modifier that represents visibility. It means it is


visible to all.

○ static is a keyword. If we declare any method as static, it is known as the static


method. The core advantage of the static method is that there is no need to
create an object to invoke the static method. The main() method is executed by
the JVM, so it doesn't require creating an object to invoke the main() method. So,
it saves memory.

○ void is the return type of the method. It means it doesn't return any value.

○ main represents the starting point of the program.

○ String[] args or String args[] is used for command line argument. We will discuss
it in coming section.

○ System.out.println() is used to print statement. Here, System is a class, out is an


object of the PrintStream class, println() is a method of the PrintStream class. We
will discuss the internal working of System.out.println() statement in the coming
section.

To write the simple program, you need to open notepad by start menu -> All Programs
-> Accessories -> Notepad and write a simple program as we have shownbelow:
As displayed in the above diagram, write the simple program of Java in notepad and
saved it as Simple.java. In order to compile and run the above program, you need to
open the command prompt by start menu -> All Programs -> Accessories -> command
prompt. When we have done with all the steps properly, it shows the following output:
What happens at runtime?
At runtime, the following steps are performed:

Classloader: It 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.

How to set path in Java


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.
However, if you have your Java file outside the JDK/bin folder, it is necessary to set the
path of JDK.

There are two ways to set the path in Java:

1. Temporary

2. Permanent

1) How to set the Temporary Path of JDK in Windows


To set the temporary path of JDK, you need to follow the following steps:

○ Open the command prompt


○ Copy the path of the JDK/bin directory
○ Write in command prompt: set path=copied_path

For Example:

set path=C:\Program Files\Java\jdk1.6.0_23\bin

Let's see it in the figure given below:


2) How to set Permanent Path of JDK in Windows
For setting the permanent path of JDK, you need to follow these steps:

○ Go to MyComputer properties -> advanced tab -> environment variables -> new
tab of user variable -> write path in variable name -> write path of bin folder in
variable value -> ok -> ok -> ok

For Example:

1) Go to MyComputer properties
Now your permanent path is set. You can now execute any program of java from any
drive.

Setting Java Path in Linux OS


Setting path in Linux OS is the same as setting the path in the Windows OS. But, here we
use the export tool rather than set. Let's see how to set path in Linux OS:

export PATH=$PATH:/home/jdk1.6.01/bin/

Java Variables
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.

Variable is a name of memory location. There are three types of variables in java: local,
instance and static.
1) Local Variable
● A variable declared inside the body of the method is called local variable.
● A local variable cannot be defined with "static" keyword.

2) Instance Variable

● A variable declared inside the class but outside the body of the method, is called
an instance variable. It is not declared as static.
● It is called an instance variable because its value is instance-specific and is not
shared among instances.

3) Static variable
A variable that is declared as static is called a static variable.
Example to understand the types of variables in java
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12. }//end of class

You might also like