SUBJECT MATERIAL, EBOOKS, IMPORTANT QUESTION , ASSIGNMENT QUESTION, OLD QUESTION PAPER

Monday 7 August 2017

Variables in java

Explaination: A variable can be thought of as a container which holds value for you during the life of your program. Every variable i... thumbnail 1 summary
Explaination:

  • A variable can be thought of as a container which holds value for you during the life of your program.
  • Every variable is assigned a data type which designates the type and quantity of a value it can hold.
  • A Java variable is a piece of memory that can contain a data value. 
  • A variable thus has a data type. 
  • Data types are covered in more detail in the text on Java data types.
  • Variables are typically used to store information which your Java program needs to do its job. 
  • This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.
Java Variable Types

In Java there are four types of variables:

  • Non-static fields (Instance Variable)
    • A non-static field is a variable that belongs to an object. 
    • Objects keep their internal state in non-static fields. 
    • Non-static fields are also called instance variables, because they belong to instances (objects) of a class. 
    • Non-static fields are covered in more detail in the text on Java fields.
    • Syntex:
      • datatype  variablename = variable value>;      //anywere in program
    • Example:
      • int m=100;  //instance variable  
  • Static fields
    • A static field is a variable that belongs to a class.
    • A static field has the same value for all objects that access it. 
    • Static fields are also called class variables. 
    • Static fields are also covered in more detail in the text on Java fields.
    • Syntex:
      • static datatype  variablename = <variable value>;      //static variable  
    • Example:
      • static int m=100;//static variable  
    • Local variables
      • A local variable is a variable declared inside a method. 
      • A local variable is only accessible inside the method that declared it. 
      • Local variables are covered in more detail in the text on Java methods.
      • Syntex:
        • datatype  variablename = variable value>;      //inside function
      • Example:
        • int m=100;  //local variable  
    • Parameters
      • A parameter is a variable that is passed to a method when the method is called. 
      • Parameters are also only accessible inside the method that declares them, although a value is assigned to them when the method is called. 
      • Parameters are also covered in more detail in the text on Java methods.
    Java Variable Naming Conventions
    • There are a few rules and conventions related to the naming of variables.
    • Java variable names are case sensitive. The variable name money is not the same as Money or MONEY.
    • Java variable names must start with a letter, or the $ or _ character.
    • After the first character in a Java variable name, the name can also contain numbers (in addition to letters, the $, and the _ character).
    • Variable names cannot be equal to reserved key words in Java. For instance, the words int or for are reserved words in Java. Therefore you cannot name your variables int or for.
    • Variable names are written in lowercase. For instance, variable or apple.
    • If variable names consist of multiple words, each word after the first word has its first letter written in uppercase. For instance, variableName or bigApple.
    • Even though it is allowed, you do not normally start a Java variable name with $ or _ .
    • Static final fields (constants) are named in all uppercase, typically using an _ to separate the words in the name. For instance EXCHANGE_RATE or COEFFICIENT.