Dec 20, 2007

Would You Like Some Variables As Well?

On a quick note beforehand... how did you find the looping post? I may end up redoing it for you guys if you didn't "grok in fullness" because I may not have done it justice, the <> statements blogger really doesn't like and I had to rewrite the whole thing a couple times. Am searching for another method to show proper form currently (but for now examples won't be tabbed in).

Alright so we have used variables already but they weren't really explained in any sort of detail. Variables are a sort of container but they are picky containers. Tupperware with attitude. If you try to put something in it it doesn't like it spits it back out with a nasty error message. To declare a variable you must say what type it is and give it a name. To use a variable it must be initialized. So here is an example of a statement declaring a variable:

int x = 0;

The int is the type of variable x is.
x is the name of our variable.
= 0 initializes our variable.

We can also declare a variable and initialize it in two steps:

int x;
x = 0;

Now for our purposes at the moment our variables are going to go inside our main() method. This is for reasons that will be explained in upcoming lessons.

Now you aren't just going to want to use integers. You are going to want to use many different types of variables. Here is a list of the variables for you:

int, these are integers and can store positive and negative nondecimal numbers.
boolean, these are true and false operators and must be set to true or false.
byte, can hold values from -127 to 128 according to this site.
double, can hold decimal values.
long, can hold really long nondecimal numbers, kind of like a super integer.
char, can store characters like "j" or "a".
String, can hold a bunch of characters strung together, like "hello there" or "ass22d&&^(*".

There we are, toy with those at will, we'll get them out of the main() method next time.

5 comments:

Mal said...

The nice thing about variables in Java is that they are quite similar to VB and C++ in declarations, storage, and use. It's mainly just the syntax that varies but of course syntax varies in any language in the world...English, French, Chinese,...etc. Great job describing them as "containers" by the way. This is an excellend word to use when decribing to a first time user. Then again Chris isn't exactly a first-time user as after all those years of pre-cal the word "variables" should be implanted in his head. Great job.

Mr. Kuropatwa said...

A suggestion and a comment:

A Suggestion (two actually):

Not sure why you're having trouble using the (pre) tag to display preformatted text. It should work. If you make me "admin" on the blog I can get in there and take a look at it for you. Your call, I'm good whatever you decide to do. ;-)

Try using http://scribd.com to upload docs (Word or what have you) and embed them on the blog with the original formating preserved. The advantage to using the (pre) is one less step to getting your content published. The advantage to using scribd.com is you have complete control over how your content is displayed and it can be ported to any site online embedding the code scribd.com generates for you.

A Comment:
You're making excellent use of labels on the blog!! As the content here grows it will be really easy for visitors to find the specific content they will come here looking for.

Look out Kathy Sierra, GreyM's a bloggin'! ;-)

Andy said...

Just got pointed here by a friend, and it looks like you're doing a great job. One thing I'd point out is that there's a difference (in Java, anyway) between basic variable types and more complex types (classes, really).

The differences aren't too important at the moment, I suppose, but they will become important sooner or later. Just something to keep in mind. (For example, there's a significant difference between the int type and the Integer type.)

Andy

Andy said...

(Sidenote and being a bit picky: Would you mind changing your settings to allow OpenID-authenticated posts as well? This post from the Blogger blog explains how. Thanks!)

Grey-M said...

Well hello all of you!

Alright so Mr. Malandrakis thank you for your great input!
Mr.K I have no real clue why the pre tag was messing up so badly, every time I previewed my post the tags got messed up real bad. You can have admin access for sure for future problems but the post is good for now. Will definitely investigate Scribd for future posts.

Andy I believe I have enabled the OpenID shindig. Thanks for the reminder about the differences with between the Integer and int, I never really got too deep into that section. Believe it has a lot to do with wrappers and casting but I definitely have to read my books and sites again on that.