Skip to main content
OCA Java Programmer 1Z0-803

JAVA Declare and Initialize Variables

By November 2, 2013September 12th, 2022No Comments

For this objective of the Oracle Java Certified Associate Exam (OCA) 1ZO-803 we look at how we can declare and initialize variables. Java is strictly typed so we have to declare the variable’s data type. In this way memory is set aside as needed for the variable’s data. We have created two videos fro you to help you with your studies so make sure you take note of that and watch both. TheUrbanPenguin is very keen for you to gain your Java certification so please keep up your efforts in the study and you will be rewarded in the long run. This is not a short journey but worth every ounce of effort that you add.

Class, Instance and Local Variables

Variables defines at the class level and not within any other code block such as methods and constructors are defines as Class level Variables. If we create and Instance of the class then these become Instance Variables. Class and Instance variables do not need to be initialized numbers will have a value of 0 or 0.0 and char or String values will be “”.

public class MyClass {
  int cost;
  public static void main(String[] args){
    a = cost + 10;

The variable cost above can be left initialized and will have a value of 0

Local variables are defined within methods and are local to that method. These variables have to be initialized as they do not have default values.

public static void main(String[] args){
  int cost;
  int a = cost + 10;

The above code will not work as cost has no value. Local variables do not default to 0.

public static void main(String[] args){
  int cost = 0;
  int a = cost + 10;

Now the code will work as cost has a value. We have two videos for this section take a look at the first were we look at uninitialized variables.

Data Types

Data types can be Primitive or Reference. Primitive of those data types native to Java and Reference types are those created form Object References. A variable in declared with its type and initialized when populated.

int z; //the variable z is declared as type integer
       //z will be be uninitialized if this is a local variable
       // z will equal 0 if it is a class variable
z = 100; //z is initialized with a value of 100
int z = 100; //z is declared and initialized on the one line


Whole Numbers

  • boolean
  • byte
  • short
  • int
  • long

These are all signed except boolean, so include negative values and range from 1 bit, (boolean), 8 bits byte, 16 bits (short) through to 64 bits (long).


  • float
  • double


  • char


int x = 23;
int y;
y =23;
float w;
w = 1.4f;
double z = 1.4;
int x = 0xFF; //initialized with a hex value
int x = 0b11111111; initialized with a binary value


Reference types make a reference to an object class. We commonly use String which is an the class: java.lang.String

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.

String name = "Andrew";

Here we set up a variable called name that is based on the String class.

If we create object instances then they too are Reference types.

Account AC1 = new Account;

Here we declare and initialize the variable Ac1 as an instance of the Account class.

I hope this all makes sense to you and we see you back soon.