Java Notes
Java Notes
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:
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.
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.
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.
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().
○ void is the return type of the method. It means it doesn't return any value.
○ String[] args or String args[] is used for command line argument. We will discuss
it in 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:
Bytecode Verifier: Checks the code fragments for illegal code that can violate access
rights to objects.
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.
1. Temporary
2. Permanent
For Example:
○ 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.
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