Java Made Easy

The 21st century is characterized by a great many number of technological changes that completely redefined our previous experiences. From social media to interacting with friends and family to A.I. that perform complex tasks with high accuracy and efficiency, we find ourselves in an ever-changing world.

The tools used to implement these drastic changes are programming languages. Languages serve as a bridge between two entities. Programming languages allow for communication between people and machines. These languages change and different languages are used for different purposes. Java is one such language which has not only stood the test of time but also maintained its relevancy, a rarity among the countless languages we find today.

Java has several uses in the industry. It’s most prominent use is in the field of android app development. However before we get there we need to learn the basics of this language.

BASICS

We will be going over some simple syntax of this language and explaining them. First let us begin with a very simple program.

In Java, a class acts as a template in which we define data members which store the data and methods which manipulate data members. In this simple program there are no data members. ‘public static void main(String args[])’ acts as the main method. In every program in Java, there must be a main method. ‘System.out.println’ is the statement used to print whatever you wish. After printing the cursor moves to next line. The next statement to be printed would be printed on the next line. ‘System.out.print’ is used if after printing we want the cursor to move to the next space on the same line. The next statement to be printed would be printed on the same line,next to previous statement.

READING INPUTS FROM USER:

In the next concept we learn how to read input from user.

‘import’ is a keyword used to bring a library into view for the program to use some of its functionalities. In this case use import the library util- which contains utilities to provide additional functionalities. ‘.*’ is used to bring the entire library into view. If we only want one class from the library util, we can use ‘import java.util.class_name;’. Java takes input from the user with the help of a scanner class. We need to declare a scanner variable which will read the inputs from the user. In this case it is called ‘sn’. It may be anything. To store the input of the user, we need correct pairs of variables and corresponding functions. For example, in this case a string variable ‘name’ needs to store the value of input, so we use ‘scanner_variable.nextLine()’. If it was an integer variable, we need to use ‘scanner_variable.nextInt()’. If data type is double, use ‘scanner_variable.nextDouble()’. For float data type, we use ‘scanner_variable.nextFloat()’. The syntax for characters is a little different. We instead use ‘scanner_variable.next().charAt(0)’.

We can use one scanner variable for multiple inputs. However if you have used nextInt(), don’t use nextLine() with the same scanner variable as this causes an error in the Scanner class. You can use different scanner variables for integers and strings. For example, sn1 for integers and sn2 for strings.

CONDITIONALS:

Every programming language has conditional statements which work on a true-false condition. The major conditional statements are if, else, else if and switch case.

If the statement is true then the ‘if’ condition executes whatever is under it. If the condition is not true, it moves onto the next statement. ‘else if’ condition acts as a secondary ‘if’ that is checked only if previous conditions are false. ‘else’ statement executes if none of the other conditions are satisfied. ‘if’ followed by multiple ‘else if’ statements forms an if-else-if ladder.

In the above example, we make use of biconditional operators, that is operators that check 2 conditions. ‘&&’ is known as AND operator. It returns true only if both conditions are true. ‘||’ is known as OR operator. It returns true if either of the two conditions are true.

There are several other operators like ‘>’, ‘<’, ‘>=’, ‘<=’ and ‘!=’ which are greater than, lesser than, greater than or equal to, lesser than or equal to and not equal to respectively.

Next we have the switch-case statement. Switch-case is similar to an if-else-if ladder.

The ‘switch’ keyword takes in an argument and the following cases check that argument. In the above program, we check whether a number is odd or even using switch-case. After every case, there must be a ‘break’ statement. Otherwise, if a case satisfies the argument’s value, but doesn't have a ‘break’ statement, the control of the program executes next case, and keeps going down the cases till it encounters a ‘break’ statement. This is called fall-through. In the above program we make use of fall-through so that case 1,3 and 5 have same output and case 0,2 and 4 have same output. We may also use a ‘default’ statement which executes if none of the conditions are satisfied. However, this is optional.

ITERATIVE STATEMENTS:

Iterative statements or loop statements are crucial in all programming languages. There are three iterative statements in Java. These are ‘for’, ‘while’ and ‘do while’ statements.

‘for’ loops are used when we know the number of times we want to loop. In the above example 5 times. The syntax of a ‘for’ loop is ‘for(initial_condition; condition_to_be_checked_before_each_instance_of_the_loop; increment_or_decrement)’. You may create an infinite loop by using this statement ‘for(;;)’.

‘while’ loops are used when we don’t know how many times we need to repeat the loop. The loop runs while the condition in the brackets is satisfied.

‘do while’ loops are used when we want the loop to run at least once. The ‘do while’ loop also has a semicolon after the while statement, which is not present in the other loops.

CONCLUSION

With these basic concepts at hand, you now have the ability to code complex programs, using these tools as you please, to serve different functions in your program. Hopefully this short blog has piqued your interest in this language. Stay tuned for more complex concepts of Java.

Happy Learning!

--

--