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 Development

Build Websites from Scratch with HTML & CSS
Brad Hussey, Code College
4.5 (13,867)
Complete C# Unity Game Developer 2D
GameDev.tv Team, Rick Davidson, Gary Pettie
4.7 (101,009)
Learn and Understand AngularJS
Anthony Alicea
4.6 (21,653)
Running a Web Development Business: The Complete Guide
Evan Kimbrell
4.6 (1,663)
Website Localization For Translators
Dorota Pawlak
4.7 (827)
Introduction to JavaScript Object Notation (JSON)
Marie Taylor
4.4 (864)
Javascript for Beginners
Framework Tech
4.4 (2,391)
Become a Certified Web Developer: HTML, CSS and JavaScript
Framework Tech, Mark Lassoff
4.6 (3,459)

More Development Courses

Popular topics

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