Tuesday, September 11, 2018

Constructors in general

Note that a conventional distinction is made in Java between the "default constructor" and a "no-argument constructor":
  • the default constructor is the constructor provided by the system in the absence of any constructor provided by the programmer. Once a programmer supplies any constructor whatsoever, the default constructor is no longer supplied.
  • no-argument constructor, on the other hand, is a constructor provided by the programmer which takes no arguments.
This distinction is necessary because the behavior of the two kinds of constructor are unrelated: a default constructor has a fixed behavior defined by Java, while the behavior of a no-argument constructor is defined by the application programmer.
Example
Resto is an immutable class, since its state cannot change after construction. Note that:
  • all validation is performed in its constructor
  • almost all of its javadoc is concerned with stating precisely what is to be passed to the constructor


import java.math.BigDecimal;
import java.util.Objects;

/** Model Object for a Restaurant. */
public final class Resto {

  /**
   Full constructor.
    
   @param id underlying database internal identifier (optional) 1..50 characters
   @param name of the restaurant (required), 2..50 characters
   @param location street address of the restaurant (optional), 2..50 characters
   @param average price of a meal (optional) $0.00..$100.00
   @param comment on the restaurant in general (optional) 2..50 characters
  */
  public Resto(
    String id, String name, String location, BigDecimal price, String comment
  ) throws ModelCtorException {
    this.id = id;
    this.name = Objects.requireNonNull(name);
    this.location = location;
    this.price = price;
    this.comment = comment;
    validateState();
  }
  
  public String getId() { return id; }
  public String getName() {  return name; }
  public String getLocation() {  return location;  }
  public BigDecimal getPrice() { return price; }
  public String getComment() {  return comment; }

  /** For debugging only. */
  @Override public String toString(){
    return id + ":" + name;
  }
  
  @Override public boolean equals(Object aThat) {
    if (this == aThat) return true;
    if (!(aThat instanceof Resto)) return false;
    Resto that = (Resto)aThat;
    for(int i = 0; i < this.getSigFields().length; ++i){
      if (!Objects.equals(this.getSigFields()[i], that.getSigFields()[i])){
        return false;
      }
    }
    return true;
  }  
  
  @Override public int hashCode(){
    return Objects.hash(getSigFields());
  }
  
  // PRIVATE 
  private final String id;
  private final String name;
  private final String location;
  private final BigDecimal price;
  private final String comment;
  
  /** Does all of the checks specified in the constructor's javadoc.*/
  private void validateState() throws ModelCtorException {
    //...elided
  }
  
  private Object[] getSigFields(){
    return new Object[] {name, location, price, comment};
  }
} 

Monday, January 11, 2016

Method Overloading or Method Overriding

OverridingDemo.java
class Parent
{
   public void show(int... i)
   {
    System.out.println("parent");
   }
}


class Child extends Parent
{
    public void show(int[] i)
    {
      System.out.println("child");
    }
}

public class OverridingDemo
{
    public static void main(String[] args)
     {
       Child c = new Child();
       int arr[]=new int[]{2,3};
       c.show(arr);//child
       c.show(2,3,4);//error
     }
}
Answer:-       Its method Overriding.
int[] i and int …i  both internally represents an Array.

The only difference is
while calling show(int[] i) you have to pass array/null.You cannot pass no of arguments to it
But while calling show(int … i),You can pass array or any number of arguments,or null

As the method is overrided,any call to c.show(-),will always invoke show(int[] i),and if you pass integer array,it works fine.
Else compile time error

Caution:-
In JDK build 1.7.0_03-b05,Its output is as explained above.
In JDK build 1.6.0_32-b05,There is no error which calling c.show(2,3,4),and it gives child as ouput
In JDK build 1.5.0_10-b03,show(int …) & show(int a[]) are treated as same,so it wont allow overriding,because both the show() are treated as same,and you cannot have 2 method with same signature

Reverse Number Java Example

import java.util.Scanner;
class ReverseNumber
{
public static void main(String[] args) 
{
int no,rev=0,r,a;
Scanner s=new Scanner(System.in);
System.out.println("Enter any no.: ");
no=s.nextInt();
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
System.out.println("Reverse: "+rev);
}
}
Enter any no. :
153
Reverse: 351

Thursday, January 7, 2016

Best 5 Books For Learning Java

1. SCJP Sun Certified Programmer for Java 6

SCJP Sun Certified Programmer for Java 6
Author : 
Bert Bates / Kathy Sierra

Description :
This book is perfect if you are preparing for Java Certification Exam or preparing for interviews or wants to master in Core Java.

2 .The Java Language Specification, Third Edition

The Java Language Specification, Third Edition
Author :
James Gosling, Bill Joy, Guy Steele, Gilad Bracha

Description :
Written by the inventors of the Java Language Specification. This book provides complete and detailed coverage of the Java programming language.

3. Effective Java (2nd edition)

Effective Java (2nd edition)
Author :
Joshua Bloch

Description :
This book is perfect if you have some experience in Java, this book teach you how to write code efficiently and correctly.

4. Thinking in Java (4th edition)

Thinking in Java (4th edition)
Author :
Bruce Eckel

Description :
Whether you are absolutely new to Java, or an experienced Java developer, you also will find this book a useful learning material.

5. Introduction to Programming Using Java, Sixth Edition

Introduction to Programming Using Java, Sixth Edition
Author :
David J. Eck

Description :
Good book for java beginners, and might also be useful for experienced programmers.

String Reverse Java Program


public class StringReverseExample
{
public static void main(String[] args)
{
String string="abcdef";
String reverse=new StringBuffer(string).reverse().toString();
System.out.println("\nString before reverse:"+string);
System.out.println("\nString after reverse:"+reverse);
}
}
Result:
String before reverse:abcdef
String after reverse:fedcba