Udemy logo

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");


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
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 : ");
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


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) {
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”.

Page Last Updated: May 2014

Top courses in Java

Java for Beginners
Navin Reddy
4.5 (1,524)
Learn Selenium with Java, Cucumber & Frameworks
Pavan Kumar
4.6 (7,764)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.7 (4,033)
Oracle Java Certification - Pass the Associate 1Z0-808 Exam.
Tim Buchalka's Learn Programming Academy, Goran Lochert
4.5 (5,832)
Modern Java - Learn Java 8 Features By coding it
Pragmatic Code School
4.5 (11,129)

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 Business.

Request a demo