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

Lect 2

Uploaded by

Sojal Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lect 2

Uploaded by

Sojal Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

What means Java is Platform Independent?

• Platform-independent means that the java compiled code(byte code)

{.class file} and executable code {.java file} can run on all operating

systems.
Compilation and Execution of Java Program
• Whenever, a program is written in JAVA, the javac compiles it and
gives .class file or the bytecode and it is machine non native code.

• The bytecode generated is a non-executable code and needs an


interpreter to execute on a machine. This interpreter is the JVM and
thus the bytecode is executed by the JVM.

• And finally the executable code is a sequence of machine instructions


that can be executed by the CPU.
Java is platform-independent but JVM is Platform Dependent

• Every system has its own JVM which gets installed automatically when
the jdk software is installed. For every operating system separate JVM is
available which is capable to read the .class file or byte code. JVM is
platform-dependent and hence Java is able to become “Platform
Independent”.
Difference between JVM, JDK, JRE and JIT.
• Java Development Kit (JDK) contains JRE , various development tools
like Java libraries, Java source compilers, Java debuggers, bundling
and deployment tools.
JVM is heart of JAVA

• JDK is for development purpose whereas JRE is for running the java

programs.

• JDK and JRE both contains JVM so that we develop and run our java

program.
• JIT compiles bytecode to native machine code to optimize efficiency.

JVM without JIT interprets the same sequence of bytecode repeatedly

and incurs a longer execution time.


Your First Java Program
class Sample everything needs to be placed
{ inside a class. Class is a keyword .
Sample is a Java identifier that
public static void main( String args[ ]) specifies name of the class.
{
System.out. println ( “Java”); public static void main
} public : access specifier that
} shows Sample class is unprotected
and is accessible to all other
classes.
• System refers to a final class that we can find in the java.lang.package
and this System class has a public and static class called PrintStream.
• All instances of the class PrintStream have a public method called
println()
• out is an instance of the PrintStream class
static: static is put before main() every method in Java should be
because interpreter uses it before part of an object. Here, println
objects are created. method is a member function of
the out object. This out object is a
void: main method does not static data member of System
return any value ( but simply class.
prints some text on the screen .)
println () : always shows output
String args[] : declares array on a new line.
named args , it is array of objects
of class type String.
Command line Arguments
class CommandLineExample compile by > javac CommandLine
{ Example.java
public static void main(String run by > java CommandLineExam
args[]) ple sat
{
System.out.println("Your first o/p: sat
argument is: "+args[0]);
}
}
class CommLineArgs javac CommLineArgs.java
{ java CommLineArgs Simple, OO, Robust,
p.s.v.m( String args[]) Multithreaded, Secure, Portable
{
int count, i=0, String str;
count= args.length; o/p
S.o.p (“number of arguments=“+count); number of arguments =6
while ( i < count) 1 : Java is Simple!
{ str= args[i]; 2: Java is OO!
i= i+1; 3: Java is Robust!
S.o.p ( i+”: “ + “Java is “ +str+ “!”); 4: Java is Multithreaded!
} 5: Java is Secure!
} 6 : Java is Portable!
}
2nd Example
import java.lang. Math; y= Math.sqrt(x): invokes the method sqrt
of the Math class.
class SquareRoot
{ p.s.v.m ( String args[])
S.o.p (“y=“ +y)
{ double x=5;
here + acts as the concatenation operator
double y; of 2 strings. The value of y converted into
y= Math.sqrt(x); a string representation before
concatenation.
S.o.p(“y =“+y);
} import java.lang. Math : to instruct
} interpreter to load the Math class from
sub package lang that belongs to package
java. sqrt is the method of Math class .
3rd Example
class Box
.
{ float len;
float breadth;
void getdata ( float a, float b)
{
len= a; beadth=b;
}
class BoxArea
{ p.s.v.m ( String args[ ])
{ float area;
Box mybox = new Box( );
mybox.getdata( 14, 10);
area= mybox.len * mybox.breadth;
S. o. p (“Area=“ +area);
}
}
new is a Java keyword. It creates a Java object and allocates memory for it on the heap.
Understanding Reference variable

1. Reference variable is used to point object/values.

2. Classes, interfaces, arrays, enumerations, and, annotations are reference types in Java.
Reference variables hold the objects/values of reference types in Java.

3. Reference variable can also store null value. By default, if no object is passed to a reference
variable then it will store a null value.

4. You can access object members using a reference variable using dot syntax.

You might also like