answersLogoWhite

0

What is a variable in the Java?

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

A variable is something that can store data, such as numbers and words. One of the common types of variables is called "int", which can store numbers.

Creating a variable is simple:

int myVar;

"int" is the data type of the variable, and "myVar" is the name of the variable, you can choose almost any name you want for your variables.

Then you can assign a number to this variable, you can even use negative numbers:

myVar = 5;

Notice how you do not need to type "int" again, you only need to do it once when you create the variable.

Here's how to add numbers to variables:

myVar = myVar + 4;

OR

myVar += 4;

When you do either of these it will add 4 to the value of myVar, which means myVar now equals 9. (5 + 4 = 9)

You can also use subtraction: -

Multiplication: *

Division: /

and Modulus: %

Another imortant data type is the String, a String can store words and letters, and behaves much like an int.

String myVar;

myVar = "Hello";

myVar += " there, Sir.";

Now, like we did with the int earlier, myVar equals "Hello there, Sir."

One last thing, you can add different variables together, but they must be the same data type:

myVar += anotherVar;

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

A local variable is a variable (of any primitive type: double, boolean, char, etc.) which only exists within a method. That is, it is created and used within a particular method and cannot be directly accessed by other methods in the same class or in different classes.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A variable is any object where the logical representation (the object's value) may be changed. A constant is the opposite of a variable; an object where the logical representation cannot be modified. Note that the logical representation is not the same as the underlying representation. The underlying representation may include mutable members which may be modified regardless of an object's "constness". The logical representation is typically used to logically compare objects with one another; mutable members should not participate in the logical representation.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A variable that is used as a parameter. If, for example, a method "methodA" calls a method "methodB", it may be desirable to pass information from methodA to methodB. This is done via parameters. In the calling program ("methodA"), and assuming you want to pass two integers, you might write something like:

methodB(5, 7);

//or with variables: methodB(a, b);

In methodB, the variables must be received:

methodB(int x, int y)

{

...

}

In this example, the number 5 (or the contents of variable "a") is copied to variable x, and can be used within the method body. Similarly, the second value (7, or b) is copied to variable "y", and can be used within methodB.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In most programming languages, including Java, an array is a single variable name that refers to a list of values. In Java, elements are counted starting at 0. Thus, if an array called myArray has 10 elements, the individual elements will be called myArray[0], myArray[1], etc., up to myArray[9].

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

variable are declared,scoped and initialized in the java programming language.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a variable in the Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is native variable in java?

native is a key word used in java method. there is no variable as native in java


How you make shared and synchronized variable in java thread?

A Variable that is shared as well as synchronized cannot be created in Java. These two terms are mutually exclusive and a variable that is synchronized in java cannot be shared and vice versa


What is the syntax of global variable in java?

There's no global variables in Java.


What is a variable type in Java?

int


Can you define variables in interface in java?

yes we can define a variable in an interface in java.


What is the code required to convert an integer variable to a string variable in Java?

There are several different methods to convert an integer variable to a string variable in Java. For example, one can use the following code to convert an integer variable to a string variable: Integer.toString(number)


How do you equate a php variable to a javascript variable?

Ideal thing would be to retrieve the value from PHP using AJAX and then assigning it to a java script variable. Thereafter compare it to the java script variable that is already present.


Can a java program has two different variable?

Yes. You can have as many variables as you want in Java


What is instance variable in java?

That refers to a variable attached to an object - also known as a field.


What is the meaning of a method in Java programming?

a method is a variable


How do you increase a variable by 5 in Java?

variable_name=variable_name+5;


Variable lnitialization in java language?

I suppose you want to ask about variable initialization.Java initialize its variables in its constructor.