Showing posts with label java concept. Show all posts
Showing posts with label java concept. Show all posts

Friday, June 12, 2015

Primitive Message Types: Google Protobuf

Below are the primitive Message Types and their meaning, as used in Google Protobuf.

S No.
Type
Java Mapping
Usage
1
double
double

2
float
float

3
int32
int
Variable length. Not good for negatives
4
int64
long
Variable length. Not good for negatives
5
uint32
int
Variable length
6
uint64
long
Variable length
7
sint32
int
Variable length. Better for Negatives
8
sint64
long
Variable length. Better for Negatives
9
fixed32
int
4 bytes
10
fixed64
long
8 bytes
11
sfixed32
int
4 bytes
12
sfixed64
long
8 bytes
13
bool
boolean

14
string
String

15
bytes
ByteString
Variable length

Monday, June 8, 2015

Google Protobuf: proto3 enhancements

Proto3 is the new version of Google Protobuf with various enhancements and additions. Let's go over them one by one.

1. Since all fields are optional by default. Hence "optional" keyword has been done away with. If nothing is specified, it will be considered optional. (Protof files just became a tad smaller)



2. Support to group fields are not longer there. Group was just another way of nesting information in messages. But it was inefficient way to do (better way, Nested Messages). Hence is no longer supported.



3. Packed repeated fields were introduced in 2.1.0. Repeated fields needed to be explicitly set packed=true to use this encoding. In proto3, repeated primitive fields have been made packed by default. They have to be set false to be disabled. 



4. Common proto types (timestamp.proto etc) have been added. They can be used after importing. This takes a step towards usable library in protobuf. See below,



5. reserverd keyword introduced. By this, user has the control over reserved field_names and field_numbers. Once declared as reserved, these can't be used anywhere in the message.

Below, the field_numbers and names can't be used anywhere in the message again. Gives us more control.


6. Support for Objective-C and C# introduced in proto3. yay!

7. There are other language specific changes for various languages.



Google Protobuf: Introduction

Google Protobuf has simplified a historically cumbersome process of sending messages from one component of an application to another.

They have made the communication, data storage, platform independent. An object serialized in Java can easily be de-serialized in C++.

Google protobuf aids in managing structured data in a better way. Definitions are easily extensible and simple to use.

.Proto File
Data Definition is created once in a simple format. It is stored as .proto file. These proto files are much simpler to write and manage than xml files that we have been using till now.

This file is compiled and we get generated source code in whatever programming language we want. We can then populate the object using Builder/Getter-Setter, Serialize it and send/store. Later de-serialize it in a different language (or same) and retrieve the information.

 An example below



This .proto can now be compiled and language specific Data Access Classes are created. These classes have auto-generated getter/setter functions for members.

We can declare package name at the top of protofiles, dependency on other protofiles, just like any Java code. Compiler takes care of it all.

More about Google Protobuf below;

Sunday, June 7, 2015

Google Protobuf: Historical Significance

With the advent of distributed applications, the messaging between one component to another has become all too prominent. There are a lot of middleware mechanisms to do so. Tibco EMS, ZMQ, etc. I have worked on projects involving a few of such mechanisms. All have their own positives and negatives depending on the problem at hand.

While communicating between components, we need data units to be sent to and fro. What those data units will be? You might have used xml units send across. Remember how cumbersome was to write parsing code at both places. And if the components are written in different languages (Java and C++ say), then it becomes all too messy. Writing receivers at both ends and parsing logic too. Handling exceptions and what not. 

Some might have used json. Programmers of the old might remember Serialization/De-serialization and sending Java as a whole. But what to do when other component is not in Java? Again it becomes messy.


Well Google Protobuf's is one such endeavour to put those issues to an end.

Google provides libraries for various programming languages. A .proto file is created (looks somewhat like json, but not all too much), in which we can define structured data structure of the Object to be move to and fro. The same proto file can be compiled in different languages into class objects. 

These classes can now be used simply, Call setters and populate the object. Send it across. Easy de-serialization at other end, and we get local language specific Object. Call getters and get the data.

Thus a Java Object created Serialized, Sent Across, Received, De-Serialized as C++ Object.

Thus from messy history, this seems like a fresh breeze. Doesn't it?

Saturday, April 19, 2014

Overloading main() in Java

A common fresher interview goes like, Can we overloading main() in Java?
It's a very tricky question on the face of it. But if we just recollect basics, it won't be too hard to answer.

Answer is Yes, we can very much do.

By default we use String args[] as the method argument for main(). We can have multiple mains with different arguments types.





Error Cases
1. main(String args[]) is missing



Friday, July 12, 2013

15 Mandatory Core Java Interview Questions - Part 2

15 Mandatory Core Java Interview Questions - Part 1

16. What is Java Byte Code?

JVM does not understand *.java files. JDL compiled *.java files and create *.class files, which contain bytecode and understood by JVM. If we take the same bytecode and run it on different enviroments, due to the JVM which is uniform, we will get same results.

This make life simpler for developers,  isn’t it? Why worry about where the code will run. Just write the code and be done.

Compiler compiles the code in a bytecode for a Virtual Processor (JVM) which in turn converts it into Machine code for a Real Processor (LinuX/Windows platform).

17. Can we define main() as private?

Yes, It can be defined as private. Program will compile fine. But we will get a runtime Error.



18. Differentiate between final, finally, finalize.

final: final variable cannot be changed once initialized, final method can't be overridden, final Class can't be extended.

finally: code inside finally block executes after the try block (Even when there is an exception). Clean up code is normally put inside finalize to close open references in case of Exceptions.

finalize: This method is at Object level. When the Object is Garbage collected, this method is called. This method is overridden in special circumstances only for system resources clean-up.

19. List down the OOPs concepts.

a) Polymorphism
b) Inheritance
c) Encapsulation

20. Explain the concept of Polymorphism.

Well as name suggests, Polymorphism means poly-forms, means Many Forms. When same name is used in many forms, we call it polymorphism. Same entity can be used for multiple actions. It happens with Method Overloading and Method Overriding.

21. Explain the concept of Inheritance.

In Java, once class can acquire properties of another class via inheritance. Sub class (which acquires properties) inherits all methods of Super Class (parent class), In Java Only Single inheritance is allowed. A class can extend only one class.

22. Explain the concept of Encapsulation.

Encapsulation is what Java Class stands for. The data of an Object is kept safe from outside world via encapsulation. Access to it is regulated, public/private/protected modifiers are used to regulate the access. It is like putting a child in a protective wool wrap to protect him/her from the world.

23. What happen if within static context, we call non-static variable.

This will result in an Error. Static Variable belongs to the Class. This variable is initialized when Clss is loaded. At that time the Object of the class is not created. Hence we will get an Error while compilation as non-static variable does not exist at that time.



24. What is Pass by reference, Pass by value?

When an object is passed during the function call, either a replica of that object can be passes or the memory location of that object (reference) can be passed.

When passed by value, the original object is safe. Any alteration to the passed object is not reflected back to the original object. When passed by reference, original object is worked upon and all changes are reflected back.

25, Is Java Pass by Value or  Pass by Reference?

Java is Pass by Value. Period.
You will find a lot of literature on this across the spectrum, bottom line is java is just pass by value. For primitive types, the value is copied over while calling a method. For user define data types, address of the memory where the object is stored is pass by value. We don't call it pass by reference as reference is like a constant pointer (we cannot reassign a reference once assigned).

26. Can constructor be overloaded in Java?

Yes, constructor overloading is permitted in Java. Each constructor need to have it's own unique parameter list.

27. De-construct and explain public static void main (String args[]).

public: main() needs to be called by JVM outside the application, hence it needs to be public
static: main() needs to be called before instantiating any object, hence it is static
void: main() is designed not to return anything hence void
String args[]: main takes as input stream of input from cmd line (as string).

28. How to calculate size of an object in Java?

Java does not have a sizeof type Operator that is present in C. Calculating the size of a User Defined Object is tricky and we are just able to approximate by calculating size of member variables. No straightforward approach. It leads me to say that Java designers didn't think we would need to calculate the sizeof Object as memory allocation/deallocation is done by Java rather than the user.

29. Explain System.out.println

System is a final class from java.lang package. out is a public member variable of System class, of type PrintStream. println is a public method of PrintStream class.

30. Can we have global variables in Java?

The answer is No. By design global variables does not exist in Java. They create unnecessary nuisance. We can use public final static which can be used as Global Constants in a separate Class (say Constants.java). We can use like Constants.x, Constants.y.





Sunday, May 12, 2013

DEPENDENCY INVERSION (D INV)

To put it straight, DI = IoC + D Inv. How? Lets explore.
Before the helper and DI were introduced, Hands/Legs/Face were designed first. When Body was created it just used new Hands/Legs/Face. Body does not dictate the design of Hands or Legs. Suppose Hands were designed to have teeth. Body can’t dictate that Hands should not have Teeth (High level module can’t interfere with design of Low level module, it can just use it). There are there and Body has to live with it. So everyone will eat off their hands, literally.
Programming wise Body is High Level Component and Hands are Low Level Component and are kept in separate packages. Body Component Package depended upon Hand Component Package for compilation. Thus HLC depends upon LLC for compilation.
DInv says other wise. Low Level Components (Hands) also need to confirm  to some abstraction (Hands have to be type of SomeHands, so now we have SmallHands, ThinHands and also TeethHands which are type of SomeHands, but NOT all Bodies need to have TeethHands). Body is also bound by SomeHands. It can’t fit ShortLeg instead of SomeHands.
DInv says both Low Level and High Level modules need to confirm to some abstraction.
Programming wise, this is achieved by separating High Level component  (Body) and Low Level component  (Hands) into separate packages. And the Interfaces (SomeHands, SomeLegs) defining the Behavior/Services which the HLC needs are inside HLC package and are owned by it. Here LLC implement the interfaces owned by HLC. Thus LLC depend on HLC for compilation. Thus usual Dependency relationship is INVERTED.

Wednesday, March 20, 2013

DEPENDENCY INJECTION (DI)

What I feel is people understand Dependency Injection (DI) more than Inversion of Control (IoC). When I ask in an interview, “What have you used spring for?”. The answer normally is, “I have used it for IoC” or “I have used it for DI”. But when I ask the difference between IoC and DI, I mostly  get the answer, “They are the same” or “DI was earlier called IoC”.
What really is DI? Is it the really new name for IoC? Is there any difference between DI and IoC? Lets try and explore there questions.
Let me begin by stating that DI is not the new name of IoC. DI is something which affirms to IoC concept. When we do DI, we are doing IoC (Not the other way round). Following IoC does not mean DI would have happened. DI is just one of the ways to achieve IoC.
Example: God creating Man
When God creates Man. He would start with an Empty Body (Body Class). Then he would create new Hands, new Legs, new Face inside the Body (inside Body constructor). This Body now has to live with whatever Hands, Legs, Face were created (Tightly coupled). But no one can refute that a Man is created alright.
Now if God wants to create another Man (He would need to do so a lot, given there are 6 billion of us on this planet, and we are born regularly), God will start with another empty Body and try and put different features to it. Say SmallHands, RoundFace, LongLegs.
So now God will keep on creating new Bodies (class Body1, class Body2, class Body3) etc. Poor God!
How nice it would have been, had God just created one Body (written one Body class) and said that it will have SomeHands, SomeFace and SomeLegs.
How nice it would have been had God had a helper (Framework like Spring), who would create different Hands (SmallHands, LongHands), different Faces (RoundFace, SquareFace). He would just copy the generic Body God created and will insert these Hands/Legs/Face to it independently. Good for God!
This is called Dependency Injection, where dependencies (on which Body depends) are not created (inside constructor) when Body is created by God. They are instead created and fitted when Body needs to be sent to Earth.
This affirms to IoC as control of creating new Type of Hands/Face/Legs lie not with the God but with the Helper who makes job of God easier,
This is what they call “Don’t call us, We’ll call you” (Hollywood Principle). Body, please dont call Hands() constructor to create new Hands, instead when Helper needs a Body() to be sent to Earth,  it will create new Hands(), will then call Body and inject the Hands.

Tuesday, January 1, 2013

INVERSION OF CONTROL (IOC)

The more I read about IoC, the more it gets complicated. One thing gets clear though, it is one of the the most misunderstood terms. More often than not, it is confused to be an old name for Dependency Injection (DI). Also people tend to understand it directly via some Code Snippet or via its implementation by any Framework.
In reality it’s just a concept borne out of some one’s imagination one fine day. Hence it should be understood irrespective of any code/framework. First came the logic and then came the code.
Most of the people I meet at workplace, have started off their careers by reading a Java or C++ book before they started to code at workplace.
My case have been the other way round. I started off my career without reading any language specific book per say. I googled knowledge out of internet as per the need arose.
I think this would be an apt example of Inversion of Control. Instead of a Book controlling what I learn and in which sequence. I was in the control of what I needed to learn. Googling (Sending Event) about a particular topic as and when required, I learned that topic (Note: The explanation of topics on Google  is eventually the same as in the book).
To be more specific, the e-version of a book v/s printed version. In e-version, I can search the topic I want to read. So the control moves from book (printed version) to the user (e-version)
Thus the Control got INVERTED.
Examples:
1. GMAT vs CAT
Another good example that I can think of IoC is GMAT exam vs CAT exam. Those who have taken GMAT would understand this better. A question pops up, you have answer it. Then only you can move to the next question. You can’t skip and come back later to the same question. The control is with the Application (GMAT Software).
Consider at the same time CAT Online Exam. We can skip questions, come back later. Choose what question to solve when. Thus some control is shifted to the person who is taking the Exam (Inversion of Control).
2. Simple Delivery vs Midnight Delivery
Another Example is Simple vs Midnight Delivery Service. In simple service, we book a courier. It is upto the delivery guy at what time he will deliver within the SLA.
Midnight Delivery Service is when we want to send our Girl Friend flowers at midnight. Here we are able to set the time when the Delivery guy should deliver (midnight). We can even call the Delivery person and specify/change the timing. Some control shifts from the wish of Delivery Guy to us.
Complete IoC will happen if say someone has sent us a courier. When we get home, we call up the delivery guy and say , “Now I am home, you come and deliver”.