I had a bit of a cold this weekend, so didn’t leave the house much. In fact I didn’t leave my bed much. To pass the time I decided to learn Java. As you do.
First stop was netbeans.org to download the IDE. I could have just started writing the code in vi but I wanted to see if these IDE’s are as cool as people tell me. After running through the quickstart guide I see some of why people rave about IDE’s. You can rename a class and its updated everywhere! You can move classes around and their packages get updated! This probably sounds very obvious to many of you. But I’ve been writing in php and ksh for the last while and neither have a netbeans. With php you have vi and var_dump() as your debugger.
Second stop was to topcoder.com. Topcoder is basically a code competiton site. However all the previous competitons are archived and you can view and submit code for the problems. So I took a stab at one. And hours later I managed to get code that passed all the tests. It was a simple test, that I got right on nearly my first attempt. In theory. In practice I got a lesson in number types in Java.
1/2=0.5 right?
What I was doing was:
int a=1;
int b=2;
float c=0;
c=a/b;
System.out.println(“c: ” + c);
And got c: 0.0 !
It turns out that if you divide an integer by an integer in Java, you get an integer, regardless of what type you are assigning the answer to. The integer then appears to be typecast into whatever you declared. This confused me at first but it makes some sense. If you declare integers Java assumes that you want to work in integers. A lot of integer arithmetic would get slow, and possibly wrong, if it was converting to floats. This way you are sure that integer arithmetic works.
The solution to my problem. Declare a and b as floats or doubles, then you get the right answer.
Hopefully this will help someone from falling into the same issue!
Technorati Tag: Java
成人电影
成人小电影
免费成人电影
免费观看成人电影
日本成人电影
黄色电影
黄色小电影
免费黄色电影
免费观看黄色电影
日本黄色电影
激情电影
激情小电影
免费激情电影
免费观看激情电影
日本激情电影
色情电影
色情小电影
免费色情电影
免费观看色情电影
日本色情电影
性爱电影
性爱小电影
免费性爱电影
免费观看性爱电影
日本性爱电影