Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday, 9 December 2016

Internal details of Hello Java Program


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?

At compile time, java file is compiled by java compiler and converts the java code or source code into bytecode(.class file).
                        
class Test 
{
public static void main(String args[])   
{
System.out.println("hello java");
}
}
java source code

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
(6) Hardware
java run time


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.


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?

Ans. yes, we can if a class is not public.

class Test
{
public static void main(String args[])  
{
System.out.println("hello java");
}
}
save it  First.java

To Compile : First.java
To Execute : Test

Q. Can you have multiple classes in java source file?

Ans. yes.

multiple classes in a java source file


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[])
{
--------
}
}
 
                                                    
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate