Basics of Java: Learn Java Coding

java coding tutorialCoding in Java can be a headache, especially if you are a beginner. Java is an object-oriented high-level programming language. It’s a powerful language for developing robust desktop applications and web applications and presently, runs on many smart devices. It’s a highly portable, platform-independent code. Java code runs on Windows, Linux or Mac OS without worries of compatibility.

Before you begin coding

  • Install Java Virtual Machine (JVM), otherwise referred to as Java Runtime Environment (JRE).  If you don’t have it, you can download it from the web and install it.
  • Next, install the Java Software development kit (JDK). In case you don’t have it on your machine, download and install it.
  • Install JDK on your computer that is applicable to your machine, which is a 32-bit or a 64-bit OS.

Learn Java programming from scratch at Udemy.com

How to successfully compile and run Java code

  • You write code in a text editor using editors such as NETBeans or Eclipse. This software is an Integrated Development Environment (IDE). The code is referred to as the source code and is saved with a ‘.java’ file extension.
  • The Java compiler (Javac) compiles source code, resulting into a class file. This class file has an extension ‘. class’. This class file runs on a Java Virtual Machine (installed in the previous instructions).

How to begin using NetBeans IDE for coding in Java

  • Assuming you have downloaded and installed your Netbeans, you get the screen displayed on your first run.
  • Click on the File menu to start a new project, and then click on New Project. This will display a dialogue box.
  • In the categories, choose Java. This will display various options under Projects and you can select Java Application. Click on “Next” to display the dialogue.
  • In the label project name, you can type in the name of your  project. Select the “Create Main Class” and name the class with .Main extension.

Want to learn more about Java? Take a tutorial at Udemy.com

Your First Code

package mycode;

import java.io.*;

class HelloWorld

{

public static void main(String args[])//Main function

{

System.out.println("Hello World");

}

}

Output:

Hello World

The above code is saved in “.java” file name extension with the same name as that of the class. When the program is compiled and executed “Hello World ” gets printed on your screen as output.

Learn everything about Java programming through a tutorial at Udemy.com

Important Aspects of Java Coding:

  • Packages and class: Since java is an object oriented programming language, all the coding is done under classes and packages, which provide data abstraction and unauthorized use of the classes and its components. Packages are included using the “import” statement in the program. Java provides many built-in packages.
  • Data types: Java supports many data types to store values of different kinds including a  character, integer, float, double, long, short, byte and boolean.
  • String: The String type in Java is not a data type but a class. It’s used to store a sequence of characters. The String class helps a programmer handle long sentences and series of characters in a better way with built-in String methods provided by Java libraries.
  • Conditional Statements:  Java has three conditional statements that help you make decisions using the “if” statement, “switch” statement and ternary operator.
  • Methods: Java has millions of predefined classes and methods to perform basic tasks. The programmer invokes these methods to improve the program complexity and ease of work. For instance, “System.out.println()” method prints the string on the screen.
  • Specifier:  Java provides programmers with the ability to make the classes and methods accessible by providing keywords like private, protected and public. These specifiers are used for classes to make them accessible or prevent them from being accessed by other packages. Similarly, methods also use these specifiers to be accessible within a class of the same package or different class of other packages.For Example:
public int MyFunction()

{
//Method Body
}
protected int MyFunction()
{
//Method Body

}
private int MyFunction()
{
//Method Body
}
  • Code comments: Comments are written between ‘/*’ at the beginning of the comment and ‘*/’ at the end. Alternatively, if you want to make a single line comment, then, you can use ‘//’. Comments helps programmers understand the code during debugging and testing. For example:

/** This is my Java comment*/

Examples Coding

import java.io.*;// Including the package (Always the First line)
class Rec// Declaring a Class
{
public static void main(String[] args) // Main method Declaration.
{
int a=0,b=0; // Variable declaration
try{
Scanner scant= new Scanner(System.in)//Creating Objects of class Scanner
System.out.println("Enter length of Rectangle : "); // Console Message on Screen
a=scant.nextInt(); // Taking integer Input from User
System.out.println("Enter width of Rectangle : ");
a=scant.nextInt();
int area1 = a*b; // Calculating Area and storing in variable.
System.out.println("Area is: "+area1);
intperimiter1 = 2*(a+b);
System.out.println("Rectangle’s Perimeter is: " + perimeter1);
}
catch(Exception e)
{System.out.println("Occurred Error : "+e); //  Exceptions thrown
}
}
}

Output

Enter length of Rectangle :5

Enter width of Rectangle :10

Area of Rectangle : 50

Rectangle’s Perimeter is: 30

The “io” package is included in the first line. The import statement includes this package that you can use with the rest of your code. The main method is the entry point of code execution. All the objects and class variables are declared here. We can have any number of functions in a class and all function calls are made from the main method.

Java Provides Ways to Create, Read and Write to a File.

Example of File Handling

import java.io.*;
class FileRW {
public static void main(String args[]) throws Exception // Handling Exceptions
{
FileReader abc = null; // Creating FileReader class object to read file
FileWriter def = null; // Creating FileWriter class object to write in file
try {
abc = new FileReader("read.txt");
def = new FileWriter("write.txt");
int f;
while ((f = abc.read()) != -1) // Reading File using read method of FileReader class
{
def.write(f);// Writing on different file Using write method of FileWriter class
}
}
finally {
if (abc != null) {
abc.close();
}
if (def != null) {
def.close(); // Closing the opened Files.
}
}
}
}

The above program reads one file’s content and copies the content in another file named “write.txt”.