Monday 17 August 2015

Creating an object instance with out using new keyword in Java

Java supports both primitive types and non-primitive types. Primitive types are nothing but int, char, float, boolean, double etc. Non-primitive types are any classes like String, File or any other user defined types. Although primitive types can be created with out using new keyword, most of non-primitive types need new keyword.

Assume that we have the following classes defined. Student with out constructor and Book with constructor.

class Student
{
    String name;
    int marks;
    char section;
}

class Book
{
    Book(String name, String title)
    {
        this.name = name;
        this.title = title;
    }
    String name;
    String title;
}


There are essentially three ways in which we can create an object with out using the new keyword.

Using reflection

import java.lang.reflect.*;

class CreateObjectUsingReflection
{
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException
        {
            // Creating an object with default constructor
            Class studentClass = Class.forName("Student");
            Student rick = (Student) studentClass.newInstance();
            rick.name = "Rick";
            System.out.println(rick.name);
            // Creating an object with two strings constructor
            Class bookClass = Class.forName("Book");
            Constructor twoStringsConstructor = bookClass.getDeclaredConstructor(String.class, String.class);
            Book learn_java = (Book) twoStringsConstructor.newInstance("Learn Java Perfectly", "Merit Campus");
            System.out.println(learn_java.title + " with " + learn_java.author);
        }
}


Try this program now

Wednesday 12 August 2015

Are you learning on your Job?

After you join any Job, most of your active time goes for it. Either you are working or you are travelling to work or you are relaxing after the long day at work. In many cases you will be spending close to 12 to 14 hrs everyday doing your Job.

So it is very important that you like your job and you are learning and growing on your job. Otherwise it becomes a routine and you will become dispensable. You can be easily replaced by not so experienced person for half the salary or less.

Wherever you are doing a job, ask yourself these questions.

1. Is the current job challenging enough that it requires you to keep up to date with the latest technologies.
2. Even in the current technology does it give opportunities to learn new ways of doing things?
3. Are you in sync with the latest development methodologies?
4. Are you using the tools to make your efforts more fruitful?
5. Is your job repetitive and does not require you to think?

If the answers are not encouraging then it is time to change. It is always better to work in an environment where you are learning and progressing every day than work at an organisation where there is no learning or improvement of your skills.

Review your skills every 2 months, if you have not improved or become better from the last review then it is time to move to new role in your current project or new project in your current Organization or new project in a different organisation. But ensure that you are learning where ever you are.

Tuesday 11 August 2015

Should I learn Java or .Net or Ruby?

Should I learn java or .net or ruby

Many freshers have this question of whether to learn Java or .Net or Ruby or some other programming language. The first question you have to ask is whether you want to become a programmer or do you want to become a tester/QA. Depending upon your aptitude and your ability to solve logical problems you can choose to become a programmer or a tester.

Once you have decided to become a programmer, then the next big question is what programming language do you start with?

In order to answer that question you need to consider the following points.

1. Do you have any prior programming experience in your college or project? 

If you have spent considerable amount of time programming in your college or in mini project then it is better to continue with that language. This will give you an edge to perfect your programming skills.

2. How many of your friends or acquaintances are working in that programming language? 

If you have some professionals who are working with the same programming language, then it is advantageous. Because programming is difficult and having people to help you when you are stuck is really important. Otherwise there are very good chances that you will give up.

3. Do you have any training centre or online tutors who can help you with your practice? 

For any programming language it is important that you do lot of practice. Without practice just listening to classes will not help. So you should find some trainers who help you with your practice. Without this it will be very difficult to make a career as a programmer.

4. Do you have good material like topics, videos and practice exercises on this language?

As mentioned earlier learning programming is time consuming and needs lot of effort. Having good material with topics, videos and practice exercises make this process a bit smoother.

5. What are the market trends? What are the new innovations in that language?

If you have same amount of access for friends/acquaintances who know that language, tutors who can help you with practice and same good material, then you can consider market trends and job opportunities. Directly choosing a language because there is market today is not a good idea.

For long term success it is important that you learn one programming language thoroughly than just knowing the basics of market trending language.

Tuesday 4 August 2015

Thread vs Runnable vs Future vs Callable vs Execution Service

As Thread, Runnable were part of the multi threaded programming in the initial versions of Java, where as Future, Execution Service and Callable are added in the later version. We can achieve high performance and multi tasking using both ways, but here are few guidelines which you can use to decide what to use.

How many threads will you be launching and how frequently are they used?

If you use lot of threads and there are always tasks to perform using threading, then you could use Execution Service. Examples include processing user requests on high traffic websites etc. But if you have to launch the threads only once in a while for batch cleanup or report generation etc, then you can launch the thread using Thread class. This way you will be saving resources by preventing the execution service running all the time.

This will decide whether to use ExecutionService or Thread.

Does the calling thread need to know the status of the task?

If the calling thread need to know the status of the task, then it is better to use Callable. Callable actually is a replacement for Runnable, but was given a different name to achieve backward compatibility. So where ever you want to use Runnable, you can use Callable. But if you have Java code prior to 1.5 and it only knows Runnable, then you can use it.

This will decide whether to use Runnable or Callable.

Hope you find these guidelines useful and leave your comments below.

Other important notes:


  1. We need either Thread or ExecutionService for doing tasks parallely, just Runnable or Callable can not achieve multi threading.
  2. ExecutionService is a pool of threads and hence uses Threads internally.
  3. If required you can convert a Callable object into a Runnable using some simple wrapper classes.
References:



Learning Java Online?

Are you planning to Learn Java Online? You do not have any prior programming experience? Do you want easy to read topics? Do you want to write lots of practice tests? Do you want learn by doing?


If the answer is yes for all these questions then Merit Campus is one of the best websites you can consider for online Java training. There are good reasons for them.

Learning Java Online


If you are working professional or if you are a student and you do not have enough time to learn Java, then Online courses are the best. Since they will drastically reduce on the time spent and they are much more effective than regular classes, since you can practice on your computer while the classes are going on. In normal classroom training, it is not possible since you have to wait till the class is over to try your learning. Learn online and become effective.
 

Prior Programming Experience


At Merit Campus, we have the tutorials for the beginners. We teach you from the basics and do not expect you to have any prior programming experience. As long as you are reasonably good with your logical thinking and aptitude, then you are eligible for learning Core Java from the basics. 

Easy to read Core Java Topics


Merit Campus has easy to read Core Java Topics. All the topics are written in simple english and illustrated with multiple images which makes understanding easy. Most of the topics have java program examples and the things to try make it easy to try the learning immediately. The topics are well organised and easily navigatable or can be search instantly.

Learning by Doing


Like any other skill, Java programming can only be learnt by doing. We have numerous java questions (2000 MCQs and 600+ Java Programs), to give you enough practice. We have various types of test formats like 'Easy Session Tests', 'Test on Recent Topics', 'Random Revision Test' and tests with various expertise levels like Beginner, Intermediate and Expert. Please see Easy Java Sessions  and Java Level Tests


Monday 3 August 2015

Merit Campus Frequently Asked Questions (FAQ)


What all services does Merit Campus provide?


Our highest priority is your learning and we will try to do whatever is necessary for you to become good programmer. To enable this learning we provide multiple topics to read from and practice with 600+ Java Questions and 2000+ MCQs. In case you need more help, we also provide live training by the experts. Please see Expert Java Training To Learn Java Online

What tests should we write to participate in the competitions?


There are no separate tests for participating in the various competitions. The marks from your practise tests will be considered for the competitions.

What should I do if I get duplicate questions while writing the tests?


If you are writing 'Test on Recent Topics' or 'Topics Test', there are chances that you will get duplicate questions. If you have answered it correctly earlier, then you skip those questions. There is no impact on your total score by skipping these questions, since you will get the marks only once, no matter how many times you write the answer.

Why we have duplicate questions?


We purposely discourage you to take too many 'Test on Recent Topics'. Instead we suggest you use Random Revision Test, which will give questions randomly so that you are more prepared for the real-life problems. Please see http://meritcampus.blogspot.in/2015/07/random-revision-tests-for-effective.html for the importance of random tests.


Why should we identify our self to participate in competitions and for placement support?


Since we want to prevent fake registrations and be able to provide better services to our users, we need to uniquely identify our users. This will make our services more credible and the companies recruiting from us will have valid data.

What should I do get my recharges from daily dose, program hint or any other facebook contests?

If you have won some contests on facebook and you got mobile recharges, they will be reflected in the earnings page, only after you login with the same facebook id into Merit Campus using the 'Login With Facebook' option in the login page. So please logout from Merit Campus and login again with facebook and after that go to earnings page.

Does Merit Campus provide any Job Guarantee?

Merit Campus does not provide any guarantee for any kind of job. But we will try our level best to place good programmers in good companies.

Why we don't work on weekends?

Unfortunately we don't work on weekends since all our trainers are also programmers working on real-time projects. Since they will be working only during the week days, this is a problem. But during weekdays we work from 7AM to 9PM IST so that we can do mornings and evening classes.

What if I could not find the answer, I was looking for?

Please contact us using the details at Contact Us and we will get back as soon as we can.