Java Programming Basics to Brilliance Part 2

This post is a progression to previous post here.



Java Syntax
Every language in this world has a syntax so does a programming language like Java. Now take a look at the following programming which simply prints the all famous "hello world."

public class play {
public static void main(String[] args){
System.out.println("hello world.");
}
}

In this program what we do is create a class by the name of play. In this class we create a function or method by the name of main in which we print out a line "hello world.".
Any program in java will always have a class and a main method. When a java program is executed, the main method is called and all of the statements in this method are executed in progression.
Writing the above lines of code is a piece of cake in Eclipse. To further define what is happening in above program in details, lets break it.

public class play {
States the beginning of the class. We stated class as public, which means it's members and methods will be accessible publicly. Other than public we have private, protected and default when you don't define anything. In details below.

public static void main(String[] args){
main method is always public because JVM has to make a call to this method. 
static methods carry a functionality that shall remain same throughout the program.
void just states that the method need not return anything.
String[] args defines an array of name args which will be holding everything we input to this program in this array. We need not necessarily use this array. 

Access modifiers in slight details.
A private class can have public methods. Why?
Because when we make a class private it is usually to protect it's instance variables from outside sources and public methods are the way to initialize these private instance variables.
If this confuses you, lets learn it with an extensive example.

public class MyDog {

private String tail;

public String waggingTail(String activity){
tail = activity;
return tail;

}

}


public class MyDogActivity {

public static void main(String[] args) {
MyDog tommy = new MyDog();

String tommysTail = tommy.waggingTail("Look at it move!!!");

System.out.println(tommysTail);
}

}

Write the above code line by and use Ctrl+space for suggestions in Eclipse. The two classes MyDog and MyDogActivity are meant to be in different class file. Run the program by using keyboard shortcut Ctrl+F11 for faster access.






What happens in the above program is, we have a class called MyDog with a private variable of String type tail and a public method waggingTail which sets its value to activity.
Furthermore we have a class MyDogActivity which creates an object of MyDog by the name of tommy. Now we make a String type variable tommysTail which has to perform a its task defined in MyDog class. If this confuses you the line below might help.

MyDog is a class which defines the structure of any dog out in the world. Like any dog can wag his tail we made a function waggingTail also no dog likes their tail to be touched so we made the tail private.

Now we have a dog by the name of tommy. Like any other dog tommy has a tail called tommysTail and can perform whatever function any dog can perform like waggingTail.


I hope this clears the picture about MyDog and tommy being one of the Dog. If you still find it confusing read it over and make your own classes and objects to clear it up and feed it in your head. This is by far the most important aspect in Java and everything we do in Java consists of classes and objects one way or the other.

Moving on, lets make a simple program of adding two digits we take as input from the user.

public class Addition {

public static void main(String[] args) {

Scanner inputReceiver = new Scanner(System.in);

System.out.println("Enter first number: ");

int firstNumber = inputReceiver.nextInt();

System.out.println("Enter second number: ");

int secondNumber = inputReceiver.nextInt();

int sum = firstNumber + secondNumber;

System.out.println("The sum is: "+sum);

}

}


In the above program we make an object called inputReceiver of the class Scanner. Now what is Scanner?



Scanner like our
MyDog class in previous example is a class which has certain attributes and functions.
Like our
MyDog had a tail attribute and a function of wagging the tail, Scanner is a class predefined in Java which carries a function to take the input and forward it as required.If you want to see this class just press hold Ctrl and click on Scanner in Eclipse.
In the example above, we make an object of class Scanner by the name of inputReceiver using which we take the input
firstNumber and secondNumber. Rest of the code should be self explanatory as its simple addition and printing. This is pretty much how everything is done in Java, just thing become more complicated depending upon the complexity of a program.
Like most of the object oriented programming languages out there, Java also has methods for looping and condition checking like


for loop,
if else,
while,
do while,
switch.

There are various ways of storing data in Java at run time like
Variables which have types of int, String, char, byte, boolean,
then arrays of same types. Which are still simple and easy to understand and deal with.
Things get complicated in Java with the introduction of Collections, reason being collection consists of objects rather than simple strings and integer values and it's one of the feature that makes Java powerful.

Next up will be Collection, conditional operators and loops.

Go to next. http://www.shiftescape.com/2017/06/java-programming-basics-to-brilliance.html

Comments