Fundamentals
Hybrid Language
Java file is firstly compiled using javac compiler.
And .class file is generated.
Then It is executed using interpreter inside JVM, so, no new file is generated at this stage, since Interpreter is used.
Platform Independent
Java language is platform independent, because all the system calls are handled by JVM.
So, after compiling the Java program, we can port the compiled(.class) file to other OS, and JVM for that particular OS can execute the .class file.
But in languages like C++, system calls are directly inside the compiled(.exe) file.
So, we cannot port the compiled file to other OS.
Running a Java program
Firstly, we need to compile the Java program.
A .class file is generated after this. (First.class)
Then, we'll run the Java program.
Java File Name
It is NOT compulsory to have file name same as class name.
It is compulsory only when the class is declared public.
Case 1
We can run the program,
Case 2
We can run the program,
Here, we need to take care when we are executing java byte code.
This happens because generated .class file name will have the same as the class name. (Second.class)
Case 3
Try running this program:
This will generate error, since class Second is declared public.
So to solve the problem, we compulsory has to have same class name as file name.
main() in Java
If the main() doesn't contain String[] args as parameter, still the program will be compiled successfully.
But there will be an error when executing the program.
Output in Java
Input in Java
To take input we need a object of Scanner class.
Class has many methods, for input of different data types.
nextBoolean() Reads a boolean value from the user
nextInt() Reads a int value from the user
nextLong() Reads a long value from the user
nextFloat() Reads a float value from the user
nextDouble() Reads a double value from the user