JDK-Java Development Kit
JRE-Java Runtime Environment
JVM-Java Virtual Machine
JDK- Used to develop Java Programme. It has development tools that are
used for compiling the java programme and for debugging also
JRE-Once the programme is compiled then we need to execute it, it can be
done using JRE, This JRE has library of java classes
This execution of java programme inside JRE is actually done using this
JVM(Present inside the JRE)
You write a Java program and save it in a file with a .java extension. For
example, you create a file named [Link].
1. Writing the Java Program
You write a Java program and save it in a file with a .java extension.
For example, you create a file named [Link].
2. Compilation with javac
javac is the Java compiler provided by the Java Development Kit
(JDK).
When you run javac [Link], it translates the human-readable Java
source code into bytecode, which is an intermediate, machine-
independent code.
The bytecode is stored in a file named [Link]. This file contains
instructions that the Java Virtual Machine (JVM) can execute.
3. Execution with java
To run the compiled program, you use the java command (e.g., java
First).
The java command looks for the bytecode in the .class file (e.g.,
[Link]), loads it into memory, and executes it.
SKELTON OF JAVA PROGRAMME:
Here import [Link].* is used to import all the classes of the [Link]
package but it not generally needed as it is done automatically
Every java programme is in form of class So there is nothing outside
classes in Java.
Everything must be inside the class and for writing the main function also
it must be inside a class.
Here class First is named like (name of the programme is [Link] so the
name of the class is class First
Here the class name can be other also for example “ class second “ also
we can save but when we compile the programme 3 files will be created-
[Link], [Link], [Link]
Also to run the programme after compiling we need to run as java Second
Inside the class First there is one main function here in java the function is
called as method
Public static void main(string[]args)
{
[Link](“hello world”);
Here the main method is of void type and also returns value of void type
RUNNING PROGRAMME:
To compile and run the java programme
Open the terminal
Type javac [Link] as here name of the file is [Link] and this javac
will compile this file and create a class named as [Link]-This
[Link] is a byte code that contains error free code
In case of any errors it will be displayed
Now again typing java demo this will run and execute the programme and
now the JVM will convert this byte code to machine code
And the output is been displayed
Explaining the code:
Public- So if you want anything from the class to be accessible outside,
then it must be public. If it is public, then it will be visible. You can access
it. Why it should be public. Because JVM when you say execute the
program, it will call this main method. So this main method is inside the
class. So JVM should be able to see that main method. So that's the
reason we have to say it is public.
Static-We cannot use a class without creating a object so if we want to
use something from class without creating a object then that should be
created as static
So if it is static, then it can be directly called without creating an object of
a class just by using class name. So yes, JVM has to call main method
which is present inside this class without creating an object it has to call.
So when we say java First, so what JVM will do, JVM will call first main, it
will call the function first. First. main, so just by using class name It will
call the function. So for making it easy for JVM. We have to write it as
static Otherwise if you don't make it as static, JVM says I cannot find main
method.
Println-Is a method to print to screen and it is having a bracket
System-starting with a capital letter is a class-again everything in java is
class
And out is a object that is present in the class System
Again inside main() method there is command line argument called as
String arg[]
Here String args[] is within the main method and String is a class
It is important to include string[]arg inside the main() method because the
JVM recogonise main() method as this type only-THE PROGRAMME WILL
BE COMPILED BUT IT WONT RUN
Note:
It is not mandatory to declare the class name as the file name itself but
when the class is declared as public then the class name should match
with the file name else error will occur
NOTE:
Here after writing [Link](arg[0]) and running java First the
output is getting displayed but with some errors
But if we run as java First Bye
here the code file is been run and output is been displayed
And additionally this Bye is also been displayed
This is because whatever we wrote after java first is going into arg[0] and
is getting displayed
NOW:
Here the Bye goes to arg[o] and all goes to arg[1] and are getting
displayed
NOTE:
Writing code in NOTEPAD:
Open command prompt
Type “md JavaCode”-This will create a directly named as JavaCode
md-make directory
then type “cd JavaCode”- This opens the JavaCode directory
Now open Notepad and write code
Save this file as ” [Link]” in the folder JavaCode and include in the
double quotes
To compile and run the code run the following commands
READING THE DATA FROM KEYBOARD:
Java has a class called as “ Scanner” that is used to read data from the
keyboard
This class called “Scanner” is present inside the package called as “util”
There are several methods present inside the Scanner class
To use this Scanner class we need to import the package as
Import [Link].*;
Here Scanner S=new Scanner([Link]);
A=[Link]()
Scanner: A class in the [Link] package used to read input from
various sources such as files, strings, and standard input (keyboard).
s: The name of the Scanner object. It refers to the Scanner class
new: A keyword in Java used to create new objects. Here, it creates
a new instance of the Scanner class.
Scanner([Link]):
o [Link]: Refers to the standard input stream, which is
typically the keyboard.
o This tells the Scanner to read data from the keyboard.
o While [Link] is used to print output
While in a=[Link]() here S is the scanner object will take the input and
the nextInt() will convert the input into integer form
Here actually nextInt() is a method in scanner class while there are many
methods apart from this
Common methods are nextInt() , nextFloat() , nextDouble()
While next() is used to take string input -one word
While nextLine() is used to take line input
While nextBoolean() takes Boolean value as input
If the value the we enter is a integer the hasnextInt() returns true
While [Link](“sum of the 2 integers is”+c);
Here instead of comma in python we used + to concatenate
NOTE:
(1)Suppose float x = [Link]();
And input is 24 then output will be printed as 24.0 because this 24 as
input is been converted into 24.0
(2) suppose String x = [Link]();
Then suppose input is “hello all” then the output will be just “hello”
To have multiple words we need to use nextLine()
NOTE:
suppose u define int x,y,z;
And give input as 24.5(float number) then the output will be error because
when we define integer the input must also be integer only
Run ths command “ javap [Link]” to find all the available
methods in the class scanner inside the util package
Here [Link](int) is used to make the scanner interprete the input as
binary then int x = [Link]() will convert the binary input as decimal
form
Here binary input means input must be either 1 or 0