0% found this document useful (0 votes)
15 views

Lecture

Uploaded by

Khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture

Uploaded by

Khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Introduction To Java Programming Language

(Elective-1)

Lecture 2
Today’s Agenda

Moving Ahead with JAVA

• Editions Of Java

• Difference Between JDK,JRE and JVM

• Downloading and Installing JDK

• Writing First Java Application

• Complete Explanation Of First Application


Editions/Flavours Of Java

• When java was originally released in 1996, it was just


java and no such thing as “editions” was there.

• But as use of java increased , then in 1999 SUN


categorized it into 3 editions called J2SE,J2EE and
J2ME .

• Later on in 2006 they changed the naming and called


them as JSE,JEE and JME.

• These editions were named based on the kind of


application which can be developed by learning that
edition.
JSE(Java Standard Edition)

• This is the most basic version of java and it provides us


core concepts of java language like the basic types and
objects as well as high-level classes that are used for
networking, security, database access, graphical user
interface (GUI) development etc.

• Since it teaches us core concepts of java that is why


many people call it CORE JAVA , although SUN never
gave this name.

• Used for developing desktop applications like


calculators, media player, chat applications etc
JEE(Java Enterprise Edition)

• The Java EE which stands for Java Enterprise Edition


is built on top of the Java SE platform and is a
collection of libraries used for building "enterprise
applications" (usually web applications).

• In simple terms we can say JEE is used for developing


applications which run on servers.

• Some popular applications developed using JEE are


amazon.in,alibaba.com,irctc.co.in,ideacellular.com,airt
el.in etc
JME(Java Micro Edition)

• Java ME is the slimmer version of Java targeted


towards small devices such as mobile phones.

• Generally people tend to think of the micro edition as


the mobile edition, in reality, the micro edition is used
not just for mobile phones, but for all kinds of devices,
such as television sets, printers, smartcards and more.

• But as smartphone technology arrived the use of JME


has reduced as Android has superseded it.
Java Editions
JDK v/s JRE v/s JVM

• Understanding difference between JDK,JRE and JVM


is very very important in java for interviews

• JDK: Java Development kit

• JRE: Java Runtime Environment

• JVM: Java Virtual Machine


What Is JVM ?

• JVM is an abstract machine that can execute


precompiled java programs.

• In simple terms it is the code execution component of


java

• It is designed for each platform(OS+CPU) supported by


java and this means that every platform will have a
different version of JVM
QUIZ

• Why JVM is called a virtual machine ?

JVM is called virtual machine because it is a software layer but it


behaves as if it is a complete machine(platform). That is all the
tasks which are done by a machine while running a program
in other languages like C , are actually done by JVM in java.

For example: Starting the execution by calling main( ),


Allocating Memory for the program,Memory cleanup etc
What JVM Contains?

• Apart from garbage collector and other important tools ,


the JVM contains 2 translators:

• Interpreter

• JITC (Just In Time Compiler)


Interpreter V/s JITC

• Originally java only had an interpreter which simply


converted bytecode to machine language and sends it
for execution to the computer.

• As we know interpreters convert line by line, so the java


interpreter converts one line of bytecode to machine
language and sends it for execution to the computer.

• Then it repeats the same process for line 2


Is there a problem in this approach?

• Yes,
because if an instruction needs to be executed 100
times (for example inside a loop) then the interpreter
needs to convert it 100 times.

• For example: a=10;


a=10; loop(i<=100)
print a; {
print a;
}

In the second example the interpreter will have to convert the


statement print a; 100 times since it is running inside a loop
Solution is JITC !

• To solve this problem the developers at SUN came up with a new


idea.

• They thought if a statement has to be executed multiple times then


why not save the converted code in computer’s memory and then
next time just execute it without converting it.

• That is in the previous code , the statement print a; which is inside


the loop would be converted only once and saved in computer’s
memory and remaining 99 times it is just executed !.

• This is called JITC(Just In Time Compiler) and the statements


which have to be executed multiple times are called HotSpots and
thus SUN’s JVM is also called HotSpot JVM
QUIZ

• Are java compiler and JITC same?

No , not at all.

The java compiler converts source code to bytecode and is not a


part of JVM , rather it comes with JDK.

The JITC lives inside the JVM and converts bytecode to machine
understandable form
What Is JRE ?

• JRE is an acronym for Java Runtime Environment

• It contains a JVM along with java classes/packages and


set of runtime libraries.

• So the JVM , while running a java program uses the


classes and other libraries supplied by JRE.

• If we do not want to write a java program , and we just


want to run it then we only need a JRE
What Is JDK ?

• JDK stands for Java Development Kit and is a


bundle of software that we can use to develop Java
based applications.

• It includes the JRE, set of library classes, Java


compiler,jar and additional files needed to write Java
applications.
JDK,JRE and JVM
QUIZ

• Can I compile a java application if I have a JRE


?

• Yes

• No

• JRE can only be used to run a Java application .


It doesn’t contain the javac tool which is used
for compilation
Downloading JDK

 STEP 1: Go to
http://www.oracle.com/technetwork/java/javase/do
wnloads/index.html
Step 2 : Accept The License

Click this radio button


Step 3 : Choose your version
 Step 4:

Choose Windows x86 if you are using Windows 32 bit


and Windows x64 if you are using Windows 64 bit.

This version compatibility is very important for further


other installations which you might need like web server
installation, Eclipse IDE installation etc
Installing JDK

 Step 5:

Now double click on the setup file which you have


downloaded .

The name of the file would be jdk-8-u66-windows-x64.exe


or jdk-8-u66-windows-i586.exe .

Just keep clicking next option and finally the setup will
run and java would be installed on it’s default path
which is c:\program files\java
Verifying Installation

 Go to the specified path, where you install your java.

 There you will see a java folder

 Inside the java folder you will see two folders, one is
would be jdk1.8.0_XX and other is for jre
Go to start type cmd and open it by double click on
it and go to javas bin folder and type java –version
as shown below:-
Developing Java Programs

Java Program Development

➢Using Notepad ➢Using an IDE like


Typing the code in Netbeans/Eclipse etc
notepad and running it
through command This approach should be used
prompt. This approach is after we have understood the
basic working of java . Because
good for beginners for
as an IDE hides all the basic
learning each step steps which are very important
thoroughly to understand
Developing Java Programs
Using Notepad

• Developing and running a program requires three


main steps:

• Writing the source code

• Compiling the code


(Conversion from source code to bytecode)

• Executing the code


(Interpretation of the bytecode)
Step 1-Writing the Source Code

• Select notepad.exe from the list shown in pop


up menu of run command.

• Right click and choose “run as administrator”


option.

• Now type the code given in next slide


Step 1-Writing the Source Code

class Test
{
public static void main(String [ ]args)
{
System.out.println(“Hello User”);
}
}
Understanding The Program

First of all we must remember that java is a highly case sensitive


language.

It means that we have to be very careful about uppercase and


lowercase letters while typing the code.

For example, in the previous code three letters are compulsorily


in uppercase and they are
“T” of Test ,
“S” of String and
“S” of System.

This is because in java class names begin with upper case


Understanding The Program

The first statement of our code is:

class Test

Since java is an Object Oriented Language and it


strictly supports Encapsulation so every java
program must always contain at-least one class and
whatever we write must appear within the opening and
closing braces of the class
Understanding The Program

The second statement is

public static void main(String [ ] args)

In java also(like C/C++) the entry point of


execution of our program is the method main( ) which is called
by the JVM
The words shown in blue are keywords and each has a
different meaning and purpose for the method
main( ).

Lets understand each of them in detail


Why main( ) is public ?

 “public” is an access modifier and in Object Oriented


Programming any method or variable which is
declared public can be accessible from outside of that
class.

 Since main( ) method is public in Java, JVM can


easily access and execute it.
Why main( ) is static ?

 Every class can have two kinds of methods ,


nonstatic and static.

 A method which is nonstatic can only be called using


object of that class , while a static method can be
called without any object , simply using class name.

 When the JVM makes are call to the main( )


method there is not object existing for the class being
called therefore it has to have static method to allow
invocation from class.
Why main( ) has return type void ?

 The keyword void is called return type which


indicates that no value will be returned by the
method to it’s caller.

 Since main() method in Java is not supposed to


return any value to the JVM, its made void which
simply means main() is not returning anything.
Can we change/remove the keywords used
with main () ?

 No , not at all.

 This is because main( ) is called by JVM and to allow


JVM to successfully call main( ) these keywords are
important.

 If we forget to write these keywords then although the


code will compile but will fail to run.

 All we can do is change the order of public and static but


we can’t drop them
What is String [ ] args ?

 String is a predefined class in java

 So the statement String [ ] args is declaring args to be an


array of Strings.

 It is called command line argument and we will discuss it


later.

 For now, just remember that the statement String[ ] args


has to be present with main( ) otherwise code will not
run
Can we change/drop String [ ] args ?

 No , just like keywords used with main are compulsorily ,


similarly String [ ] args is also compulsory.

 All we can do is change the name from args to something


else.

 Also we can interchange the array name and [ ] .

 For example: String args[ ], String [ ] args,


String [ ]str,String str[ ] all are valid
Understanding System.out.println( )

 Now let’s understand code in the body of the


main( ) method, which will print a message on the
console.
Syntax:
System.out.println(“ message ”);

 System is a predefined class .


 out is an object reference (not object).
 println( ) is a method.
 Together all three are used for displaying text on console.
 We will discuss this part in detail once we have covered
basics of java
QUIZ

• Does every method has to be public , static and


void ?

• Yes

• No

No , it is not a compulsion. This is only with the


main() method that we have to make it
public,static and void . All other methods have
declarations as decided by the programmer
QUIZ

• Will a java program compile without main( )?

• Yes

• No

Yes, because main( ) is not needed for


compilation . But is used for execution of the
code. So we can compile a java program
without main , but we cannot run it.
QUIZ

• What of the following are correct declarations


of main( ) for java compiler?

A. public static void main(String [ ] args)


B. static public void main(String [ ] args)
C. public static int main(String [ ] args)
D. public static void main(String args)
E. public static void main(String args[ ])
F. public static void main(String x)
G. static public void main(String [ ] x)

Answer: All are correct


QUIZ

• What of the following are correct declarations


of main( ) for JVM?

A. public static void main(String [ ] args)


B. static public void main(String [ ] args)
C. public static int main(String [ ] args)
D. public static void main(String args)
E. public static void main(String args[ ])
F. public static void main(String x)
G. static public void main(String [ ] x)

Answer: A,B,E,G
End Of Lecture 2

You might also like