Declare Java variables
Overview
In this tutorial, we will learn to declare Java variables in Eclipse IDE. Two important points to remember is that:
- Java is statically-typed language.
- Java is case sensitive.
What is statically-typed Language?
Statically typed language expects the variables to be declared before using them. In Java we need to declare the variables before using them in the Java programs. Any undeclared variables would result in compilation error
<Undeclared_Variable> cannot be resolved to a variable
Syntax
The syntax for declaring a variable is:
dataType variable_name; //declaring variable
In the preceding syntax, dataType specifies a Jave type, such as int, char, double, etc. Variable can have any name that follows some specified naming conventions.
Declare Java Variables
For example to declare String variable we can use the following line of code.
String name;
Declare an integer variable i.
int i;
Declare a double variable called price.
double price;
Initialize Variable
We need to initialize the variable and assign a value. The most common way is to initialize the variable along with the declaration statement.
Examples
String name=”Learn java”;
int i=0;
double price=12.5;
Common mistakes
We get this error when we try to run the program without initializing the variable.
The local variable name may not have been initialized
Java is case sensitive language. So, String and string are not equal. We get error if we try to declare
string name=”Learn Java”;
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :