Wednesday, July 29, 2015

Functional Interfaces

Functional Interfaces

In single line description, Interface with only abstract method are referred as Functional Interface. However, A functional interface can have more than one static or default methods, and also can Override some methods from java.lang.Object

Luckily you do not need to verify the Functional Interface, Compiler will do it for you. But you can specify @FunctionalInterface annotation to reduce it’s workload.

Below is an example of Functional interface.
 interface Demo { 
    // Only abstract method. 
    void method1(Collection collection); 
    // Functional interface can have more than one static or default  methods. 

    default void method2() 
    { 
      System.out.println("This is default in Functional Interface."); 
    } // And also can override methods of java.lang.Object 
    @Override 
    public String toString(); 
     
  }

Some frequently used and popular Functional Interfaces are:  

  • java.awt.event.ActionListener;
  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.beans.PropertyChangeListener

Note :

A new package in Java 8 is added that contains functional interfaces that are commonly used for lambda expression and method reference : java.util.function 

Tuesday, July 28, 2015

scjp summary


Before we dig into class declarations, let's do a quick review of the rules associated with declaring classes,
  There can be only one public class per source code file.
■
Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.
■
If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.
■
If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
■
the first line in the source code file.
If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be
■
import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
■

Class Declarations and Modifiers
Java is a package-centric language; the developers assumed that for good organization and name scoping, you would put all your classes into packages.
They were right, and you should. Imagine this nightmare: Three different programmers, in the same company but working on different parts of a project, write a class named Utilities. If those three Utilities classes have not been declared in any explicit package, and are in the classpath, you won't have any way to tell the compiler or JVM which of the three you're trying to reference. Sun recommends that developers use reverse domain names, appended with division and/or project names. For example, if your domain name is geeksanonymous.com, and you're working on the client code for the TwelvePointOSteps program, you would name your package something like com.geeksanonymous.steps.client. That would essentially change the name of your class to com.geeksanonymous.steps.client.Utilities. You might still have name collisions within your company, if you don't come up with your own naming schemes, but you're guaranteed not to collide with
classes developed outside your company (assuming they follow Sun's naming  convention, and if they don't, well, Really Bad Things could happen).
A file can have more than one nonpublic class.Files with no public classes can have a name that does not match any of the classes in the file.

■
import statements, and package statements in a source file:

Source File Declaration Rules

Friday, July 24, 2015

JVM

Summary on Garbage collection in Java


1) Java Heap is divided into three generation for sake of garbage collection. These are young generation, tenured or old generation and Perm area.

2) New objects are created into young generation and subsequently moved to old generation.

3) String pool is created in Perm area of Heap, garbage collection can occur in perm space but depends upon JVM to JVM.

4) Minor garbage collection is used to move object from Eden space to Survivor 1 and Survivor 2 space and Major collection is used to move object from young to tenured generation.

5) Whenever Major garbage collection occurs application threads stops during that period which will reduce application’s performance and throughput.

6) There are few performance improvement has been applied in garbage collection in java 6 and we usually use JRE 1.6.20 for running our application.

7) JVM command line options –Xmx and -Xms is used to setup starting and max size for Java Heap. Ideal ratio of this parameter is either 1:1 or 1:1.5 based upon my experience for example you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.

8) There is no manual way of doing garbage collection in Java.


10 Points about Java Heap Space


1. Java Heap Memory is part of Memory allocated to JVM by Operating System.

2. Whenever we create objects they are created inside Heap in Java.

3. Java Heap space is divided into three regions or generation for sake of garbage collection called New Generation, Old or tenured Generation or Perm Space.

4. You can increase or change size of Java Heap space by using JVM command line option -Xms, -Xmx and -Xmn. don't forget to add word "M" or "G" after specifying size to indicate Mega or Giga. e.g you can set java heap size to 258MB by executing following command java -Xmx256m HelloWord.

5. You can use either JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to query about Heap size programmatic in Java.

6. You can use command "jmap" to take Heap dump in Java and "jhat" to analyze that heap dump.

7. Java Heap space is different than Stack which is used to store call hierarchy and local variables.

8. Java Garbage collector is responsible for reclaiming memory from dead object and returning to Java Heap space.

9. Don’t panic when you get java.lang.outofmemoryerror, sometimes its just matter of increasing heap size but if it’s recurrent then look for memory leak in Java.

10. Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object.

Difference between soap and restfull webservice java


Both SOAP and RESTful web services allows a client to query server for some information, but the way they are implemented and used is quite different. Main difference between SOAP and REST is that SOAP provides an standard form of communication between client, server and other parties and has restricted set of rules and format, while REST uses to maximum of HTTP protocol, in both client and servers, to allow them to communicate with each other regardless of their implementation.

I found some really awesome comparisons on web, I am posting here


REST vs SOAP Web Services

I am seeing a lot of new web services are implemented using a REST style architecture these days rather than a SOAP one. Let’s step back a second and explain what REST is.

What is a REST Web Service?
The acronym REST stands for Representational State Transfer; this basically means that each unique URL is a representation of some object. You can get the contents of that object using an HTTP GET, to delete it, you then might use a POST, PUT, or DELETE to modify the object (in practice most of the services use a POST for this).

Who's using REST?
All of Yahoo's web services use REST, including Flickr, del.icio.us API uses it, pubsub, bloglines, technorati, and both eBay, and Amazon have web services for both REST and SOAP.

Who's using SOAP?
Google seems to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprise software as well.

REST vs SOAP
As you may have noticed the companies I mentioned that are using REST api's haven't been around for very long, and their api’s came out this year mostly. So REST is definitely the trendy way to create a web service, if creating web services could ever be trendy (let’s face it you use soap to wash, and you rest when you’re tired). The main advantages of REST web services are:

Lightweight - not a lot of extra xml markup Human Readable Results Easy to build - no toolkits required SOAP also has some advantages:

Easy to consume - sometimes Rigid - type checking, adheres to a contract Development tools For consuming web services, its sometimes a toss up between which is easier. For instance Google's AdWords web service is really hard to consume (in CF anyways), it uses SOAP headers, and a number of other things that make it kind of difficult. On the converse, Amazon's REST web service can sometimes be tricky to parse because it can be highly nested, and the result schema can vary quite a bit based on what you search for.

Note: Whichever architecture you choose make sure it’s easy for developers to access it, and well documented.


Another Reference:

RESTful Services are appropriate in these scenarios:
•          If you have limited bandwidth
•          If your operations are stateless
•          If your clients require caching

While SOAP is the way to go when:
•          If you require asynchronous processing
•          If you need formal contract/Interfaces
•          In your service operations are state full

Thursday, August 23, 2012

Summary of hashCode and equals methos in Java


Summary & Miscellaneous Tips about hashCode and equals method
  • Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
  • The equals method provides "deep comparison" by checking if two objects are logically equal as opposed to the "shallow comparison" provided by the equality operator ==.
  • However, the equals method in java.lang.Object class only provides "shallow comparison", same as provided by the equality operator ==.
  • The equals method only takes Java objects as an argument, and not primitives; passing primitives will result in a compile time error.
  • Passing objects of different types to the equals method will never result in a compile time error or runtime error.
  • For standard Java wrapper classes and for java.lang.String, if the equals argument type (class) is different from the type of the object on which the equals method is invoked, it will return false.
  • The class java.lang.StringBuffer does not override the equals method, and hence it inherits the implementation from java.lang.Object class.
  • The equals method must not provide equality comparison with any built in Java class, as it would result in the violation of the symmetry requirement stated in the general contract of the equals method.
  • If null is passed as an argument to the equals method, it will return false.
  • Equal hash codes do not imply that the objects are equal.
  • return 1; is a legal implementation of the hashCode method, however it is a very bad implementation. It is legal because it ensures that equal objects will have equal hash codes, it also ensures that the hash code returned will be consistent for multiple invocations during the same execution. Thus, it does not violate the general contract of the hashCode method. It is a bad implementation because it returns same hash code for all the objects. This explanation applies to all implementations of the hashCode method which return same constant integer value for all the objects.
  • In standard JDK 1.4, the wrapper classes java.lang.Short, java.lang.Byte, java.lang.Character and java.lang.Integer simply return the value they represent as the hash code by typecasting it to an int.
  • Since JDK version 1.3, the class java.lang.String caches its hash code, i.e. it calculates the hash code only once and stores it in an instance variable and returns this value whenever the hashCode method is called. It is legal because java.lang.String represents an immutable string.
  • It is incorrect to involve a random number directly while computing the hash code of the class object, as it would not consistently return the same hash code for multiple invocations during the same execution.

Saturday, January 21, 2012

How to get certified with Oracle



After spending around  year as a Java Developer  I thought of having a certification in Java. I  have started google to get the information.  So I want to share the information with you all which might helpful for you all.

Sun offers six levels of Certification exams for Java technology developers. In increasing level of difficulty, these are - 
  1. Sun's Java Associate (SCJA)
  2. Sun's Java Programmer Certification (SCJP)
  3. Sun's web component Developer Certification (SCWCD)
  4. Sun's business component Developer Certification (SCBCD)
  5. Sun's Developer for Java Web Services (SCDJWS)
  6. Sun's mobile application Developer Certification (SCMAD)
  7. Sun's Java Developer Certification (SCJD)
  8. Sun's Java Architect Certification (SCEA)

I have started searching the kind of certification I can go through. I found the relevant one for me is following :
Exam Number:1Z0-851
Associated Certifications:Oracle Certified Professional, Java SE 6 Programmer (SCJP 6.0)
Exam Registration:Register

About the Contents :
The Sun's Java Certification Programmer 6 exam consists of 60 questions. These need to be completed in 210 minutes. To clear the certification exam you need to get 61% correct answers.
There are three type of questions in the exam - 
  1. Multiple choice questions in which you have to select one or more correct answers from a set of options. A majority of the questions are of this type. In the exam it would be clear from the question whether it has one correct answer or multiple correct answers. For questions with multiple correct answers, you must get all the correct answers, to get credit for that question. Also these type of questions will mention the number of correct answers.
  2. True or False type questions
  3. Questions that require you to drag and drop items from one column to another.
The test paper covers contents (what I found is) as follows :

Declarations, Initialization and Scoping
Flow Control
API Contents
Concurrency
OO Concepts
Collections / Generics
Fundamentals

* Will discuss above points in my coming blogs.

The details can be found on the oracle's websites  Click Here

Where To take Exam

Now we know what should be the done but where and  how to get the exam. Lets talk about the same.
First get your nearest vendor affiliated with Oracle. you can find the nearest vendor from the Pearson VUE.
For scheduling your exam you need to have a web account with the Pearson VUE. once registering with the site you can get your login credentials with 24 hours.
After getting the credentials you can select the type of the certification you are going to join. In case of SCJP 6.0 we have to select following :




  • Once you logged in with the specific vendor you can see all the certification available with that Vendor. 
  • Select your's and you will be prompted for the search and select of the nearest center where you can give your exam.
  • After selecting the centre you will be get options to get the appointment for the exam. 
  • The selection of dates lands you on the payment page. Once you done with the payment you will get the e-receipt. Or if you don't want to pay online you can contact to your Test center.


For more help or guidelines Click Here






Traditional Threads vs. Virtual Threads: A Performance Benchmark on Spring Boot 4

Traditional Threads vs. Virtual Threads: A Performance Benchmark on Spring Boot 4 Bottom line: For high-concurrency Spring Boot apps on mo...