-----------------------------------------------------Compile time VS
Runtime------------------------------------------------------------------
As ,we know computer cannot understand the human readable programming language like
c++, java , so we need some mechanism to convert this high level programming level
into machine language.
Here compiler and interpreter comes into picture.
Both converts the high level program into machine language.
Lets explain it with an analogy ,suppose in a restaurant there are two waiters name
Rashmika and sai Pallavi .
Rashmika takes all the order at once and delivers it in one go.
While, sai Pallavi takes one order at a time, delivers order and then take second
order and so on.
( Rashmika is same as a compiler )
Takes all the code in one go, converts code in machine language and give to system
for processing and share output with the user.
If error comes program won't get executed
Program :
line 1 No output
line 2
line 3
line 4
Error
Programming lanugages used : C,C++;
( Sai Pallavi is same as a interpreter )
Takes one instruction at a time, converts first instruction to machine language and
gives it to system for processing and generate output and then convert second
instructions.
If error comes it stop execution from that point
Program :
line 1 output1
line 2 output2
line 3 output3
line 4 output4
Error Error
Programming lanugages used : Perl,python;
-----------------------------------------What Makes Java a Platform Independent
Language?-------------------------------------------------------
First we will understand about platform independent,
whenever we write a c or c++ code our compiler or interpreter will convert that
into machine code then our computer will execute it.
But machine code is highly dependent on the operating system of our computer.
Ex : if i have written a c++ program in my system with windows 10 application then
compiler will convert that into machine language, then my system will execute it.
suppose i want to run the same machine code in any other machine with Linux
operating system then it will [Link] we want to run it in Linux then we have to
again compile the code here.
That means program written is c++ is platform dependent meams depends on the
operating system of the computer.
Lets see how java solve this issue:
Java program first convert java program into byte code by using the JavaC compiler
and the byte code needs an interpreter for its [Link] interpreter is known
as JVM that Java virtual Machine.
Java program will not be executed by the operating system,it will be only executed
by the JVM and JVM is different for different operating system.
Java program ---Java compiler(JavaC)---> Byte code ---Interpreter(JVM)--> for
execution
So java uses both a compiler as well as interpreter.
Java is Platform independent because program is executed by JVM not by the
operating system.
-----------------------------------------------------JVM , JRE ,
JDK----------------------------------------------------------------------------
JVM( Java virtual machine ) - It is used for execution of the byte code.
JRE( Java runtime environment ) - For the execution of the byte code JVM needs some
tools like compiler these all are called the JRE
JDK (Java development kit)- JVM + JRE
-----------------------------------------------------------------Java VS C+
+-------------------------------------------------------------------
---------------------------------------------------Why we use public static void
main in java?--------------------------------------------------------------
Java program is executed by the JVM which is an interpreter.
Every method and every function is written inside a class , so when JVM executes a
program , it will first search for main method because it is the starting point of
execution.
As ,we know when any method is public then only we can access the method outside
the class.
So main method is declared as public so that JVM can access it outside the class.
For access any non-static method outside the class we need to create an Object of
that class and then we can access that method through that class.
On , the other hand for accessing the static method we don't need to create
object of that class. so main method is static so that it JVM can access the static
method without Object creation.
Main method does't return anything so its return type is void.
Once the main method finishes its execution the java program terminates.
--------------------------------------------------Can we Compile and Run Java
program without main method--------------------------------------------------
Yes if we are using Java 6 or earlier versions we can compile as well as run the
java program without main method.
We can compile and run program through a static block , we can write our entire
program in static block.
Java will execute the static block even before the static method, but once the
static block is executed java will throw an exception saying that main method not
found.
To avoid this exception we can add [Link](0); at the end of the static block.
However,in java 7 or new version we can compile without main method but cannot run
program without it.
-------------------------------------------------Can we overload main method in
java?--------------------------------------------------------------------
method overloading means creating multiple methods with same name but with
different parameters.
Yes, we can overload main method in Java.
JVM will automatically detect the actual main method and executes [Link] if u want
to use the overloaded main method then, we have to call it in our actual main
method.
public class Main
{
public static void main(int a){
int sum = a + 2;
}
public static void main(String[] args) {
Main m = new Main();
[Link](2);
}
}
-------------------------------------------------------What is [Link]()
in java-----------------------------------------------------------------
It is used to print data on the screen.
How other programming languages print data : we need to use the methods of a
PrintStream Class which is used to print something on screen.
In java we have Println method which is used a method of a printStream class and
it is used for printing.
In java we can't directly create an object of a printStream class, so for
accessing the printStream class java provides us with a System class which
contains an object of a print stream.
This object js called out a defined as a static :
Static Out - PrintStream
And, as we know we can call static member of a class directly with a class name .
so we can call Out Object as a [Link], we have written first System which is a
final class then call its static member which is out and it is an object of a print
stream class and after that we can call the println() method which is defined in
PrintStream class.
---------------------------------------------------------Final keyword in
java-----------------------------------------------------------------------------
When we don't want to allow the modification of values in the program .
We can use the final keyword in three places:
*variables
*functions
*Classes
If we are using the final keyword in the variable then, we cannot modify it through
out the entire program .
So , it stop the value modification.
final int a = 2;
now we cannot modify the value.
a = 5;
will throw an error.
If we are using the final keyword with a function or method it will stop overriding
of the function means we cannot write the specific implementation of the method of
base class in the child class.
If we are using the final keyword in the Class , then it will stop inheritance
means we cannot create a child class from a base class.
----------------------------------------------------Why Strings are Immutable In
Java----------------------------------------------------------------------
Immutable = One defined cannot be changed
Suppose a string str1 = "String1" , does is mean we cannot change its value to
something else, ofcourse we can.
str1 = "new value"
Then, what does immutable String means!
First see how String are stored in the backend of java.
Java stores String in separate space which is know as String Pool.
When we create a String1 = "CodeVerse" , then it will create an Object in a
stringPool with a value as |CodeVerse| and String will point to CodeVerse.
String1 -----------> CodeVerse
If we want to create another String2 with the same name , it will not create
another object it will just point to the same old object |codeVerse|.
If i want to modify the value of String1 to something else String1 =
"SomethingElse" then java will create another object of String1 called |
SomethingElse|.
In another programming language , if we try to use the = operator , then it will
modify the existing value of the string. But in java instead of modifying the
object value in string pool, java creates a new object of that string.
We cannot modify objects in string pool once created, so every time when we try to
change string value java creates new object in string pool so strings are immutable
in java.
Let's understand why java developers decided to make the strings immutable:
* For memory saving
( when we create 10 strings with same value ,so instead of creating
10 object Java will create only one object in Stringpool and all 10 strings will
point to it )
It saves a lot of memory.
* Makes efficient Multithreading
( If multiple threads are accessing the String Object and trying to modify the
value at the same time, then it will create a serious security issue )
Immutable strings make multithreading more efficient in java.
* Make efficient Collections
( String are used in collections as key of collections so frequent modification in
string may cause in modification in Collections )
-------------------------------------------------------why we use static variables
in java?----------------------------------------------------------------