Methods in Java

Click to rate this post!
[Total: 1 Average: 4]

Methods in Java:

A Java method is having   statements that are combined together to perform an action. When  invite  the System.out.println() method.

Creating Method:

For creating a method we must have the same method header and method body

Syntax:

public static int methodName(int a, int b){

//body

}

Here Public static is an modifier ,int is a return type, a and b are Parameters

Method Calling:

For calling a method there are two ways that is return a value and no return value.  When a method calling a program invokes a method, and control by called method then returns control to the caller in two conditions, when the return statement is executed, it reaches the method ending closing brace.

Example:

The getArea() method in the Square class that was explained in the below on objects returns an integer:

// a method for computing the area of the square    Public int getArea() {        return width * height;    }

File and Input/ Output Stream:

This class can be used for performing and reading operation from a resource byte by byte

Syntax: DataInputStream dis= new DataInputStream(InputStream);

FileInputStream:

This class can be used for reading the data from a file byte by byte and BufferedInputStream is a altered stream using which we can improve the performance of the application.

Syntax:

FileInputStream fis= new FileInputStream(File);

FileInputStream fis= new FileInputStream(string);

BufferedInputStream bis= new BufferedInputStream(InputStream);

Example:

Import java .io.*;

Public class FileReading{

Public Static Void main (string [ ] ar) throws IO Exception {

FileInputStream fis = new FileInputStream (“input.txt”);

BufferedInputStream bis= new BufferedInputStream (fis);

Int ch ;

While ((ch=bis. read ( ) ) ! =-1) {

System.out.println((char)ch); }

bis.close( );

}

}

FileOutputStream: It is used for writing the content into a file byte by byte

Syntax: FileOutputStream fos= new FileOuputStream(File);

FileOutputStream fos= new FileOuputStream(string);

Exceptions:

An Exception is a Runtime Error which occurs because of logical failure or invalid input or wrong data. The exception class is super most class of all Exceptions.

The Exceptions are classified into two types

  • Compile time Exception
  • Run Time Exception

The runtime errors are further classified into two types based on the whether there are handled or not

  • Checked Exception
  • Unchecked Exception

According to the checked exception these were occurred at the compilation time and the user is handled this checked exception by skipping it. And coming to the unchecked exception are occurs at the time of execution. In a program it includes errors, bugs are ignored at the time exception.

Exception Hierarchy: The exception classes are divided into some categories of the java.lang.Exception class. There is another class called Error which is comes from the Throwable class. In java programs errors are irregular conditions that come in failures, these are not handled.

JAVA Tutorial Video

The Exception class has two main subclasses:

If a class is specified inside anther class then it is called inner class or nested class.The inner classes are the helper classes to the outer classes based on the location of the inner classes the keyword is specified.

  • Regular inner class
  • Method inner class
  • Anonymous inner class
  • Static inner class

If a class is declared inside another class without any keyword then it is known as regular class. The regular inner class can be accessed directly inside the instance method of outer

c lass because by the time control comes into the instance method of outer class an object of outer class is already created. The regular inner classes will exist only if an object of outer class exist.

If a inner classes exists inside a method then it is called as method local inner class. If a method local inner class can be used only it that method where it is declared and it should be used after declaration. If a inner class does not have any name then it is called anonymous inner class, if a class is declared inside another class with static keyword then it is in inner class, it exists without outer class object.

Example:

Class Outer {

Static Class Inner {

Public class void main (String [ ] ar) {

System.out.println(“inner class main method”);

} }

Public static void main (string [ ] ar) {

System .out.println(“Outer class main method”);

} }

Example:

Class Outer

{

Class Inner

Void inner Method( ) {

System.out.println(“inner method”);

}

}

Void Outer Method( ){

Inner i=new Inner( );

  1. inner Method( );

System.out.println(“Outer Method”);

}

Public static void main (string[ ] ar ){

System.out.println(“outer class main method”);

Outer O= new Outer( );

O.Outer Method( );

}

}

Multi Threading:

Thread is a piece of code that executed separately if a program contain multiple threads then it is called as multithreading and they language which supports multi threading will be called as multi threaded language.

Every java program will by default contain one thread called as main thread which is responsible for executing the java program.

Life Cycle of Thread Class:

Life Cycle of Thread

 

Procedure to create a thread by extending thread class:

  • Declare a class which extends from thread class
  • Specifying the functionality of user defined in run ( )
  • Create an object of a class which extends from thread class
  • Attach the above created object to the main thread
  • Execute the functionality of user defined thread available in run( ) by using start( )

Procedure to create a user defined thread by implementing Runnable interface:

  • Declare a class which implements runnable interface
  • Specifying the functionality of user defined thread in run( )
  • Create an object of that class which implements runnable interface
  • Attach the above created object to the main thread
  • Execute the functionality of user defined thread available in run( ) by using start ( )

Methods of Thread class:

It will provide the information of currently executing thread. This method can be used to suspend the execution of the thread for the specified amount of milli seconds. The Sleep( ) method will through interrupted exception which must be handled because it is checked exception.

Methods of Thread Class

Collection:

It is an object which can store group of other objects every object requires 10 bytes of memory and to store 4 objects .we require 40 bytes of memory .Hence 4 objects are stored one time inside the collection object and one time outside the collection object and wasting the memory by duplication.

  • To overcome of the array concept it drawbacks the sun Micro systems has released collections in java 2 version.
  • In order to avoid the wastage of memory the JVM will store the references of the objects instead of storing the objects directly.

 Collection Interface:

The abstract data which represents collection Interfaces to allows independently in details of their illustration in object-oriented languages.

The methods perform the action of algorithm by through searching and sorting in the process ,the algorithm should be in many forms and same methods.

Collection

  • This collection is a combinations multiple objects ,it stores instance of an object to handle the set.
  • Collection object is a collection the references of other objects and class whose object can store group of references of other objects.

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


100% Secure Payments. All major credit & debit cards accepted.

Call Now Button