Are you staying at home and worried about your future? Do not waste the time in worrying. International certification is vital in the process of getting hired or even salary hikes. So if you are not certified and keen to do so, here is a change to get trained for the certification during this lockdown.
Launching 20 hrs extensive course for IZ0-808 certification. Training will consist of lot many real time questions with assignments.
Over the years the need of global certification is increasingly in demand.You
may be fresher or experienced, the global certification is certainly a way to
take ahead your IT career. Out of many platforms, Oracle's Java platform is one
of the most broadly accepted platforms in the IT industry. With more than 25
years of its existence, Java most widely used platform for all kind of application
development from website to mobile apps running of cell phones.
Oracle offers
certifications to certify the developers to give them recognition about their
expertise in different areas of Java platform.
Since
shifting the ownership from Sun to Oracle, the path of Java certifications is
augmented. There is a change in the levels of examinations, their pre-requisite
etc.There are lot of confusions about which exam to appear for and which not.
In this blog, we are discussing such commonly asked questions which will be
helpful to solve all such FAQs.
I am confused between Oracle Certified
Associate (OCA) and the Oracle Certified Professional (OCP). What are these
exams ?
The new structure of
Oracle's Java Certification is bit confusing. Oracle have introduced 2 exams
OCA and OCP. Lets try to get it clear:
OCA is the entry-level cert
for Java programmers. It is an excellent beginning point to study the very
basics of the language. Normally this examination will cover basics of
Object Oriented Programming questions of Java.
OCP is the next-level cert
for Java programmers. It’s a fairly more regular exam that deals with
topics like generics, design patterns, File I/O (NIO.2), threads,
JDBC, and concurrency.
To be eligible for OCP
one must certify OCA, this is clear-cut certification path. The above exams do
not emphasis the versions as, though new versions of Java are published the
earlier exams are still preserved by Oracle for many years. Which means that
you can go for any of the version of your wish.
The following tables
show the exams(with exam codes) you have to do to get the OCA and OCP for Java
7 and Java 8:
Earlier in pre-Java 7 there was only one
exam and certification was called Sun Certified Java Programmer (SCJP, exam
code 310-065) initially and later changed to Oracle Certified Professional Java
Programmer (OCPJP, exam code 1Z0-851). This was pretty simple, and the exam was
covering almost all the topics excluding topics like JDBC etc.
What was the reason to split the
certification into two examinations? On the one hand, if you think of the
extensive list of topics included in the Java 6 exam, it makes sense. With just
one examination, getting the certification was an overwhelming task for many
people. Having two tests allows you to establish more achievable goals and also
to have a first accreditation (OCA) in less time. On the other hand, now the
examinations are more difficult (higher passing score and less time per
question) and altogether more expensive. Have a look at below table :
(The prices shown in
below table are current prices in INR as per the dollar rate.)
I suggest you go for the Java 8 certification, since
it’s comparatively new and will keep existence the latest version of the Java
Programmer certification for at least three years. The additional content you
must study for the Java 8 exam concerning the Java 7 examination is not that
much and however worth preparation.
I Have a Java 6 or Java 7 Cert. What should be
my next step?
This is really a most
discussed question. As there are many of the developers who are already SCJP
5.0/6.0 or OCPJP 7.0 certified. Of course, there is not clear instructions on
this from Oracle. This entirely depends on your aims and condition. My real
suggestion is as mentioned below:
• If you have a
Java 6 accreditation, go for the Java 8 cert. The certification you
have is suitable old, and altogether there have been significant add-ons to the
Java language in its last two significant announcements.
• If you have a
Java 7 certification, go for the Java 9 cert. You still have a pretty
recent accreditation, and the changes presented to the Java language in Java 8
are not that significant. You should also contemplate that these kinds of exams
are comparatively costly and that preparing for them needs time and effort. So
what I would do is just learn about Lambda expressions and the new calendar
classes to keep your Java knowledge conversant.
I am an Experienced Developer. Should I opt
for Oracle Java Certification?
Again this depends on
your motivation towards certification and need of the industry you are working.
Even if you are experienced developer, certification will always add the value
to your career. This will certainly improve your career path in IT industry.
There are many clients, who prefers the certified developers to work on their
projects. So, it is always better to go for certification even if you are
experienced.
Do I Need to Pay for Training to Get My Java
Cert?
This depends on your
skills/expertise in Java. You can find out the good training organizations with
certified trainers. Generally classroom training will vary from 20 to 30 hrs
sessions for single exam. You can register for such training in the well known
training institutes like SEED Infotech Ltd.,
Many times this question will make you uncomfortable in Java interview. Though most of us know the rule of multiple inheritance, the reason behind this concept is not known to many of the students.
The above question in fact can be re-framed as,
"Why Java does not support multiple inheritance but can implement multiple interfaces?"
Traditionally when we do programming in c++, we have option of extending more than one classes and utilize the services from both the classes.so what's the advantages of this design in Java that a class can only extends one class ? Since interface is a pure kind of class(abstract class actually), why not limit the number of interfaces implementation just like class extension ?
Being able to extend only one base class is one way of solving the "dimond problem"
This is a problem which occurs when a class extends two base classes which both implement the same method. Then how do you know which one to call ?
A.java:
publicclassA{publicintgetValue(){return0;}}
B.java:
publicclassB{publicintgetValue(){return1;}}
C.java:
publicclassCextends A, B {publicintdoStuff(){returnsuper.getValue();// Which superclass method is called?}}
Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.
It is the most common and regular way to create an object and actually very simple one also. By using this method we can call whichever constructor we want to call (no-arg constructor as well as parametrised).
Employee emp1 = new Employee();
2. Using Class.newInstance() method
We can also use the newInstance() method of the Class class to create objects, This newInstance() method calls the no-arg constructor to create the object. We can create objects by newInstance() in following way.
3. Using newInstance() method of Constructor class
Similar to the newInstance() method of Class class, There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects. We can also call a parameterized constructor, and private constructor by using this newInstance() method.
Both newInstance() methods are known as reflective ways to create objects. In fact newInstance() method of Class class internally uses newInstance() method of Constructor class. That's why the later one is preferred and also used by different frameworks like Spring, Hibernate, Struts etc.
Whenever we call clone() on any object JVM actually creates a new object for us and copy all content of the previous object into it. Creating an object using clone method does not invoke any constructor.
To use clone() method on an object we need to implements Cloneable and define clone() method in it.
Employee emp4 = (Employee) emp3.clone();
Java cloning is the most debatable topic in Java community and it surely does have its drawbacks but it is still the most popular and easy way of creating a copy of any object until that object is full filling mandatory conditions of Java cloning.
5. Using deserialization
Whenever we serialize and then deserialize an object JVM creates a separate object for us. In deserialization, JVM doesn’t use any constructor to create the object. To deserialize an object we need to implement the Serializable interface in our class.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();
Surprised ? Many developers might not aware of the fact that arraylist have some default size. Normally we consider arraylist with "0" size. Lets have a dig at the concept :
ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size, obviously. 10 is probably a more or less arbitrary number for the default number of elements. When you create a new ArrayList with nothing in it, then ArrayList will have made an array of 0 element, and when you add first element then it create array of 10 elements behind the scenes. Ofcourse those 9 elements are all null.
Using This line ...!
List list=new ArrayList<String>();
Made an array of 0 element
list.add("FirstValue");
It create array of 10 elements behind the scenes with "FirstValue" at its 0 index.
Every time the array is full, ArrayList creates a new, larger array and copies the elements from the old array to the new array.
Note :- (copying the array time so better to think about its first)
if list.size == 9 and we do list.add("EleventhString")
It will create new array and copy all the data in newly created array. The size of new arary depends on the current size of array and the algorithm is
The Strings in java are immutable because they are stored in the "StringPool". The basic property of String pool is not to allow the "duplicate strings". i.e. if we declare, String s1="abc"; String s2= "abc"; instead of 2 String objects only one String object will be created in String pool as content of both the Strings are equal. Ofcourse, this saves lot of memory at runtime. But because of this, there might be possibility that client1 changes the object referred by abc and client2, would get the modified String. So, to avoid this String objects are immutable in Java. Another reason of why String class is immutable could die due to HashMap.
Since Strings are very popular as HashMap key, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap. Mutable String would produce two different hashcodes at the time of insertion and retrieval if contents of String was modified after insertion, potentially losing the value object in the map.
At the same time, String was made final so that no one can compromise invariant of String class e.g. Immutability, Caching, hashcode calculation etc by extending and overriding behaviors.
Sometimes we have to convert char to String in java program. Here we will look into different methods you can convert character to string in java. We will also learn how to convert char array to String using different methods.
publicclassCharToStringJava{publicstaticvoid main(String[] args){// char to stringchar c ='a';String str =String.valueOf(c);// using Character class
str =Character.toString(c);// another way
str =newCharacter(c).toString();// string concatenation - worst performance
str =""+ c;// char array to stringchar[] ca ={'a','b','c'};
str =String.valueOf(ca);// recommended way
str =newString(ca);}}
String.valueOf(char c)
This is the most efficient method to convert char to string. You should always use this method and this is the recommended way to convert character to string in java program.
Character.toString(c)
This method internally calls String.valueOf(c), so there is no difference between this one. You can use this too if you are already using Character class in your code.
new Character(c).toString();
This is another way, however not recommended because we are creating a Character unnecessarily.
String concatenation
str = "" + c; is the worst way to convert char to string because internally it’s done by new StringBuilder().append("").append(c).toString() that is slow in performance.
Let’s look at the two methods to convert char array to string in java program.
String constructor
You can use String(char[] value) constructor to convert char array to string. This is the recommended way.
String.valueOf(char[] data)
String valueOf method is overloaded and there is one that accepts character array. Internally this method calls the String constructor, so it’s same as above method.
That’s all for converting char to string and char array to string in java.
Java 8 has been one of the biggest release
after Java 5 annotations and generics. Some of the important features of
Java 8 are:
1. Interface
changes with default and static methods
Java 8 interface changes include static
methods and default methods in interfaces. Prior to Java 8, we could have only
method declarations in the interfaces. But from Java 8, we can
have default methods and static methods in the interfaces.
For
creating a default method in java interface, we need to use “default” keyword
with the method signature. For example,
package com.seed;
publicinterfaceInterface1{
void method1(String str);
defaultvoid log(String str){
System.out.println("I1 logging::"+str);
}
}
Notice
that log(String str) is the default method in the Interface1. Now when a class will implement
Interface1, it is not mandatory to provide implementation for default methods
of interface. This feature will help us in extending interfaces with additional
methods, all we need is to provide a default implementation.
Java Interface Static Method
Java interface static method is similar to default
method except that we can’t override them in the implementation classes. This
feature helps us in avoiding undesired results incase of poor implementation in
implementation classes. Let’s look into this with a simple example.
package com.seed
publicinterfaceMyData{
defaultvoidprint(String str){
if(!isNull(str))
System.out.println("MyData
Print::"+ str);
}
staticboolean isNull(String str){
System.out.println("Interface
Null Check");
return str ==null?true:"".equals(str)?true:false;
}
}
Now let’s see an implementation class that is having isNull() method
with poor implementation.
package com.seed;
publicclassMyDataImplimplementsMyData{
publicboolean isNull(String str){
System.out.println("Impl Null
Check");
return str ==null?true:false;
}
publicstaticvoid main(String args[]){
MyDataImpl obj =newMyDataImpl();
obj.print("");
obj.isNull("abc");
}
}
Note that isNull(String
str) is a simple class method, it’s not overriding the interface
method. For example, if we will add @Override annotation to the isNull() method, it will
result in compiler error.
Now when we will run the application, we get
following output.
InterfaceNullCheck
ImplNullCheck
If we make the interface method from static to default, we will get
following output.
ImplNullCheck
MyDataPrint::
ImplNullCheck
Java interface static method is visible to
interface methods only, if we remove the isNull() method from the MyDataImpl class, we won’t be able to
use it for the MyDataImpl object.
2. Functional interfaces and Lambda Expressions
An interface with exactly one abstract method is known as Functional
Interface.
A new annotation @FunctionalInterface has been introduced to mark an interface
as Functional Interface. @FunctionalInterface annotation is a facility to avoid
accidental addition of abstract methods in the functional interfaces. It’s
optional but good practice to use it.
Functional interfaces are long awaited and much sought out feature of
Java 8 because it enables us to uselambda expressionsto instantiate them. A new packagejava.util.functionwith bunch of functional interfaces
are added to provide target types for lambda expressions and method references.
3. Java Stream API for collection classes
Before we look into Java 8 Stream API Examples, let’s see
why it was required. Suppose we want to iterate over a list of integers and
find out sum of all the integers greater than 10.
Prior to Java 8, the approach to do it would be:
privatestaticint sumIterator(List<Integer> list){
Iterator<Integer> it = list.iterator();
int sum =0;
while(it.hasNext()){
int num = it.next();
if(num >10){
sum += num;
}
}
return sum;
}
There
are three major problems with the above approach:
1.We
just want to know the sum of integers but we would also have to provide how the
iteration will take place, this is also calledexternal iterationbecause client program is handling the
algorithm to iterate over the list.
2.The
program is sequential in nature, there is no way we can do this in parallel
easily.
3.There
is a lot of code to do even a simple task.
To overcome all the above
shortcomings, Java 8 Stream API was introduced. We can use Java Stream API to
implementinternal
iteration, that is better because java framework is in control
of the iteration.
Internal iterationprovides several features such as
sequential and parallel execution, filtering based on the given criteria,
mapping etc.
Most of the Java 8 Stream API method arguments are
functional interfaces, so lambda expressions work very well with them. Let’s
see how can we write above logic in a single line statement using Java Streams.
privatestaticint sumStream(List<Integer> list){
return list.stream().filter(i -> i >10).mapToInt(i -> i).sum();
}
Notice that above program utilizes java framework
iteration strategy, filtering and mapping methods and would increase
efficiency.
4.Java Date Time API
Why do we need new Java Date
Time API?
Before we start looking at the Java 8
Date Time API, let’s see why do we need a new API for this. There have been
several problems with the existing date and time related classes in java, some
of them are:
1.Java
Date Time classes are not defined consistently, we have Date Class in bothjava.utilas
well asjava.sqlpackages.
Again formatting and parsing classes are defined injava.textpackage.
2.java.util.Datecontains both
date and time, whereasjava.sql.Datecontains only date. Having this injava.sqlpackage
doesn’t make sense. Also both the classes have same name, that is a very bad
design itself.
3.There
are no clearly defined classes for time, timestamp, formatting and parsing. We
havejava.text.DateFormatabstract
class for parsing and formatting need. UsuallySimpleDateFormatclass
is used for parsing and formatting.
4.All
the Date classes are mutable, so they arenot
thread safe. It’s one of the biggest problem with Java Date and
Calendar classes.
5.Date
class doesn’t provide internationalization, there is no timezone support. Sojava.util.Calendarandjava.util.TimeZoneclasses
were introduced, but they also have all the problems listed above.
There are some other issues with the methods defined in Date and
Calendar classes but above problems make it clear that a robust Date Time API
was needed in Java.
Java 8 Date
Java 8 Date Time API isJSR-310implementation.
It is designed to overcome all the flaws in the legacy date time
implementations. Some of the design principles of new Date Time API are:
1.Immutability: All the
classes in the new Date Time API are immutable and good for multithreaded
environments.
2.Separation
of Concerns:
The new API separates clearly between human readable date time and machine time
(unix timestamp). It defines separate classes for Date, Time, DateTime, Timestamp,
Timezone etc.
3.Clarity: The methods
are clearly defined and perform the same action in all the classes. For
example, to get the current instance we havenow()method. There are format() and parse() methods
defined in all these classes rather than having a separate class for them.
All the classes useFactory
PatternandStrategy Patternfor better handling. Once you have
used the methods in one of the class, working with other classes won’t be hard.
4.Utility
operations:
All the new Date Time API classes comes with methods to perform common tasks,
such as plus, minus, format, parsing, getting separate part in date/time etc.
5.Extendable: The new Date
Time API works on ISO-8601 calendar system but we can use it with other non ISO
calendars as well.
Java 8 Date Time API
Packages
Java 8 Date Time API consists of following packages.
1.java.time Package: This is the base package of new
Java Date Time API. All the major base classes are part of this package, such
as LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration etc. All of these classes are
immutable and thread safe. Most of the times, these classes will be sufficient
for handling common requirements.
2.java.time.chrono Package: This package defines generic
APIs for non ISO calendar systems. We can extend AbstractChronology class to create our own calendar
system.
3.java.time.format Package: This package contains classes
used for formatting and parsing date time objects. Most of the times, we would
not be directly using them because principle classes in java.time package
provide formatting and parsing methods.
4.java.time.temporal Package: This package contains temporal
objects and we can use it for find out specific date or time related to
date/time object. For example, we can use these to find out the first or last
day of the month. You can identify these methods easily because they always
have format “withXXX”.
5.java.time.zone Package: This package contains classes
for supporting different time zones and their rules.