While studying to upgrade my Sun Certified Java Programmer status from version 1.4 to version 6, I came across what appears to be an anomaly in the Java compiler. In Java, a literal integer (like 8) is always implicitly an int, i.e. a 32-bit value. A byte is only 8 bits. However, the following narrowing assignment is legal. No explicit cast is required.
byte b = 30;
The compiler automatically narrows the literal 30 to a byte. It can do this because 30 is a compile-time constant that is within the range of a byte. Now, recall that a floating point literal (like 43.4) is implicitly a double, i.e. a 64-bit value; but a float is only 32 bits. Consider the following code:
float f = 43.4;
You might think that this is legal because 43.4 is a compile-time constant that is well within the range of a float. If the compiler obliges us by implicitly casting an int constant to a byte, why shouldn't it do the same for double to float assignments? However, the compiler does no such thing. The above code does not compile. The literal value has to be either explicitly cast to a float, or have an f (or F) appended to it to make it a float literal. Thus, the following assignments are all legal and compile successfully:
float f = (float) 43.4;
float f1 = 43.4f;
float f2 = 43.4F;
Why is the compiler so finicky about floating points? I have not been able to think of an answer. Can you?
Thursday, September 17, 2009
Tuesday, September 26, 2006
C++ After Java
It's true!! I have been assigned to a project requiring me to program in C++. I have to leave my beloved Java programming language and move back in with C++ again. It is now about ten years since I last worked with either C or C++. It occurred to me that there might be others who have been in the same predicament, so I did a Google search on "moving from Java to C++". Lo and behold! It came up with a page with exactly the same title. There is some good stuff there that refreshed my memory about C++. Others might find it useful, too; so here is the link: Moving from Java to C++
I backtracked the link to www.horstmann.com which turned out to be the home page of Cay Horstmann, a computer science professor at San Jose State University and an author of many books on C++, Java and object-oriented programming including Core Java from Sun Microsystems Press. I found it interesting and checked out Cay's blog, too. Good stuff for Java and OO-technology afficionados.
I backtracked the link to www.horstmann.com which turned out to be the home page of Cay Horstmann, a computer science professor at San Jose State University and an author of many books on C++, Java and object-oriented programming including Core Java from Sun Microsystems Press. I found it interesting and checked out Cay's blog, too. Good stuff for Java and OO-technology afficionados.
Wednesday, September 06, 2006
Hello, world!
Well, well, well... my first post. Thinking of how to start off, I was reminded of the first program that is traditionally written by anyone learning a new programming language (or, at the least, was written in the days that I was learning my first programming languages). On the same lines, blogging is a new way for me to communicate with the world, so this is it - the "Hello World" post.
As the blog name indicates (hopefully), the theme of my posts will be software and information technology. This is intended to include the technical, human and business aspects of the software development process, methodologies, tools, frameworks (e.g., J2EE - now rebranded to Java Platform, Enterprise Edition or Java EE, in short) and IT industry news. I reserve the right to occasionally deviate from this theme and that is what the "and Everything" denotes, but I don't expect these deviations to be too far off from the main theme.
P.S.: Fans of Douglas Adams will recognize the play on "Life, the Universe and Everything", part 3 of a trilogy in four (now five) parts. I do hope that I am not infringing on any copyright by basing my blog's name on this book title.
As the blog name indicates (hopefully), the theme of my posts will be software and information technology. This is intended to include the technical, human and business aspects of the software development process, methodologies, tools, frameworks (e.g., J2EE - now rebranded to Java Platform, Enterprise Edition or Java EE, in short) and IT industry news. I reserve the right to occasionally deviate from this theme and that is what the "and Everything" denotes, but I don't expect these deviations to be too far off from the main theme.
P.S.: Fans of Douglas Adams will recognize the play on "Life, the Universe and Everything", part 3 of a trilogy in four (now five) parts. I do hope that I am not infringing on any copyright by basing my blog's name on this book title.
Subscribe to:
Posts (Atom)

