Home Java Beginners Java - Variable, Constant and Literal in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java - Variable, Constant and Literal in Java

Advertisement
In this example you will see that how you can use variables, constants and literals in your program easily.

Java - Variable, Constant and Literal in Java

     

In this example you will see that how you can use variables, constants and literals in your program easily.

Variable : You can assign the values to the variable once it has been declared. The values of the variable can be changed anywhere in the program if the variable is accessible in that scope. In this example we have used the variable  intvariable to illustrate this.

Constants: Constants are declared using the final keyword. The values of the constant can't be changed once its declared.

Literal : literal is an explicit number or string constant used in Java programs. This specifies the syntax of your declaration of different types of values and operations. That mean literal is totally based on the syntax. Whenever you want to show the message with java special symbols then you have to use the literals. For example to show the message "Directory of this file : c:\code\varconstltr.java" then you can write as shown below

System.out.println("Directory of this file : c:\\code\\varconstltr.java");

There are three type of literals : Numeric Literals, Char type Literals, String Literals as follows.

Code of the Program : 

public class varconstltr{
  public static final int constint=5;
  public static void main(String[] args){
  int intvariable;
    for (int 0;i <= 10;i++){
  intvariable = i;
  System.out.println("All the values are : \n intvariable = " 

+ intvariable + "\nconstint = " + constint);
  }
  System.out.println("Directory of this file : c:\\code\\varconstltr.java");
  }
}

Download Variable,Constant and Literals Example

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Ask Questions?    Discuss: Java - Variable, Constant and Literal in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Garrett
June 11, 2011
Constants in JavaScript

I'm having trouble understanding constants and variables. I'm seeking a career as a web designer and I'm just starting in on learning JavaScript. Can you give me an example of what variable codes and constant codes look like and what they are used for on websites such as Yahoo and Google?
vijay
November 3, 2011
feedback

wow...fantastic and very nice...
DMCA.com