Copyright

Declaring Variables in Java

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

You'll learn how to declare variables of various types in Java, naming conventions for variables and how declarations can be used to initialize the variable's value.

Declaring Your Intentions

Whether it be personal or business relationships, most people are more comfortable if you declare your intentions beforehand. When coding Java programs, you MUST declare what type of information a variable represents before you can use it. Java variables provide named storage that our programs can manipulate. Java supports the following predefined data types, which are known as primitive data types:


Type Size Contains Default Value
boolean 1-bit true or false false
char 16-bit Unicode character \u0000
byte 8-bit Signed integer 0
short 16-bit Signed integer 0
int 32-bit Signed integer 0
long 64-bit Signed integer 0
float 32-bit IEEE 754 floating point 0.0f
double 64-bit IEEE 754 floating point 0.0d


Declaring Primitive Data Types

The basic form of a variable's declaration is:

datatype variable;

where 'datatype' is one of the primitive data types and 'variable' is the name the programmer will use to identify the variable. The default value associated with the specified data type will be stored in the variable. The programmer can assign any initial value desired by following the variable's name with an '=' and any valid Java expression that reduces to a single value:

datatype variable = expression;

Here are some examples:

// Assume default values.
int count;    // = 0;
boolean answer;    // = false
double x, y, z;    // = 0
// Declare and initialize
short myAgeInYears = 65;
double initialMPH = 78.6;
char firstLeter = 'A';
boolean myAnswer = true;
short age = myAgeInYears;
double speedMPH = initialMPH + 5.5;
double a = 3.0, b= 4.0, c = 5.0;
// Declare some constants
final double PI = 3.1415926;
final double TWO_PI = 2.0 * PI;
// Use constant in a declaration
double r = 7.5;
double circumference = TWO_PI * r;

By using commas as illustrated, multiple variables can be declared in a single Java statement. Using the 'final' keyword indicates the final value that the variable can take on, effectively making it a constant.

Declaring Object Reference Types

Primitive data types are built into the Java language so that their storage requirements and data organization are fixed and always known beforehand. However, Java objects are defined by class definitions, and their storage requirements and data organization are not fixed. An object must be constructed (using the 'new' operator) before it can be used. To access the object, a reference to it is stored in the declared variable, as opposed to a specific value, as is the case with primitive data types. When declaring a variable which will hold a reference to an object, one uses the class name instead of the primitive data type name. Here are some examples using a simple class named Rectangle:

// Define a simple Rectangle class
class Rectangle {
  // A rectangle state is determined by height + width
  private double height;
  private double width;
  // Construct from supplied parameters
  public Rectangle(double h, double w) {
   height = h;
   width = w;
  }
  // Computation methods
  public double Permieter() {
   return 2.0 * (height + width);
  }
  public double Area() {
   return height * width;
  }
}
/* Declare a variable named 'bigRect' which will
ultimately hold a reference to a Rectangle object
but doesn't initially. */
  Rectangle bigRect; // default is null
/* Declare and construct a specific Rectangle
object and store a reference to it in 'myRect'. */
  Rectangle myRect = new Rectangle(2.5, 3.5);

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support