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

Learn Java programming from scratch at Udemy.com

How to successfully compile and run Java code

How to begin using NetBeans IDE for coding in Java

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:

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

/** 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”.

Top courses in Java

Java 8 New Features In Simple Way
DURGASOFT DURGA
4.6 (5,178)
Java Programming Masterclass for Software Developers
Tim Buchalka, Tim Buchalka's Learn Programming Academy, Goran Lochert
4.6 (119,625)
Bestseller
The Complete Android & Java Developer Course - Build 21 Apps
Paulo Dichone | Android, Java, Flutter Developer and Teacher, Fahd Sheraz
4.2 (5,949)
Java Programming for Complete Beginners
in28Minutes Official
4.4 (10,028)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo