Posts

Parameterized constructor Example

class Rectangle  {   int length;  int breadth; Rectangle(int len,int bre)   {   length  = len;  breadth = bre;   } public static void main(String args[])  {          Rectangle r1 = new Rectangle(20,10);   System.out.println("Length of Rectangle : " + r1.length);   System.out.println("Breadth of Rectangle : " + r1.breadth);   } }

Default constructor Example Program

class NoteBook {     /*This is default constructor. A constructor does      * not have a return type and it's name      * should exactly match with class name     */     NoteBook() {             System.out.println("Default constructor");    }  public void mymethod()     {                  System.out.println("Hello");     }     public static void main(String args[]) { /* new keyword creates the object of the class           * and invokes constructor to initialize object          */ NoteBook obj = new NoteBook(); obj.mymethod();     } }

constructors in Java Notes

constructors : In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default. There are two types of constructors in Java: no-arg constructor, and parameterized constructor. Rules for creating Java constructor There are two rules defined for the constructor. Constructor name must be the same as its class name A Constructor must have no explicit return type A Java constructor cannot be abstract, static, final, and synchronized. Types of Java constructors There are two types of constructors in Java: ...

Scanner Example Programs

Java Scanner Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them. The Java Scanner class breaks the input into tokens using a delimiter which is white space by default. It provides many methods to read and parse various primitive values. The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc. The Java Scanner class extends Object class and implements Iterator and Closeable interfaces. The Java Scanner class provides nextXXX() methods to return the type of value such as nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt...

BufferedReader Example Program

import java.io.*; class BufferedReaderDemo { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter 1st number"); int x=Integer.parseInt(br.readLine()); System.out.println("enter 2nd number"); int y=Integer.parseInt(br.readLine()); System.out.println("sum is :"+(x+y)); } }

DoWhileExample Program

public class DoWhileExample  {  public static void main(String[] args)  {      int i=1;      do       {          System.out.println(i);                 i++;      }         while(i<=10);  }  }

WhileExample Program

public class WhileExample  {  public static void main(String[] args)  {      int i=1;      while(i<=10) {          System.out.println(i);                  i++;      }  }  }