Head First Java Chapter 01 summary
* What Is Java
Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the name from Oak to Java.
* How Java Works
1) Source — Create a source document. Use an established protocol (in this case, the Java language)
2 ) Compiler — Run your document through a source code compiler. The compiler checks for errors and won’t let you compile until it’s satisfied that everything will run correctly.
Compile the Party.java file by running javac (the compiler application). If you don’t have errors, you’ll get a second document named Party.class The compiler-generated Party.class fi le is made up of bytecodes
3) Output /Byte Code — The compiler creates a new document, coded into Java bytecode. Any device capable of running Java will be able to interpret/translate this fi le into something it can run. The compiled bytecode is platform independent.
4) JVM — Your friends don’t have a physical Java Machine, but they all have a virtual Java machine (implemented in software) running inside their electronic gadgets. The virtual machine reads and runs the bytecode.
Run the program by starting the Java Virtual Machine (JVM) with the Party.class fi le. The JVM translates the bytecode into something the underlying platform understands, and runs your program. What Is JVM
* Code structure in Java
Within the curly braces of a method, write your instructions for how that method should be performed. Method code is basically a set of statements, and for now you can think of a method kind of like a function or procedure.
* Anatomy of a class
When the JVM starts running, it looks for the class you give it at the command line. Then it starts looking for a specially-written method that looks Main method
Next, the JVM runs everything between the curly braces { } of your main
method.Every Java application has to have at least one class, and at least
one main method per application.
* Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
String
- stores text, such as "Hello". String values are surrounded by double quotesint
- stores integers (whole numbers), without decimals, such as 123 or -123float
- stores floating point numbers, with decimals, such as 19.99 or -19.99char
- stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean
- stores values with two states: true or false
* Java Data Types
As explained in the previous chapter, a variable in Java must be a specified data type:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String
* Java Conditions and If Statements
Java supports the usual logical conditions from mathematics:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
The if Statement-Use the if
statement to specify a block of Java code to be executed if a condition is true
.
if (20 > 18) {
System.out.println("20 is greater than 18");
}
Note that if
is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the condition is true
, print some text:
* Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
While Loop — The while
loop loops through a block of code as long as a specified condition is true
:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
For Loop- When you know exactly how many times you want to loop through a block of code, use the for
loop instead of a while
loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Example explained
- Statement 1 sets a variable before the loop starts (int i = 0).
- Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.
- Statement 3 increases a value (i++) each time the code block in the loop has been executed.
* Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;
We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal — place the values in a comma-separated list, inside curly braces:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
- The assignment operator uses one equals sign =.
- The equals operator uses two equals sign ==.
- A while loops runs everything within its block, as long as the conditional test is true.
- If the conditional is false, the while loop code block won’t run, and execution will move to the code immediately after the loop block.