we have learned about the first java program, how to compile and run a java program. Here we are going to learn, what happens while compiling and running the java programs.
What happens at compile time?
class Test
{
public static void main(String args[])
{
System.out.println("hello java");
}
}
At the run time following steps are performed:
}
}
What happens at run time?
At the run time following steps are performed:
(1) Class File
(2) Classloader
(3) Bytecode verifier
(4) Interpreter
(5) Runtime
Classloader: classloader is the subsystem of jvm that is used to load the class file.
Bytecode Verifier: checks the code fragments for illegal code that can violate access right to objects.
Interpreter: read bytecode stream then execute the instruction.
Java programming language is WORA(Write Once Run Anywhere) programming language because of this bytecode. Java bytecode is portable. Java bytecode is platform independent.
java file(source file)→compiler→.class file(bytecode)
What is bytecode in java?
Bytecode in java is just a symbol and each symbol taking one byte in memory that is the reason is called bytecode. Bytecode(.class file) is generated by the java compiler after compiled the java source code(java file). After bytecode is generated, these bytecode are loaded into java virtual machine(JVM) and then interpreted and then execute.Java programming language is WORA(Write Once Run Anywhere) programming language because of this bytecode. Java bytecode is portable. Java bytecode is platform independent.
java file(source file)→compiler→.class file(bytecode)
Q. can you save java source file by other names than the class name?
class Test
{
public static void main(String args[])
{
To Compile : First.java
To Execute : Test
Q. Can you have multiple classes in java source file?
Ans. yes.Q. Can we keep more than one main method in a single java class?
Ans. Yes, we can keep more than one main method in a single java class.For example:
class Demo
{
public static void main(String args[])
{
System.out.println("hello java from actual main method");
}
public static void main(int args[])
{
System.out.println("hello java from another main method");
}
}
output: hello java from actual main method
Here will be executed only one main method psvm(String args[]) by default.
There are some points or rules in java
- In java we have to write every statement in the class block, It is the mandatory rule.
For example:
class First
{
------
------
------
}
- Java is a case sensitive language.
For example:
suppose we declared the main method in java
public static void main(String args[])
Public static void Main(String args[])
here first main method is right and second main method is wrong because here P in Public is capital and M in Main is capital.
- Always keep the first letter of your class name as a capital because of its increase the readability of your code. you can also use the first letter as small, but the first letter of a class name is good for you code readability.
For example:
class Capital
{
-------
-------
-------
}
Here C is capital in class name Capital.
- If you want to make any class of java executable then it must have a function or method with the name of 'main'.
For example:
class Simple
{
public static void main(String args[])
{
--------
}
}
0 comments:
Post a Comment