Java Tutorial: Introduction for beginners – Part 2 (Basic)

Java tutorial basic

Java Tutorial Basic – Part 2

What is Java?

Java is a general-purpose, open-source and free programming language. It originally developed by Sun Microsystems in 1995 and more than 3 billion devices run Java. It is used to creating Desktop applications, Mobile applications, Web applications, Web servers, Games, and more. Java works on a variety of platforms. such as Windows, Mac, Linux and more. Java is easy to learn and simple to use. In this tutorial. You will learn some basic Java programming concepts.

Read more >>

Java Tutorial – Part 2

Exception handling

Types of Java Exceptions

  1. Checked Exception (ex: SQLException, IOException)
  2. Unchecked Exception (ex: NullPointerException, ArithmeticException )
  3. Error (ex: VirtualMachineError)
try {
  // code
}
catch(Exception e) {
  // code to handle errors
}

Without Try…Catch

Create Java file, called TryCatch.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class TryCatch {
  public static void main (String [] args){
    int no = 100/0; // ArithmeticException: / by zero.
    System.out.println("Hello World!");
  }
}

Open Command Prompt and navigate to the directory where you saved “ TryCatch.java” then enter the following command and press enter to compile your code.

javac TryCatch.java

Then enter following command and press enter to run your code.

java TryCatch
TryCatch example 1

with Try…Catch

Create Java file, called TryCatchWith.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class TryCatchWith {
  public static void main (String [] args){
    try {
      int no = 100/0; // ArithmeticException: / by zero.
    } catch(Exception e) {
      System.out.println(e);
    }
    System.out.println("Hello World!");
  }
}

Open Command Prompt and navigate to the directory where you saved “TryCatchWith.java” then enter the following command and press enter to compile your code.

javac TryCatchWith.java

Then enter following command and press enter to run your code.

java TryCatchWith
Java tutorial basic

with Try…Catch and Finally

Create Java file, called TryCatchFi.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class TryCatchFi {
  public static void main (String [] args){
    try {
      int no = 100/0; // ArithmeticException: / by zero.
    } catch(Exception e) {
      System.out.println(e);
    } finally {
      System.out.println("try...block: ended");
    }
     System.out.println("Hello World!");
  }
}

Open Command Prompt and navigate to the directory where you saved “ TryCatchFi.java” then enter the following command and press enter to compile your code.

javac TryCatchFi.java

Then enter following command and press enter to run your code.

java TryCatchFi

with Multi-catch block

Create Java file, called TryMultiCatch.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class TryMultiCatch {
  public static void main (String [] args){
    try {

      int no[]= new int[3];
      int no5 = no[5]; // ArrayIndexOutOfBounds

    } 
    catch(ArithmeticException e) { System.out.println("ArithmeticException");} 
    catch(ArrayIndexOutOfBoundsException e) {System.out.println("ArrayIndexOutOfBoundsException ");}
    finally {System.out.println("try...block: ended");}

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

Open Command Prompt and navigate to the directory where you saved “TryMultiCatch.java” then enter the following command and press enter to compile your code.

javac TryMultiCatch.java

Then enter following command and press enter to run your code.

java TryMultiCatch
Java tutorial basic

Methods

method is a code which only runs when it is called.

Create a Method

public class CreateMethod {
  modifier returnType methodName (parameter1, parameter2, ... , parameterN) {
   // code
  }
}

public class CreateMethod {
  static void methodName () {
   // code
  }
}

// public static − modifier
// int, void − returnType
// a, b, int a, int b − parameters

Call a Method

Create Java file, called CreateMethod.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class CreateMethod {
  static void helloWorld() {
    System.out.println("Hello World!");
  }

  public static void main (String[] args){
   helloWorld();
   helloWorld();
   helloWorld();
   helloWorld();
   helloWorld();
  }
}

Open Command Prompt and navigate to the directory where you saved “CreateMethod.java” then enter the following command and press enter to compile your code.

javac CreateMethod.java

If there are no errors, enter following command and press enter to run your code.

java CreateMethod 

Method Parameters

Create Java file, called MethodPar.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class MethodPar {
  static int methodAdd(int no1, int no2) {
    int result = no1 + no2;
    return result;
  }

  public static void main (String[] args){
   int a = methodAdd(100,110);
   int b = methodAdd(542, 475);
   int c = methodAdd(258,154);

   System.out.println("a: "+a);
   System.out.println("b: "+b);
   System.out.println("c: "+c);
  }
}

Open Command Prompt and navigate to the directory where you saved “MethodPar.java” then enter the following command and press enter to compile your code.

javac MethodPar.java

If there are no errors, enter following command and press enter to run your code.

java MethodPar

Classes 

A class should always begin with an uppercase letter. (ex: NewClass) The name of the java file must match the class name.

Create a Class/Object

Create Java file, called NewClass.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class NewClass{
  String name = "George";
  int age = 25;

  public static void main(String[] args) {
    NewClass newObj1 = new NewClass();
    NewClass newObj2 = new NewClass();

    System.out.println(newObj1.name);
    System.out.println(newObj2.age);
  }
}

Open Command Prompt and navigate to the directory where you saved “ NewClass.java” then enter the following command and press enter to compile your code.

javac NewClass.java

If there are no errors, enter following command and press enter to run your code.

java NewClass

Create a Multiple Classes

Create Java file, called PersonClass.java, you can use any java editor software for this. then enter the following code and save it.

public class PersonClass {
  String name = "George";
  int age = 25;
}

Create Java file, called MainClass.java, you can use any java editor software for this. then enter the following code and save it.

public class MainClass {
  public static void main(String[] args) {
    PersonClass newObj1 = new PersonClass();

    System.out.println(newObj1.name);
    System.out.println(newObj1.age);
  }
}

Now open Command Prompt and navigate to the directory where you saved “PersonClass.java” and “MainClass.java” files. then enter the following command and press enter to compile your codes.

javac PersonClass.java
and
javac MainClass.java

If there are no errors, enter following command and press enter to run your code.

java MainClass

Multiple Classes and Method

Create Java file, called ClassMethod.java, you can use any java editor software for this. then enter the following code and save it.

public class ClassMethod {
  public void newMethod() {
    System.out.println("Hello");
  }
}

Create Java file, called  MainClassMethod.java, you can use any java editor software for this. then enter the following code and save it.

public class MainClassMethod {
  public static void main(String[] args) {
    ClassMethod newObj1 = new ClassMethod();

    newObj1.newMethod();
  }
}

Now open Command Prompt and navigate to the directory where you saved “ClassMethod.java” and “MainClassMethod.java” files. then enter the following command and press enter to compile your codes.

javac ClassMethod.java
and
javac MainClassMethod.java

If there are no errors, enter following command and press enter to run your code.

java MainClassMethod
Java tutorial basic

Java Tutorial Basic: Data Input/Output

Data Output

public class DataOutput {
   public static void main(String args[]) {  
     System.out.println("Output");
   }
}

Data Input

//import
import java.util.Scanner;

Create Java file, called DataInput.java, you can use any java editor software for this. then enter the following code and save it.

import java.util.Scanner;

public class DataOutput {
   public static void main(String args[]) {  
     Scanner input = new Scanner(System.in);
      
     //String
     System.out.println("Enter your name: ");
     String name = input.next();
     System.out.println("Name: "+name);

     //int
     System.out.println("Enter your age: ");
     int age = input.nextInt();
     System.out.println("Age: "+age);

     //float
     System.out.println("Enter your height: ");
     float height = input.nextFloat();
     System.out.println("Height: "+height);
   }
}

Now open Command Prompt and navigate to the directory where you saved “DataInput.java” file. then enter the following command and press enter to compile your codes.

javac DataInput.java

If there are no errors, enter following command and press enter to run your code.

java DataInput

Java Tutorial Basic: Files and I/O

//import
import java.io.*;

Read File

FileInputStream name = new FileInputStream(fileLocation");   

Create Java file, called ReadFile.java, you can use any java editor software for this. then enter the following code and save it.

import java.io.*; 

public class ReadFile {  
  public static void main(String args[]){    
    try{    
      FileInputStream file = new FileInputStream("input.txt");    
      int c = 0;    
      while((c = file.read()) != -1){    
        System.out.print((char)c);    
      }    
      file.close();    
    }catch(Exception e){System.out.println(e);}    
  }    
}  

Now open Command Prompt and navigate to the directory where you saved “ReadFile.java” file. then enter the following command and press enter to compile your codes.

javac ReadFile.java

If there are no errors, enter following command and press enter to run your code.

java ReadFile

Write File

BufferedOutputStream name = new BufferedOutputStream(new FileOutputStream("fileLocation"));
	

Create Java file, called WriteFile.java, you can use any java editor software for this. then enter the following code and save it.

import java.io.*;
public class WriteFile {
   public static void main(String args[]) { 
      try {
        FileOutputStream file = new FileOutputStream("output.txt");
	BufferedOutputStream bufout = new BufferedOutputStream(file);    
	
	String text = "Hello, How are you?";   
 
     	byte b[] = text.getBytes();    
        bufout.write(b);    
        bufout.flush();    
        bufout.close();    
        file.close();    
      }catch(Exception e){System.out.println(e);}   
   }
}

Now open Command Prompt and navigate to the directory where you saved “WriteFile.java” file. then enter the following command and press enter to compile your codes.

javac WriteFile.java

If there are no errors, enter following command and press enter to run your code.

java WriteFile

Sources : 
https://pixabay.com/ (images) 

Hello Friends,

Hope this post (Java Tutorial Basic: Introduction for beginners (Part 2)) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in programming and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page: https://www.facebook.com/ctechf

Best Free eBooks to Learn Python Programming

Free eBooks to Learn Python, Here are 13 of the best Python eBooks for python lovers.
Download Now

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python
Python Tutorial: Django Tutorial for Beginners

How to be a Programmer

Leave a Reply

22 Shares
Share
Tweet
Share
Pin
Share