A SCJP question about arrays

January 10th, 2007

Question: Given the following:

1.public class CommandArgs {
2.   public static void main(String[] args) {
3.     String s1 = args[1];
4.     String s2 = args[2];
5.     String s3 = args[3];
6.     String s4 = args[4];
7.    System.out.println(" args[2] = " + s2);
8.  }
9.}

and the command-line invocation,

java CommandArgs 1 2 3 4

what is the result?

  1. args[2] = 2
  2. args[2] = 3
  3. args[2] = null
  4. args[2] = 1
  5. Compilations fails
  6. An exception is thrown at runtime

Ok, this question is trying to cheat you. Look well. First of all, the first index in a Java array is zero. Then, the command line showed sets args[0] = 1, args[1] = 2, args[2] = 3 and args[3] = 4. The assignment:

6.     String s4 = args[4];

Causes a RuntimeException, or more precisely, an ArrayOutOfBoundsException. The correct answer is 6.

References:

A SCJP question about right shifts and two´s complement aritmetic

January 10th, 2007

Which of the following expressions results in a positive value in x?(Choose all that apply)

  1. int x = -1; x = x >>> 5;
  2. int x = -1; x = x >>> 32;
  3. byte x = -1; x = x >>> 5;
  4. int x = -1; x = x >> 5;

To solve this question, we need to know:

  • All numerical data types are stored as bit patterns.
  • how to represents negative numbers using two´s complement.
  • Integers are signed 32 bits values, bytes are 16 bits values.
  • All numerical operands returns a integer value.
  • >> is a right shift operand (keeps the leftmost bit unchanged), >>> is a unsigned right shift operand (left-bits are zero filled).
  • When the value to be shifted (left-operand) is an int, only the last 5 digit bits of the right-hand operand are used to perform the shift. The actual size of the shift is the value of the right-hand operand masked by 31 (0×1f). ie the shift distance is always between 0 and 31 (if right-operand is >= 32, the shift is the right-operand % 32)

With all this by heart, it becomes easy now.

In the first item, we need to use two´s complement to know what is put into the x variable. To represent a negative number, first we invert each bit, then add 1. In this case, 1 = 00000000000000000000000000000001, inverting becomes 11111111111111111111111111111110, then adding 1, 11111111111111111111111111111111.

The operator >>> is a unsigned right shift. In other words, the shift affects all the bit pattern, including the leftmost bit. Doing 5 unsigned shifts in 11111111111111111111111111111111 returns 00000111111111111111111111111111. I´m too lazy to make the aritmetic, but this number is positive for sure (you see the leftmost bit?). The first alternative is part of the answer.

In the second alternative, how the right-operand is equal 32, the shift is 32 % 32 = 0, then the bit pattern stay unchanged, returning a negative value.

The third is easy. Remember: numerical operands returns integers values, always. Then, without a cast, the third item returns a very nasty:

Type mismatch: cannot convert from int to byte

The fourth item shows a signed shift operand, then, doesn´t matter how many shifts, the result is negative.

Conclusion: only the first item returns a positive value.

Java Certification Path - Class Access - Public Access

November 19th, 2006

A class with the public modifier gives to all classes in all packages access to it. In other words, all classes can instantiate, extend or return a class of that type. Example:

package humanity;
public class Person {}

This class is visible to all classes in all packages. But if this class is in a different package, we need to import it yet.
Some exceptions exists, mainly when we use the nonaccess final modifier. I will explain soon.

Java Certification Path - Reserved Words

November 18th, 2006

Ok, my most boring post until now. Don’t forget these Java keywords and reserved words:

abstract class extends implements null strictfp true
assert const false import package super try
boolean continue final instanceof private switch void
break default finally int protected synchronized volatile
byte do float interface public this while
case double for long return throw  
catch else goto native short throws  
char enum if new static transient  

Java Certification Path - Class Access - Default Access

November 18th, 2006

A class with default access has no modifier preceding it in the declaration. The default access is a package-level access, because a class with default access can be seen only by classes within the same package. If a class has default access, a class in another package won’t be able to create a instance of that class, or even declare a variable or return type. The compiler will complain. For example:

package humanity;
class Person {}

package family;
import humanity.Person;
class Child extends Person {}

Try to compile this 2 sources. As you can see, they are in different packages, and the compilation will fail.

Java Certification Path

November 18th, 2006

After thinking a lot about, I take a decision: I will take some certification exams in the Java Language. My object is to take the entry lavel exam Sun Certified Java Programmer (SCJP), and after follow the path until the Sun Certified Enterprise Architecture (SCEA).
I will try to put some notes here, about what I learn while chasing this objectives.
The book Sun Certified Programmer & Developer for Java 2 Study Guide (Exam 310-035 & 310-027) will be my partner for the next months. I will try to get my Java Black Belt, while I too do some mock exams. Cross your fingers!

The web noise

November 17th, 2006

When I´m again starting a new mega-over-ambitious web project, I ask myself the same question: “What´s is the f&*%ing reason for doing a web oriented project, if this project should be a very tiny, nice desktop project”? Maybe because doing it in a web way is sexy, a tech fashion. Maybe I´m starting to become a old guy with old ideas, but when we stop to ask reasons and start to do things using “best practices” or “experts recommendations” without thinking about, something is very wrong.
Some great pros of thin clients are low deployment costs, low hardware requirements, distributed and parallel platform almost for free.
Some cons of web solutions: the everyday fight against the HTTP stateless protocol, the browsers lack of standards.
Some pros of rich clients: rich user experience (you are in control of every pixel on the screen).
But there are great means to low the deployment cost of desktop applications. The Java Web Start Technology is under used, but a great to glue all together.
The nature give the most simple answer which works, and in development isn´t different. If the desktop development was the first to appear, it was because
it is the most simple solution which works. The tendency to create only web applications today is increasing the projects costs,
and helping its younger death.
When starting a new project, think about. What your opinion?

Great RCP toolkits for Java: Netbeans and Eclipse.

Great Web applications for Java which FAKE a desktop: WingS and Echo2.

Web Standards X The Browser Nightmare

November 14th, 2006

Why we yet suffer with browsers which doesn´t support web standards?
There are standards, and the responsible organization is the W3C .
If your browser doesn´t follow web standards, complain with your browser supplier, don´t curse the poor web designer, who did a good work learning, researching, to make a modern web site. Welcome to the 21st century. There a list of browsers to you make a upgrade.
If you are starting a new web project, and your manager or client aren´t aware of standards, caution! Stop everything, explain the reasons, there is a lot of them around.

I already worked in very poor standarlized projects. I konw what I´m saying.

My 2 cents.

Books Worth Reading

November 12th, 2006

Here are some books I’ve read that I’m recommending. I choose these books because all of them talk about computer science, software modeling or software engeneering, and all software developers can benefit from each of these books, whatever your programming language is.

So here, in no particular order…

This book have a lot of “Solved Problems” to add to your personal “Mental Library”.

Analysis Patterns: Reusable Object Models (The Addison-Wesley Object Technology Series)

Welcome

November 1st, 2006

Edsger Dijkstra once wrote:

To my reader.
These notes have the status of Letters written to myself. I wrote them down because, without doing so, I found myself repeating the same arguments over and over again. When reading what I had written, I was not always too satisfied.
Notes on Structured Programming

I don t want to compare myself to this genius who wrote much of the today computer science. But I hope to give to this blog the same meaning, the same soul.

Welcome all readers.