Please register to have access to more features!

It is highly recommended that you have Javascript enabled; many features will not work unless you do. The recommended browsers are Firefox and Chrome; the board is also NOT mobile-friendly.

Please invite your friends!

If you were referred by someone (ex. me, Jessica) please put their username in the referral box on the registration page. Ask them if you don't know their username.

If you are visiting for TESTING PURPOSES ONLY, this is the test account information:

Password: test123

Select a forum to post in:

Search found 3 matches

Return to: Java

Re: Java


Primitives are the basic type of data. There are 4 integer primitives (byte, short, int, long), 2 decimal number primitives (float, double), boolean (true and false), and char (single characters). The most common are int for integers and double for decimal numbers.

primitives.png (36.01 KiB) Viewed 2840 times


Before you can use a variable, you have to declare it with a type. A primitive variable should only be declared once in a program. Therefore, variables are usually declared at the beginning of a class. For example: int number;

The above example sets up a variable to be called number. This number will be a primitive of type int. This means that number can only be whole numbers between:
-2,147,483,648 and 2,147,483,647. The above sample declares the variable called number but does not start it with any specific value.


You can declare a variable and initialize it (give it a value) at the same time. For example: int number = 15;
The above example sets up a variable to be called number and it will be a primitive of type int. This means that number can only be whole numbers between 2,147,483,648 and 2,147,483,647. However, the above statement goes one step further and sets the value of the variable to be 15.

You can declare multiple variable names of same type by separating with commas such as: int x, y, z;
The above example sets up 3 variables to be called x, y and z. All 3 variables are to be primitives of type int. None of the variables have been set to any values.

You can also declare multiple variable names of same type and initialize the variables in the same statement as follows: int age = 30, hours = 45, sum =0;
The above example sets up 3 variables called age, hours, and sum to all be primitive ints. It also declares each one to have a different value with age being set to 30, hours being set to 45 and sum being set to 0. The preferred method of declaring variables is to declare them on separate lines so that they may easily be commented as follows:
Code: Select all
int age = 30;       // age is the employee age
int hours = 45;   // hours is the employee weekly hours worked
int sum = 0;       // sum is the total of all hours worked

Values may be assigned to variables when they are declared or they may be assigned to variables as needed later in program. Numeric variables can be assigned values by setting them equal to a number. Most Java programmers make numeric variables either int (whole numbers without decimal point) or double (numbers with decimal points). These two primitive types are easily assigned values simply by setting them equal to an appropriate number such as:
Code: Select all
int   x = 12;             //  sets x to integer of 12.
double   z = 14.5;   // sets z to 14.5 which is a double
by Jessica
June 2nd, 2013, 11:05 am
Forum: ICT Topics
Topic: Java
Replies: 2
Views: 2,854

Re: Java


Data can be categorized as constant or variable. Data is constant when it can't be changed after the program compiles. Data is variable when it might change after the program compiles. In Algebra, we assign the letters x, n, and y to represent unknowns. In programming, we call these variables and assign these variables longer and more meaningful names. Variables are locations in memory in which values can be stored. Computer programs are generic instructions written to tell the computers how to accomplish a task such as make change for a customer or make paychecks for employees. The same computer instructions must be flexible to make change for various customers or print paychecks for a variety of employees. Since you will be using the same computer instructions to calculate everyone's paycheck, you would not refer to a person's gross pay as 400.57 because that would only be correct for one employee. Therefore, we must use variable names to describe the data instead of exact numbers. For instance, in a payroll program, you might refer to a person's gross pay as grossPay or gp or gross_pay. Variables have a type, name, and a value.

Naming Variables
When selecting a name for a Java variable, you must follow the following syntactical and stylistic rules:
  • Variable names consists of letters, digits, underscores (_) or $ (no spaces allowed).
  • The first character can't be a digit. (First character is generally lowercase letter.)
  • Variable names can be any length, but extremely short or long names are awkward.
  • A stylistic rule is to begin all variable names with a lowercase letter and following words have an initial uppercase letter such as theButton, grossPay, or myAge.
  • Another stylistic rule is that variable names should be meaningful. The variable name of h means very little while the variable name of regularHours says quite a bit.
  • Java is case sensitive so that number and Number and NUMBER are different variables to the computer.
  • Class names follow same rules as variable names except they must begin with capital letter.
  • You cannot use keywords as variables. List:
by Jessica
May 31st, 2013, 3:32 pm
Forum: ICT Topics
Topic: Java
Replies: 2
Views: 2,854



Comments are used to document code so that other people reading the code can understand the logic.

Code: Select all
// Single Line Comment

Code: Select all
/* Multi-Line
Comment */

Multi-Line Comment


Syntax errors are caused when the user writes code that is not understood by the compiler. A syntax error can be caused by incorrect capitalization or spelling mistakes. The compiler informs the user of a syntax error by displaying an error message. Typing “Public Class” instead of “public class” would result in a syntax error.

Run-time errors are caused by invalid data. Run-time errors do not effect thecompilation of your program thus the program will compile and execute, but it may crash or hang after execution. If you try to divide 12 by 0 you would get a run-time error because you cannot divide by 0.

Logic errors are caused by mistakes that do not defy the rules of the language and do not crash or hang the program, but instead yield incorrect results. The user may not understand the problem that the program is trying to solve and therefore uses the wrong equation, wrong strategy, etc. If you have an object move left instead of right, that is a logic error.
by Jessica
May 29th, 2013, 9:26 pm
Forum: ICT Topics
Topic: Java
Replies: 2
Views: 2,854

Return to: Java

Reputation System ©'