Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

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};
  }
} 

Sunday, January 3, 2016

Language Fundamentals Of Java

                 In Java there are  65536 characters but in C 128 characters.Java supports "UNICODE" character set.For every value there is equal decimal value,it is called UNICODE.UNICODE is a super set of ASCII code.Java is a universal language and it supports all characters available in various languages.

TOKENS:
A token is a smallest individual unit within program.There are 5 types of Tokens.
1.Reserved Words
2.Identifiers
3.Data type
4.Constants
5.Operators

Reserved Words :
Language provides set of predefined instructions called Reserved words.Reserved words are language related words having special meaning.The meaning of this words understand by Java Compiler.Reserved Words are classified into two types.
1.Keywords (predefined)
     a.used keywords
     b.unused keywords ex: goto , constant
2.Reserved Literals
     a.True
     b.False
     c.Null

Categories:
Keywords for datatypes:-
     1.byte
      2.short
     3.int
     4.long
     5.float
     6.double
     7.boolean
     8.char
      9.void
Keywords for modifiers:-
     1.private
     2.protected
      3.public
     4.static
     5.final
     6.strictfp
     7.synchronized
     8.transient
     9.volatile
     10.abstract
     11.native
       

Friday, January 1, 2016

Java Features

java fetaures


Java Features:

1.Simple and familiar
2.Platform independent
3.Portable
4.Architecture Neutral
5.Robust
6.Dynamic
7.Object Oriented
8.Secured
9.Distributed
10. Multi-threaded

1.Simple and familiar:
     Java simplifies programmers job by removing some features of C ,C ++ languages.
1.No pointers.
2.No make files and header files.
3.No Explicit memory management.

Java is familiar because the syntax of  Java is similar to C , C ++ languages.

Q) What is Platform?
Platform is a software which provide runtime environment for applications/programs.

1.Process management.
2.IO management.
3.Device management.
4.Memory management.

Platform is a software which build on hardware.
Operating System is called Platform.

Q) What is Platform dependence ?
--In this approach the code which is designed to work on a OS cannot be executed on other OS. Because once we compile the source code it generates machine code which is specific for the OS on which it was prepared.
--C , C++ are called Platform dependent languages.

2.Platform Independence:
There are two types of platforms.
1.software based
2.hardware based

--Java provides software-based platform.It has two components.

1.Runtime Environment
2.API(Application Programming Interface).

Java code is compiled by the compiler and converted into bytecode.This bytecode is a platform independent code because it can be run on multiple platform i.e,Write Once Run Anywhere (WORA).


3.Portable: 

We may carry the java byte code to any platform.

4.Architecture Neutral:
There is no implementation features e.g. Size of Primitive types is set.The behaviour of java program does not change from one OS to another OS.In java memory layout decisions are not made at compile time.

5.Robust:

Robust simply means strong. Java uses strong memory management. There are lack of pointers that avoids security problem. There is automatic garbage collection in java. There is exception handling and type checking mechanism in java. All these points makes java robust.

Q) What is Byte Code ?

Compiled code of Java source program is called byte code.Byte code is not a machine code.It does not have any instructions related to Operating System.Byte code is platform independent code. Byte code is not a 0 & 1. It is a collection of mnemonics.Byte code is a Java Virtual Machine code.

Q) What is the difference between Byte code and machine code ?

Byte CodeMachine Code
1.Compiled code of Java. Compiled code of C,C++.
2.It is a collection of mnemonics. It is a collection of 0 & 1.
3.Platform independent Platform dependent.
Mnemonics is called as collection of verbs or commands.

JVM: 

JVM is a software which provides runtime environment for java applications or programs.It is provided by Java.It is developed in C , C++.JVM is a specification ,abstracts given by SUN Micro systems to develop java apps.
JVM is developed by 3 people.
1.SUN micro systems.
2.OS vendors.
3.3rd party vendors.
JVM is platform dependent program.JVM translates the byte code into machine code and execute.JVM is virtual machine because it is not hardware.JVM provides JIT (Just In Time) compiler which translates byte code to machine code.

6.Dynamic:

Linking or Loading of the programs are of two types.
1.Static Loading
2.Dynamic Loading.

Staic Loading:Loading or Linking of all executable blocks before executing program is called Static Loading.C,C++ uses static loading/linking.
Disadvantages:
1.Wastage of memory.
2.Any changes in one program need to compile all programs.This is called fragile problem.
Dynamic Loading:Loading or Linking of all executable blocks during execution of program is called Dynamic Loading.Java uses dynamic Loading.

7.Object Oriented:

Java is an Object Oriented Programming Language.Object Oriented is not a language,it is a programming principle.Programming principle define set of rules and regulations for organization of data and instructions.

8.Secured:
Java is secured because there is no pointers in java.Pointers perform illegal operations like accessing memory which is not part of program.There is no pointer arithemetic in java.

9.Distributed:
Java allows you to develop networking applications.It provides set of predefined protocols.

10.Multi Threaded:
Java allows you to develop thread based multitasking applications,Multitasking allows to execute more than one operation concurrently(simultaneously).


Thursday, December 31, 2015

Java History

                                  Java programming was originally developed by Sun Micro Systems which was initiated by James Gosling, Mike Sheridan , and Patrick Naughton and released in June 1991.
The language initially called  Oak and later it was changed to Java, and Java is an island of Indonesia where first coffee was produced.
                               
                                  Sun released the first public implementation as Java 1.0 in 1996.Java is guaranteed to be WORA(Write Once , Run Anywhere)..And now Java is a product of ORACLE .
Java latest version is Java SE8 released in March 2014.
                   
                                  Initially Java was used in Embedded Systems.After 1994 because of internet it was more popular.The original motivation for Java is the need for platform independent language that could be embedded in various consumer electronic products like toasters and refrigerators.


By using Java:
1.Standalone Applications(JSE)-This is also called as Core Java.
2.Enterprise Applications(JEE)
3.Device Applications(JME)

Java Editions:
 Java comes in 3 editions.
1.JSE-Java Platform Standard Edition.
2.JEE-Java Platform Enterprise Edition.
3.JME-Java Platform Micro Edition.

JSE:-It is used to develop Standalone and web supportive applications.It is a core java language.It contains language related libararies.

What is Standalone Application?
An application developed in context of one user or system is called Standalone application.An application installed in one system can be access from same system but cannot access from same system but cannot access from another system.
              Ex:notepad
                    CUI(Character User Interface)-command based
                    GUI(Graphic User Interface)-windows based
What is web supportive applications?
Applet is called web supportive program.It is a program downloaded from server and executed within client browser.

JEE:-It is used to develop Enterprises/Business Application which works on world wide web.It is used to develop service oriented web applications (or) internet applications. JEE consists of servlets,jsp,EJB,XML,Webservices.

JME:-It is used to develop device applications(mobile etc,..).

Java Versions:-
Java SE8(March 18,2014)
Java 8(code names have been discontinued,but the code name Spider is common for Java developers).
?  lambda expressions
?  default methods
?  functional programming

Java SE7(July 28,2011)
code named Dolphin.
?  Binary literals.
?  Strings in switch statement.
?  Try with resource statement.
?  Catching multiple types of Exceptions.
?  Underscore in numeric literals.
?  Type inference for generic instance creation.

Java SE6(December 11,2006)
code named Mustang.
?  Scripting language support.
?  Pluggable Annotations.

JSE5.0(September 30,2004)
code named Tiger.
?  Generics.
?  Enhanced for loop.
?  Autoboxing/Unboxing.
?  Type safe Enums.
?  Varargs.
?  Static Import.
?  Annotations.

J2SE 1.4(February 6,2002)
code named Merlin.
?  Assertions.
?  Chained Exception.
?  Regular Expressions.

J2SE 1.3(May 8,2000)
code named Kestrel.

J2SE 1.2(Decemeber 8,1998)
code named Playground.
?  Collecction framework.
?  Java String Memory map for constants.
?  Just In Time (JIT) compiler.
?  Java foundation classes(JFC) which consists of swing 1.0,Drag and Drop , and Java 2D class libraries.

JDK 1.1(February 19,1997)
?  JDBC (Java Database Connectivity).
?  Inner classes.
?  Java Beans.
?  RMI(Remote Method Invocation).
?  Reflection.

JDK 1.0(January 23,1996)
code named Oak.






What is Java?

java
Java is a Programming Language and a platform.

Platform:Any hardware or software environment in which a program runs is known as a platform.Since,java has its own API and Java Runtime Environment,it is called Platform.

Java Example:

public class HelloWorld

{

public static void main(String args[])

    {

      System.out.println("Welcome to my Website!");

     }

}