-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|687|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Blogs Home -> Blogs


 
Jayenkai
Created : 09 April 2008
Edited : 09 April 2008

Pulling my hair out...



Java
J2me, to be precise.
Working away, making my little Puzzobomb-Java-edition.
Everything's working nicely.

Today I decided to add Level Loading. What does the game need? It needs a String.

So, I first decide to use a Char array, just like I would in C.
char* LevDat="Level data goes here"

Uhoh. Java doesn't like that!

So, I make LevDat a 1024 sized array, and go about figuring out how to dump the data into it.
And spend about an hour, before going "Oh, forget it then! I'll use a string!!!"

public int PlayerX;
public int PlayerY;
public string LevDat;

^ String didn't highlight.
It's at this point that I started pulling my hair out.
..

...
About half an hour later, I realised it WAS recognising "string", it just didn't bother to highlight it.

Stupid sodding Java..


 

Comments


Wednesday, 09 April 2008, 05:36
JL235
Java is not C, a char array is defined as 'char[]' not 'char *'. Besides 'char *' isn't an array it's a char pointer (which yes can be used for traversing arrays, but it's still not the same thing and Java doesn't have pointers!). Incase your wondering, no you don't have to define the size the array before hand. 'char[] level = getLevelData()' is perfectly ok.

Also it's String not string, and no it shouldn't highlight since it's a Class name (String is a full 100% class with a few tricks that other classes don't) and not a primitive data type. Completely different things.
Wednesday, 09 April 2008, 05:45
Jayenkai
"is defined as 'char[]' not 'char *'" <-- yeah, thanks! I noticed...!

And, for the purposes of "Spinal's phone's really tweaky, memory wise" I am allocating the array memory sizes I'm using, so that I can be sure it'll actually work on his phone!
Best to be safe, than have the phone freak out 10 mins into the game!

BTW, String makes perfect sense, doesn't it.. You have int, char, byte, float.. why not have String be the odd one out, with it's own super special little capitalisation!

I'm getting there, but like you said, Java isn't C.
It's got all of C's annoying annoyances, and then a whole host of new ones too!!
Wednesday, 09 April 2008, 06:06
JL235
String has methods, it's a full and true class. All class names start with a capital letter, so yes it makes perfect sense that it's like that. "hello world" is an Object not a primitive data type.

Also I'd expect your gonna fall into the "hello" == "hello" trap. Don't use == for String comparisons because that is a reference check not a String comparison. However Java will intern all constant Strings so that you have 1 hello constant rather then 2 or more. So "hello" == "hello" will actually return true since they are the same instance. But "hello" == "he" + "llo" will (or at least should) return false (it's possible to make it return true too).

For String comparisons use the 'equals()' method.

Java also interns all interger Objects between -127 and 128, and _might_ do it for integers outside that range, but that is entirely down to the compiler.

Plus take a look at this from Java Puzzlers:

and

The only difference is += instead of +. So think they are exactly the same? Ha! The first compiles and runs, the second doesn't. Java is awesome, Enjoy!
Wednesday, 09 April 2008, 06:12
Jayenkai
I won't be printing, comparing, or anything fancy like that!
I'm only using it to get a LevelData string, and convert it into an actual level.
Nice and simple "check each letter" stuff, hence why I was originally just going to use the char array.

And, just so that I'm not only being anti-Java, this game's a complete git to code with cursor controls as opposed to draggy/mouse/touchscreen stuff! You have to move blocks, and the cursor, both at the same time! WTF!?!

Who'd have thought a simple puzzle game would be such an annoying game to make.

Centipong was much easier!
Wednesday, 09 April 2008, 06:33
JL235
To be perfectly honest, since you always write in Blitz or C I wouldn't be surprised if you structuring it badly. That might be making it more complicated then it needs to be.

If well structured you can often write less code then if your were making it in say Blitz. Simply because there are more ways to reduce code reuse and because you can do more things and so code can be far more expressive.

Plus with a good IDE (like NetBeans 6) you can usually generate lots of the trivial stuff (like accessor and mutator methods) and it aids with navigation and handling your code. Like you can right-click on any method or class and just 'go to source'. With larger projects it really does help to make you more productive.
Wednesday, 09 April 2008, 06:38
mike_g
In java when the first character is captialised it means that its an object, which is handy to identify what is what. Jay, are you using netbeans? If not I'd recommend you get it and install the javadocs. Seriously you can figure out how to do everything through the code completion feature. The statndard sting class has functions to do just about most you could want with strings.

Personally I think Java is great, give it some time Jay and I'm sure you will agree
Wednesday, 09 April 2008, 07:21
Jayenkai
Yeah, Netbeans is useful I guess.
That autocompletion popup gets annoying when you're trying to just type stuff in, and the whole "deleting tabs" is just stupid, but other than that I could get used to it.

Still happy with Notepad for most stuff though! Who needs extras!? Just type!


Oh, and I can't for the life of me figure out how to get a Quick-help for a command. I'm sure it's do-able, but every time I hit F1, expecting a Blitz-like quicky thing in the status bar, it instead spends 3 weeks loading up the big-ass help file..
And then going to the index, and so not telling me anything useful.
Wednesday, 09 April 2008, 07:35
JL235
The code completion has a pop-up box with it which fills with the JavaDoc for the method or class you are currently looking at. It's useful to type say 'window.get' and then have all the get methods listen and just go through them one by one.

For quick tid bits of information, I completely agree with Mike_G. However I keep a local copy of all 100mb of the Java API on my PC for quick lookup. Very useful when having say FireFox with the API on my small monitor and NetBeans open on the main one.
Wednesday, 09 April 2008, 13:54
Scherererer
mike_g In java when the first character is captialised it means that its an object, which is handy to identify what is what


actually its a class, not an object. An object is an instance of a class:

in that scenario, String is a class, s is an object, and technically "BLAH" is an object, but being written as a string literal.
Wednesday, 09 April 2008, 15:07
mike_g
Yeah, but thats just nitpicking. What I meant was pretty obvious.
Wednesday, 09 April 2008, 16:28
JL235
Actually Instinct (nitpicking further), String is an Object too. It's an instance of the Class class (although you can only access it as an object with .class).
Wednesday, 09 April 2008, 16:47
Scherererer
Okay JL, now you're just pulling my leg! String is a class! I want to see some proof and documentation.
Wednesday, 09 April 2008, 17:57
JL235
Easy:


Here is the String class object viewed using the BlueJ Object Inspector. Notice at the top it says String (the name I've given it) : Class<String> (it's type).

You should also bear in mind that all classes which use generics are infact the exact same class. So Class<String>.class is the same class as Class.class. This is because generics simply adds type erasure through the use of cheap casts and autoboxing at run-time (cheaper casts then normal because the compiler can guarantee that the type is correct).
Wednesday, 09 April 2008, 20:07
Scherererer
Are you sure its not saying String : Class<String> as in String is inherited from Class<String>? Also, generics weren't introduced until Java 5, so how would String which has been implamented since the get-go be a product of using generics? I think you're confusing the notation.

I know how Java generics work (actually the one thing about java that I like more than C# where a generic and a regular class are two separate things, even when named the same).

JavaDoc String - Object - Class
Wednesday, 09 April 2008, 20:23
JL235
First, no 'String : Class<String>' does not mean anything to do with inheritence. That's not how the BlueJ Object Inspector works. Perhaps that was a bad example, but in BlueJ you can imput code into a 'code pad' which will return an 'object reference' to whatever you've made. In this case I inputted 'String.class.getClass()' and when you get that 'object reference' you can name it, in this case I called it 'String'. The 'Class<String>' IS the type of the Object I was poiting to. Feel free to download BlueJ for free from the official site and try it yourself.

Yes generics were added in Java 5, so how can Class be using it then? They change the API, that's how. ArrayList was added in version 1.2, that uses generics. Comparator too. They updated the API, that's all.

Anywho, yes classes are Objects. You can even pass them around and make instances from them on the fly. I'm not wrong, in many cases I've done it myself. I made a data structure for my RTS which stored the units in a map of classes to lists where each list held instances of that class. It even worked out which classes were subclasses of which so I could 'getAllSubclassesOf(Infantry.class)' which will return a list containing instances of Infantry, Medic, Grenadier and so on. They are Objects too!


Thursday, 10 April 2008, 03:34
Jayenkai
*blink*
You know, I had my game pretty much up and running about 30 mins after my first post..
You can all give up with symantics if you'd like!