Tuesday 29 December 2015

What should I learn Java or .NET?

The first question we need to ask - can these two be compared? A 30,000 ft. view of these two will tell you that Java is a programming language (generally speaking) while .Net is a framework. .Net, as you might know, is a platform that supports several languages viz. C#, VB.Net, F# and others. A framework is nothing but a pool of ready-made functionality available at your fingertips when you are writing code using that framework.
Those who are interested in deeper analysis may perform search on the Internet to find out which among the two scores over the other for specific aspects. But generally speaking; both Java and .Net are quite robust and have found applications world-wide in very complex projects.

Which is better for future Java or .NET?

Instead of worrying about that; why not make yourself future-proof? Like I just said, no one can predict the future in the technology domain; because things change here every single minute. As a software engineer / developer - you should *not* stick to any specific language; yet master one that you are currently working on. Let me make it very clear : You need to be master of at least one and jack of several! Any programming language is just a set of tools that help you implement logic. If you hone your logic skills; you should be able to get comfortable with any programming language, tool or framework within short time. I've seen that happen with few of my ex-colleagues. 

Start with any - and learn it with all the interest and try to be excellent. If you pick up Java; don't hate .Net and vice-versa. If your current job demands C# and ASP; learn it. If you are just starting out with a big IT company that has not told you which profile they will assign to you; it just doesn't matter. 

Monday 16 November 2015

Design Patterns in Java

Hi friends...
Hope you guys had wonderful and environmental Diwali. From today onwards we will be discussing different design patterns in Java..
Normally design patterns are classified in following types :

S.N.Pattern & Description
1Creational Patterns
These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.
2Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
3Behavioral Patterns
These design patterns are specifically concerned with communication between objects.
4J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center.
We will star the discussion with different design patterns from in my coming blog 

Tuesday 13 October 2015

Why inner classes ?

Hi folks,

For times you are writing inner class. The definition of inner class is quite simple "Class within a class is inner class". :-)
The problem is most of us doesnt know why the inner classes are required? Though we know, how to write, how to create object and call the methods? Why it is done remains unanswered...

Here I am giving you an example to clear it.. Hope it helps :

Suppose you have a Java GUI (Graphical User Interface) class that acts as a chat client like Gchat or Facebook Chat. Think about what methods would need to be present in a class like that which represents a chat client – you will need a method that will read user input from the chat box, methods to actually send the user input to the server and to whoever the user is chatting with, even a method that detects that you are typing so that Gchat can display the “XZY is typing” text to your friends, etc.

Event handlers

But, there is one key thing missing here. How exactly will those methods be called? Well, as an
example, think about how Gchat works – you type in some text and when you are ready to send it to whoever you’re chatting with, you press the “Enter” or “Return” key on your keyboard. So, the “Enter” key could be considered one event that triggers a call to one of the methods we mentioned earlier. And, if our chat client class wants to detect if someone is typing in a window, then clearly the event that would trigger the call is someone typing – so we would need some code to detect when someone is actually typing in a window in real time – basically when they are pressing a button inside their chat window.
So, these events – like typing in a window and pressing the “RETURN” key – will also need some methods to detect when they occur, and those event handling methods can then call the appropriate chat client methods. For example, when the event handler method that handles the “RETURN” key button pressed event is called, then that method can call the method to send the user input to the server.

There are two types of methods needed in our example

So, it should be clear that there will need to be two different types of methods that will drive your chat client application: 1. Chat client specific methods like those that will read user input from the chat box and send user input to the server. 2. Event handling methods that will respond to actual events that occur in the chat client window – like hitting the “RETURN” key, detecting consistent typing, etc.
Because it’s clear that there will need to be two different types of methods, then from an Object Oriented Design perspective, it seems like we should have two different classes: one class for the chat client specific methods and one class for the event handling methods. That does follow the normal object oriented design practices – because we are creating classes that specialize in what they do.

The problem with having two separate classes

But, in this particular example, there is a problem with having two separate classes. And that problem is the fact that the event handling code is very much related/tied to the code that belongs to the chat client. This makes sense, as we talked about earlier with our GChat example; as soon as a user in GChat hits “RETURN” or “ENTER” on the keyboard that should trigger an event, which should then grab the text from the chat client window. And, the chat client window would be a particular instance (basically an object) of the chat client class. For example, if you are talking to your friend Bob in GChat, he will have one window in Gmail and you will have one window open in Gmail, and each window is an object of the chat client class. So there will be two separate objects of the chat client class – one for you and one for Bob.
Now, if Bob types something and hits the RETURN key, the event handler code that responds to the RETURN key being pushed will need to be able to communicate with Bob’s chat client class object so that it can grab the text from Bob’s chat client class window, or the text field where that text is actually stored. The key here is thinking in terms of objects – an object of the chat client class is created for each person using GChat, so you will have your own object and Bob will have his own object. This means that the event handling code will need to access an chat client object’s text field – which is a member of the class, and an instance variable. So, it should be clear that the event handling code needs access to the members of a chat client class object in order to be able to effectively help.

Why don’t we just combine the event handling code with the chat client code?

Combining the event handling methods with the chat client specific methods in one big class sounds like a good idea at first, but there is one big problem: If both the event handling code and the chat client code need to inherit some code from different classes then you are in trouble, because Java does not support multiple inheritance – meaning that our one “big class” can not inherit from two different classes.
This means that we want some sort of solution where the event handling code is in it’s very own class – which would allow it to inherit from any class it needs, because it’s not bundled together in one class with the chat client code as well. This is also better object oriented design. But, we also want the event handling class to have easy access to the chat client’s member variables – even the private instance variables. One possible solution is to make everything in the chat client class public so that anyone can access it, including the event handling code. But, making everything public is a bad idea.

Inner classes to the rescue

Now, this is why inner classes were created – for situations exactly like the one we described above. An instance of an inner class can access members of an instance of the outer class, because an inner class is just another member of the outer class. And, inner classes can even access the private members of the outer class – yes you did read that correctly!


Thursday 8 October 2015

Everything about public static void main( ) in Java

Its more than thousands time you might have written public static void main( ) java. But how many of you really know the details? Whats the reason to write it? What is the meaning of each and every word of it? 
Well.. I believe strongly, whatever you write in your code, you must be fully convinced about why you are writing it. Else it doesnt makes a sense... really IT DOESNT MAKES A SENSE... 

public is the visibility. This can be public, private, protected or (if you omit a value) default.
static is a special [optional] keyword that indicates that this method can be called without creating an instance of this class. Without it, you have to instantiate this class and call this method from the resulting object.
void is the return type of this method, indicating that this method doesn't return anything. Methods must have a return type.
main( ... ) is the name of this method. Methods have to be named. The parentheses indicate that this is a method.
String[] args is a single parameter for the method. String[] is the type of the parameter, indicating an array of Strings. args is the na
me of the parameter. Parameters must be named.
The method signature can therefore be:
public static void main( String[] args )
public static void main( String... args )
note that the varargs version (...) is only valid from Java 5..
If you want to dig more about it here I go : 
If you look into JDK source code (jdk-src\j2se\src\share\bin\java.c):
/* Get the application's main method */
mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
                   "([Ljava/lang/String;)V");
...
{    /* Make sure the main method is public */
...
mods = (*env)->CallIntMethod(env, obj, mid);
if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
    message = "Main method not public.";
    messageDest = JNI_TRUE;
    goto leave;
...

Tuesday 29 September 2015

Data Conversions ... Conversion Integer to String

Converting Integer to String
The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.
Syntax 
public static String toString(int i)
The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.
Example 

int number = -782;
String numberAsString = Integer.toString(number);
The code is equivalent to: 

String numberAsString = "-782";
If you will try and search for solutions, this is one of the most popular ways of converting an int to String. 



The String class has several static methods that can convert most primitive types to their String representation. This includes integers.
Example 
int number = -782;
String numberAsString = String.valueOf(number);
or 

String numberAsString = String.valueOf(-782);
This is also an efficient solution like the first option above. And because this is simple and efficient, it is also a very popular method for converting an int to String. 



Another alternative method is to create an instance of Integer class and then invoke it's toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Integer is created before conversion is performed.
Example 
int number = -782;
Integer intInstance = new Integer(number);     
String numberAsString = intInstance.toString();
We can shortened to: 
int number = -782;
String numberAsString = new Integer(number).toString();
or just: 
String numberAsString = new Integer(-782).toString();
If your variable is of primitive type (int), it is better to use Integer.toString(int) or String.valueOf(int). But if your variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke it's toString() method as shown above. 



String.format() is a new alternative that can be used for converting an Integer to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.
Syntax 

public static String format(String format, Object... args)
There are many options on how to use this method. But for conversion purposes, here is a simple example:
Example 

int number = -782;
String numberAsString = String.format ("%d", number);
And the variable numberAsString will have the value "-782" 

If you are using Java 5 or higher, this is also a very simple alternative for converting an int to String object.



The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern. 
Example 
int number = 12345;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output 

12345
This is an example that will convert to String but with commas 
int number = 12345;
DecimalFormat decimalFormat = new DecimalFormat("#,##0");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output 

12,345
This is my favorite from all the options shown in this post because of the level of control that you can do with the formatting. You can specify the number of decimal places and comma separator for readability. 



StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer. These two classes can be used to convert a Java Integer to String. 

StringBuffer Example 
int number = -782;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();
StringBuilder Example 
int number = -782;
StringBuilder sb = new StringBuilder();
sb.append(number);
String numberAsString = sb.toString();
Shortened Examples 
String numberAsString = new StringBuffer().append(-782).toString();
String numberAsString = new StringBuilder().append(-782).toString();


Attend Online Java Certification Training and excel your career

Hello Java Developer,  Are you staying at home and worried about your future? Do not waste the time in worrying. International certifi...