Variables and Literals

Variables:

The variable in java is a container that holds data value while your program executes.It is the basic unit of storage in a java program.

OR, Variables are used for storing values used in a program.


Declaring variable:

data_type variable_name=value;
Eg; int age=30;


There are three types of variables in java:

1. Local variables

2. Instance variable

3. Static variable


1.Local variables: A variable which is declared inside the body of the method is known as a local variable.


Note: Local variables have a limited scope, which means they're only accessible within the curly braces { } of the function (method) where they're created.Only those in the function know about the local variable.And the other methods in the class aren’t even aware that the variable exists.


2.Instance Variable: Instance variable is a variable that is declared within a class but outside of any method, constructor, or block.Each object created from the class has its own copy of Instance variables, and their values can vary from one object to another.Instance variables are sometimes also called member variables or fields. It is not declared as static.


Note: An instance variable is a special memory slot that belongs to each individual object of a class. It holds information specific to that object, like its name, size, or current state.


3. Static variable: Static refers to something that is stationary, fixed, or unchanging.In Java, a static variable is the opposite of an instance variable.


Memory allocation: Memory allocation for static variables happens only once when the class is loaded into memory and persists as long as the class is loaded, even if there are no objects of that class currently created.


Declaration: Instance variables are declared outside any method but within the class. Static variables are declared with the static keyword.


Accessibility: Static variables can be accessed directly using the class name, or from any instance (object) of the class.

//A Java program to demonstrate local ,instance and static variable

public class ApnaStudy{
    static int StaticVar=100; //static variables
    void method(){ //Llocal_var can't accessible outside the class.we can access it only inside it's class
    int Localvar=20; //local variable
    }
    public static void main(String[] args) {
    int InstanceVar=30; //declaration of instance variable

    System.out.println(StaticVar);
    // System.out.println(Localvar);//throws an error
    System.out.println(InstanceVar);

    System.out.println(StaticVar+InstanceVar);

    }
}

Key Points:

  • Static variables are good for constant values or data shared across all objects.
  • Local variables are temporary and useful for specific calculations within a method.
  • Instance variables store information unique to each object

Rules for declaring a variable name:

  • First letter of the variable name must not begin with a digit. Ex: int 1_arr is wrong approach
  • Variable names are case sensitive.
    Ex: int Ram and Int ram both are considered as different
  • Names should not be started with keywords.
    Ex: int voidAdd is a wrong approach
  • White spaces are not allowed.
    Ex: int Apna College (invalid)
    int ApnaCollege (valid)
  • Variable names can contain alphabets , dollar signs ($) , underscores (_),and logics if all the above conditions are met.
  • An identifier must not be longer than 65,535 characters.

Also, there are certain styles that programmers widely used in naming variables, classes and methods in Java. Here are some of them.


1. Use lowercase letters for the first character of variables’ and methods’ names.

2. Use uppercase letters for the first character of class names. Use meaningful names.

3. Do not use underscores to separate words.

4. Use uppercase letters for all characters in a constant.

5. Use underscore to separate words.

6. Apart from the mentioned cases, always use lowercase letters. Use verbs for methods’ names.

//A Program to demonstrate different types of variables

public class ApnaStudy {
    public static void main(String[] args) {
    int a=30; //here a and b are identifiers or variables
    int b=40;
    System.out.println(a+b);
    b=70; //we can change the value of any variable later in the program
    System.out.println(a+b);
    //declaration of some other types of variables    
   //Integer Type Variables
    int myNum = 5;
    System.out.println(myNum);    
    //Float
    float myFloatNum = 5.99f;
    System.out.println(myFloatNum);    
    //Character
    char myLetter = 'D';
    System.out.println(myLetter);    
    //Boolean
    boolean myBool = true;
    System.out.println(myBool);    
    //String
    String myText = "Hello";
    System.out.println(myText);
}
}


/*OUTPUT*/

70
100
5
5.99
D
true
Hello

Java Literals

A constant value in java is created by using a literal representation of it.
Example: 100,23.5, ‘X’

1. Integer Literals:

  • Represent whole numbers, both positive and negative.
  • These are all decimal values,meaning they are describing a base 10 number.
  • There are other two bases which can be used in integer literal, Octal(base 8) and hexadecimal(base 16).
  • Examples: 10, -25, 0, 42

Java Literals

A constant value in java is created by using a literal representation of it.
Example: 100,23.5, ‘X’

2. Floating-Point Literals:

  • Represent numbers with decimal points, for real numbers.
  • Can be written in decimal form (e.g., 3.14159) or scientific notation (e.g., 1.23e-5).
  • By default, Java considers floating-point literals as doubles.
  • You can explicitly specify a float by adding an 'f' or 'F' at the end (e.g., 3.14159f).
  • You can explicitly specify a Double by adding an 'd' or 'D' at the end (e.g., 3.14159d).(Not necessary to specify it,Because it is explicitly defined in java as mentioned above).

Java Literals

A constant value in java is created by using a literal representation of it.
Example: 100,23.5, ‘X’

3. Character Literals:

  • Represent a single character enclosed within single quotes (').
  • Characters in java are indices into a unicode character set.
  • Examples: 'a', '#', '\n' (newline character)

ASCII Values:American Standard Code For Information Interchange.

Like other character encodings, ASCII specifies a correspondence between digital bit patterns and character symbols (i.e. graphemes and control characters). This allows digital devices to communicate with each other.

4. String Literals:

  • Represent a sequence of characters enclosed within double quotes (").
  • Can include letters, numbers, symbols, and even special characters using escape sequences (e.g., \" for a double quote within a string).
  • One important thing to note about java strings is that they must begin and end on the same line.
  • There is no line-continuation escape sequence as there is in other languages.
  • Examples: "Hello, World!", "This is a string"

5. Boolean Literals:

  • Represent true or false values.
  • Used for logical conditions.
  • Example: true, false

6. Null Literal:

  • Represents a special value that indicates the absence of an object reference.
  • Used to assign a variable to "nothing" or indicate no object is referenced.
  • Example: null
Previous Next