Chapter 01 (Head First Java)
The Java Virtual Machine (JVM) serves as a run-time engine for Java programs. The main method in Java programming is really called by the JVM. Java programs are referred to as Write Once Run Anywhere. This implies that a programmer can create Java code on one system and expect it to execute correctly on any other machine that Java-enabled. All of this is made possible by JVM.
What will you do in Java?
when you develop a Java program, a JVM instance and JVM are created. Your java code will be handled by jvm by being converted from machine code to byte code. JVM will take care to read your class file and translate it into a language that your operating system can understand when you execute your program.
Source code : For a computer to perform a task, a program or piece of code must be written. It’s known as source code.
Compiler : Verify whether your source code is error-free.
JVM : Your java code will be handled by jvm by being converted from machine code to byte code. And runs your program.
Code structure in Java
One class definition can be found in a source code file. There are methods inside the class that describe what must be done, and each method includes one or more statements that detail how to do it.
eg: In the Dog class, the bark method will hold instructions for how the Dog should bark.
Therefore, we must include instructions about how to execute the method inside the method. A series of statements constitutes a method’s code.
Anatomy of a Class
The JVM searches for the class you specify on the command line when it first starts up. Therefore, the main method of your class must be public, static, and void. The JVM instance will then use this method to run. from there onwards and you can drive your program through this method.. A minimum of one class and one main method must be present in every Java program.
Your program begins to run in the main() method. regardless of how huge your program is. A main() method is required to start things off.
By writing a program, we can instruct a computer to perform a task. Probably, you can tell jvm to do something in your code.
eg:
and by utilizing loops, you can program it to perform an action repeatedly.
eg:
and is able to perform tasks within certain conditions.
eg:
If we examine the syntax,
each statement must end in a semicolon. eg: x = x + 1;
a single-line comment begins with two forward slashes. eg: x = 22;
most white space doesn’t matter. eg: x = 3;
Variables are declared with a name and a type. eg: int weight;
Classes and methods must be defined within a pair of curly braces.
eg:
public void go () {
//code
}
The three common looping constructs in Java are while, do while, and for. Let’s examine do while,
What is taking place in do while is similar to the way that if you are already doing something, you will keep doing it as long as a certain condition is true. Everything you do is contained in the loop block. Whatever you want to repeat must be contained inside the loop block, which is surrounded by a pair of curly braces.
As you can see in the above image, a conditional test is being used inside the while condition, and the result should either be true or false. As a result, in that example, the < (less than) operator determines whether or not ‘I’ is less than 5 or not. When ‘I’ reach 5, the while loop will trigger and the outcome will be “true.”
Conditional branching
The Java if statement is used to test the condition. It checks boolean condition: true or false.
Only if the statement (x is > y) is true will the code above cause the line that outputs “x is greater than y” to be executed. Consequently, one statement will print out depending on the value of x. But we can add an else to the condition, lets see,
Only if the statement (x is > y) is true will the code above cause the line that outputs “x is greater than y” to be executed. Otherwise, “X is not greater than Y” will be printed. Consequently, one statement will print out depending on the value of x.