Skip to main content

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

Popular posts from this blog

Unity Mobile Game Optimization Checklist

- On Image and Text components that aren’t interacted with you can uncheck “Raycast Target” on it, as it will remove them from any Raycast calculus. - Click on your textures in your “Project” window. Click on the “Advanced” arrow, and now check “Generate Mip Maps”, Unity especially recommends it for faster texture loading time and a lower rendering time. - Set the “Shadow Cascades” to “No Cascades” (Quality settings) - If you have dynamic UI elements like a Scroll Rect with a lot of elements to visualize, a good practice is to turn off the pixel perfect check box on the canvas that contains the list and disable the items that aren’t visible on the screen. - Set all non moving objects to "Static" - Above Unity3d 2017.2 you should turn off "autoSyncTransforms" on the Physics tab - Always use Crunch Compression Low on textures - Try to keep the “Collision Detection Mode” on “Discrete” if possible, as “Dynamic” demands more performance. - You can go to the TimeManager w...

How to make download file button in react js

To create a download file button in React.js, follow these steps: Import the necessary dependencies: javascript Copy code import React from 'react' ; import { saveAs } from 'file-saver' ; Create a function that handles the download action: javascript Copy code const handleDownload = ( ) => { // Create a new Blob object with the desired content const fileContent = 'This is the content of the file you want to download.' ; const blob = new Blob ([fileContent], { type : 'text/plain;charset=utf-8' }); // Use the saveAs function from the file-saver library to initiate the download saveAs (blob, 'download.txt' ); }; Create a button component and attach the handleDownload function to the button's onClick event: javascript Copy code const DownloadButton = ( ) => { return ( < button onClick = {handleDownload} > Download File </ button > ); }; Use the DownloadButton component wherever ...

How to Convert Unreal Engine 5.1 Blueprint Project to C++ Project

To begin, open your project in the Unreal Engine 5 or a newer version of the editor.  Next, access the New C++ Class Dialog by selecting Tools and then New C++ Class .  From there, create a new "None" class and press "Create Class."  You might receive a warning message regarding the game module compilation, but you can disregard it and choose "No."  After closing any pop-ups, warnings, or success notifications, exit the Editor.  Proceed to your project's folder, right-click on the .uproject file, and select "Generate Visual Studio project files."  Double-click your project's .sln file to open it in Visual Studio.  In Visual Studio's Solution Explorer, find and choose your project.  Select "Development Editor" in the build configuration drop-down menu.  Right-click on your project and select "Build" to compile it without any errors.  Afterward, set the build configuration drop-down menu to "Development" ...