Learn Java Programming
1.0 Language basics
The Java programming language has grown from its humble beginnings to cover almost all types of programming, be it programming devices to enterprise computer systems.
To become an expert Java programmer, its necessary to go beyond just the language, and see practical application of the language features to common solutions.
- Understand the Java programming language
- Learn solution techniques and best practices while using the Java programming language.
- Learn to develop web applications
- Learn the tools and framework that would help become a power programmer
In short, the goal of this series is to make you an expert Java programmer, whatever your current level of expertise will be.
One of the reasons why the Java programming language has been so popular is because of its promise of write-once, run-anywhere. What this means is that your java program, that you can compile on your Windows operating system, for example, will run without any changes on the Linux or Mac OS operating systems.
How is this possible?
How does Java programming language make the write-once, run-anywhere rule work across platforms and operating systems?
The word platform is used to denote the diverse and different hardware and operating system (your hardware could be Intel-based, or AMD, or a Power-PC architecture, but whatever be the underlying hardware, your Java program will run the same on all the platforms)
The Java programs that you write are compiled into a format called bytecode. These bytecode is then converted into machine instructions by the Java Virtual Machine (JVM). Multiple JVMs exist for each of the different platforms (there is a JVM for the Microsoft platform or the Linux and Mac OS operating systems). This JVM will take your .class file and generate machine code for the target platform on which your code will be executing. This is how your program runs the same on the different hardware platforms.
Some basic rules of the Java Virtual Machine specification:
Let us look at the following code:
public class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
-
- The public static void main (String args[]) method is the first method that will get called when the JVM executes this class.
- You will have to save this file as HelloWorld.java. It is mandatory we name the file as the class containing the main method. Since the HelloWorld class contains the main method, we have to name the file as HelloWorld.java
The fundamental building blocks of the Java programming language:
Variables
We know that in the computer data is stored in binary format in a series of 0s and 1s. It is the high level language that helps us make meaning of this strings of 0s and 1s and allows us to program in a English-like language
For example, let us say I want to store my name and age in the computer memory.
Ultimately, I know when it is stored in the computer memory it will be a group of 0s and 1s. But the data I am going to store, the name and age have two different characteristics.
For example
- name – can contain the values from the English language a to z
- age – has to be a number value – cannot contain the alphabets – and should be not be a decimal (we do not tell anyone that I am 25.43 years old, do we?!)
So, in the above example, name and age represent some information, and they have some basic characteristics.
Both name and age represent information and have a specific data-type.
The name and age identifiers, I use to represent my name and age, can also be used used to represent your name and age, then the identifiers name and age represent something called a variable.
public class Welcome {
public static void main(String args[]) {
String name = "My name";
int age = 25;
System.out.println("My name is " + name + " and my age is " + age);
name = "Your name";
age = 18;
System.out.println("Your name is " + name + " and my age is " + age);
}
}
Note:
1. Variables are always defined starting with a lower-case. Please remember to name your variables beginning with a lower-case character. This is a good practice and needs to be followed.
2. Camel-casing
When we have a very long variable name that consist of more than one word, the rule is to start the first letter of the alphabet with a lowercase, and for the next word and the following words, use the upper case.
for example:
String greetingForTheDay = "Welcome";
int noOfDaysBeforeNewYear = 150;
DataTypes
Here are some of the basic data types in the Java programming language:
1. Boolean
This data-type is used to hold the values “true” or “false”. They are used to denote the result of a condition. And the result can be either of these (true or false)
boolean student = true;
Here student is a boolean variable.
2. Integer
This data-type is used to represent data that are whole numbers.
int age = 25;
Here age denotes an integer variables – integer variables are sufficiently large to store up to 2 billion value.
3. Long
When we have a represent a value more than 2 billion, then an integer may not be suitable.
long timeSinceJanuary1st1970 = 987655487659876587655788;
4. Float
When we represent a decimal value, we use a float
float price = 1900.50
5. Double
When we need to have a higher level of precision, than a float, we have to use a double data-type
double speedOfLightinMetresPerSecond = 390808797987707970.890795439864
6. Char
This data-type is used to represent a single character of data
char symbol = ‘a’;
7. Byte
Represents the ordinary raw data-type
byte b = ‘0x2EF’;
8. String
This data-type is used to represent a sequence of characters
String greeting = “Welcome and have a good day!”;
Control Structures
Now that we have a basic understanding of variables and data-types, let us move over to the control structures offered by the Java programming language.
Control Structures help us in Conditional Evaluation and Execution.
Control Structures , like the variables, form a building block in the foundation of Java development. They allow us to alter the code behavior according to the criteria set in the code.
The IF statement
The if statement offers the ability to execute a block of code if the given condition is TRUE. If the condition is NOT TRUE, then the code block will not execute.
The test condition in the IF statement, may be any expression including test for nonzero values or tests for equality
The syntax for the if statement is as follows:
if (conditional expression) {
block of code;
}
For example,
String role = "admin
. . .
if (role == "admin") {
echo ('Welcome to the admin area');
}
The else statement
The optional else statement provides a default block of code that executes if the condition returned in an IF statement is FALSE.
Like the IF statement block, the block of code for the else statement is also delimited with curly braces.
if (role == "admin") {
echo ('Welcome to the admin area');
} else {
echo ('Welcome to the public area');
}
The elseif statement
The elseif statement is used when we want to test several conditions simultaneously This allow to test some additional conditions until one of the condition is found true, or until we reach the finally else block.
if (role == "admin") {
echo ('Welcome to the admin area');
} elseif (role == "guest") {
echo ('Welcome to the public area');
} else {
echo ("Welcome back, $username");
}
The ternary operator
The ternary operator works like an if statement, but returns a value from one of two expressions. The ternary operator takes three operands.
The format for a ternary operator is as shown below:
{expression} ? value_when_true: value_when_false;
boolean logged_in_user = TRUE;
String role = 'admin';
String greeting = (logged_in_user==true)?"Welcome" + role: "Please login again";
System.out.println(greeting)
...
Since the logged_in_user boolean variable is assigned the value as TRUE, the output here is:
Welcome admin
If the logged_in_user boolean variable had been set to FALSE, then the above code would have printed
Please login again.
The switch statement The switch statement compares an expression to numerous values. It is very common to have an expression, such as a variable for which you will want to execute different code for each value stored in the variable.
Let us say for example, we have a variable called action, and depending on the value for each action like “add”, “edit” or “delete”, we may perform different actions.
One way of implementing this is using multiple if cand elseif statements
if (action == "ADD") {
System.out.println("do add data here");
//-- more code comes here
} elseif ($action == "MODIFY") {
System.out.println("perform data modification here");
//-- more code comes here
} elseif ($action == "DELETE") {
System.out.println("perform data deletion here");
//-- more code comes here
}
The switch statement is a clean and neater way to test for multiple values.
The above example can be re-written using a switch statement as follows:
switch (action) {
case "ADD":
System.out.println("perform data addition here");
//-- more code comes here
break;
case "MODIFY":
System.out.println("perform data modification here");
//-- more code comes here
break;
case "DELETE":
System.out.println("perform data deletion here");
//-- more code comes here
break;
}
The switch statement compares the variable to the values in each of the case statement. If a match is found, the code is executed. The code in subsequent pages is executed till the end of the switch statement, or until a break command is encountered.
The SWITCH statement also provides a way to do something if none of the other cases match. It is generally the last statement in the “case”s of the SWITCH statement.
switch (action) {
case "ADD":
System.out.println("perform data addition here");
//-- more code comes here
break;
case "MODIFY":
System.out.println("perform data modification here");
//-- more code comes here
break;
case "DELETE":
System.out.println("perform data deletion here");
//-- more code comes here
break;
default:
System.out.println("Action has to be either ADD, MODIFY or DELETE");
}
Summary:
- We learned about the Java programming language – how it fulfills the promise of “Write once, Run anywhere”
- We learned about variables and data-types. We saw examples of how to declare variables and use the built-in data-types
- We learned about the control structures – the if block, the ternary operator, and the control statements
- We learned the Java coding conventions – how to define variable names and about camel-casing