Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Variables

Variables hold information in Java just like in any other language.
If you haven't programmed before think of maths remember algebra x+3=5 find 'x'?
In this case x=5-3, x holds the number 2, in programing their are 2 differences:
1. A variable can be named anything it doesn't have to be 1 single letter.
2. A variable can hold anything it doesn't have to be a number.

Variable Naming

I mentioned variables can be named anything, their are still rules:

1. Variable names are case sensitive, usually we keep them lower case (standard practice).
2. Variable names can only use A-Z,a-z,0-9
3. A variable name cannot start with a number.
4. The variable name cannot be used by anything else eg: System is a java object and would conflict

Valid name examples:

Joe, blow, intI, i, x, y, z, myvariable, var1, var2, anythingReally

It helps if you make them meeningful, but they are still valid, at the end of this document I will show you standard naming practices which may please your boss / or teacher.

Primitive Variable Types

I stated a variable can hold anything, this is not totally true, a variable can hold anything according to its data type. When you declare a variable you give it a data type, this limits what information can be stored in that variable, and also effects the amount of memory your program will use because computers come with 4gb of RAM nowadays I will focus on what you can store in each type of variable.

Declaration Name Values Description
short srtNumber = 1; short Whole numbers:
-32,768 to 32,767
 
int intNumber = 1; integer Whole numbers:
-2,147,483,648 to 2,147,483,64
 
long intNumber=1; long Whole numbers:
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
 
boolean booWorks = true; boolean true or false  
char chrTest='b'; charector Unicode, 0 to 65,535 Note the use of a single quotation mark around the letter.
byte bytTest = 'b'; byte Covers values from -128 to 127.  
double dblNumber = 0.15; double

+ or -
4.94065645841246544e-324d to 1.79769313486231570e+308d

 
float dblNumber = 0.15f; float + or -
1.40129846432481707e-45 to 3.40282346638528860e+38
Note java works in doubles by default for fractions so you need 'f' on the end when setting.
String strHelloWorld="Hello World"; String Holds an array of charactors. Note String is not a primitive data type in Java but added here because its kinda important.

Practical

Updating Hello World:

Start a new project call it HelloWorld2 and add a class again named "HelloWorld2" and with add static main ticked :-) see tutorial3 for help

Code:
public class HelloWorld2 {


public static void main(String[] args) {

/* Expanding the HelloWorld Sample */

// Original Hello World Using a variable
String strHelloWorld="Hello World";
System.out.println(strHelloWorld);

// Joining multiple names and displaying
// try replacing with your names!
String strTitle = "Mr";
String strForeName = "Joe";
String strSurName = "Blow";
String strFullName = ""; //<-- set this automaticaly
// join names
strFullName = strTitle+" "+strForeName+" "+strSurName;
/* The '+' symbol joins strings and adds numbers! To join a number use ""+ more later */
// show result
System.out.println("Hello: "+strFullName);
}
}

Working with numbers:

Start a new project call it TimesTable1 and add a class again named "TimesTable1" and with add static main ticked :-)

Code:
public class TimesTable1 {


public static void main(String[] args) {

/* Times Tables Excersize */
int i=0; // 'I'ncrementer
int intTable=5;
int intAnswer;

System.out.println("Times table "+intTable+" up to 5.");
// 1
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 2
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 3
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 4
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
// 5
i=i+1;
intAnswer=intTable*i;
System.out.println(""+intTable+"*"+i+"="+intAnswer);
}

}

Note how repetitive this code is, I will be covering loops next which will make the code smaller.
""+intTable+"*"+i+"="+intAnswer, seems complex, just remember to put any text you want to print directly out in double quotation marks with plus at each end!, ""+ is used at the begining to tell java to treat it as a string and join instead of trying to add the numbers up!
Why not try the 12 times table?
[click for answer]

Why not start from 5 to 10?
[click for answer]

Naming good practices

Now you have covered variable types and used variables here are some basic good practice rules for naming variables:

1. Start each variable with a 3 lower case letter abbreviation of the variable type, this helps you know the type of datavariable is expecting:

boo = boolean
int = integer
sng = single
flo = float
lng = long
chr = char
str = string

2. Function names, Class names, and Variable names may sometimes conflict, remember I said "the variable name cannot be used by anything else" rule 4? To avoid this conflict:

Variable names remain lower case, with only the first letter of each subsequant word to be upper case eg: myVarName
Function names use a uppercase letter to begin with and each subsequant word eg: MyFunctionName
Class object names use all uppercase letters eg: MYCLASSNAME

This avoids conflicts.

3. Use meeningful names, with eclipses autocomplete loooooonnnnggg variables names are not a problem, always use more descriptive rather then shorter variable names, the only exception being 'i' since it is comon practice to use 'i' in for loops and everyone tends to ignore this rule.

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source