Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

Java Notes

The document provides information on writing a simple "Hello World" Java program. It explains the steps to install the JDK, create a Java class with a main method that prints "Hello World", compile the program with javac, and run it with java. It also defines some key Java concepts like classes, methods, and System.out.println used in the first Java program.

Uploaded by

Shivank Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Java Notes

The document provides information on writing a simple "Hello World" Java program. It explains the steps to install the JDK, create a Java class with a main method that prints "Hello World", compile the program with javac, and run it with java. It also defines some key Java concepts like classes, methods, and System.out.println used in the first Java program.

Uploaded by

Shivank Rana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

First Java Program | Hello World Example

• We will learn how to write the simple program of java.


We can write a simple hello java program easily after
installing the JDK.
• To create a simple java program, you need to create a
class that contains the main method. Let's understand
the requirement first.
• The requirement for Java Hello World Example
• For executing any java program, you need to
• Install the JDK if you don't have installed it, download
the JDK and install it.
• Set path of the jdk/bin directory.
• Create the java program
• Compile and run the java program
Creating Hello World Example
• Let's create the hello java program:
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
• Test it Now save this file as Simple.java
• To compile: javac Simple.java
• To execute:java Simple
• Output: Hello Java
Compilation Flow:
• When we compile Java program using javac
tool, 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 which 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 to create 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 is used for command line argument. We will learn it later.
• System.out.println() is used to print statement. Here, System is a class,
out is the object of PrintStream class, println() is the method of
PrintStream class. We will learn about the internal working of
System.out.println statement later.
• To write the simple program, you need to
open notepad by start menu -> All Programs -
> Accessories -> notepad and write a simple
program as displayed below:
• As displayed in the above diagram, write the
simple program of java in notepad and saved
it as Simple.java. To compile and run this
program, you need to open the command
prompt by start menu -> All Programs ->
Accessories -> command prompt.
• To compile and run the above program, go to
your current directory first;
• my current directory is c:\new.
• Write here:To compile:javac Simple.java
• To execute:java Simple
Valid java main method signature
• public static void main(String[] args)
• public static void main(String []args)
• public static void main(String args[])
• public static void main(String... args)
• static public void main(String[] args)
• public static final void main(String[] args)
• final public static void main(String[] args)
• final strictfp public static void main(String[] ar
gs)
Invalid java main method signature
• public void main(String[] args)
• static void main(String[] args)
• public void static main(String[] args)
• abstract public static void main(String[] args)
Resolving an error "javac is not recognized as an
internal or external command"?
• If there occurs a problem like displayed in the
below figure, you need to set path. Since DOS
doesn't know javac or java, we need to set
path. The path is not required in such a case if
you save your program inside the JDK/bin
directory. However, it is an excellent approach
to set the path.
What happens at compile time?
• At compile time, java file is compiled by Java
Compiler (It does not interact with OS) and
converts the java code into bytecode.
What happens at runtime?
• At runtime, following steps are performed:
• Classloader: 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 right 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:
– Temporary
– 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
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
Difference between JDK, JRE, and JVM
• A summary of JVM
• Java Runtime Environment (JRE)
• Java Development Kit (JDK)
• We must understand the differences between
JDK, JRE, and JVM before proceeding further
to Java. See the brief overview of JVM here.
• If you want to get the detailed knowledge of
Java Virtual Machine, move to the next page.
Firstly, let's see the differences between the
JDK, JRE, and JVM.
JVM
• JVM (Java Virtual Machine) is an abstract machine. It is called a
virtual machine because it doesn't physically exist. It is a
specification that provides a runtime environment in which Java
byte code can be executed. It can also run those programs which
are written in other languages and compiled to Java byte code.

• JVMs are available for many hardware and software platforms. JVM,
JRE, and JDK are platform dependent because the configuration of
each OS is different from each other. However, Java is platform
independent. There are three notions of the
JVM: specification, implementation, and instance.

• The JVM performs the following main tasks:


– Loads code
– Verifies code
– Executes code
– Provides runtime environment
JRE
• JRE is an acronym for Java Runtime Environment.
It is also written as Java RTE. The Java Runtime
Environment is a set of software tools which are
used for developing Java applications. It is used to
provide the runtime environment. It is the
implementation of JVM. It physically exists. It
contains a set of libraries + other files that JVM
uses at runtime.
• The implementation of JVM is also actively
released by other companies besides Sun Micro
Systems.
JDK
• JDK is an acronym for Java Development Kit. The Java Development
Kit (JDK) is a software development environment which is used to
develop Java applications and applets. It physically exists. It contains
JRE + development tools.
• JDK is an implementation of any one of the below given Java
Platforms released by Oracle Corporation:
• Standard Edition Java Platform
• Enterprise Edition Java Platform
• Micro Edition Java Platform
• The JDK contains a private Java Virtual Machine (JVM) and a few
other resources such as an interpreter/loader (java), a compiler
(javac), an archiver (jar), a documentation generator (Javadoc), etc.
to complete the development of a Java Application.

You might also like