开发者

JAVA Multiple Operations when defining a variable

开发者 https://www.devze.com 2023-04-06 05:05 出处:网络
In Java is it possible to have multiple operations on a variable definition? For example double myLocalVar开发者_开发知识库iable = (myLocalVar2 + myLocalVar3 * myLocalVar4);

In Java is it possible to have multiple operations on a variable definition?

For example

double myLocalVar开发者_开发知识库iable = (myLocalVar2 + myLocalVar3 * myLocalVar4);

Thanks for the help!

CJ


Yes, and you can even perform multiple assignments inline:

double myLocalVariable1, myLocalVariable2 = (myLocalVar2 + myLocalVar3 * myLocalVar4);

or if they where previously created variables:

double x;
double y = x = 5 * 4 + 1;


Yes, this is very much possible in Java. Of course, you need to take care of evaluation precedence to get the desired/correct result.


Yes, it is possible. The result of the right hand side expression is used to initialize myLocalVariable.


Yes. You can. There are 3 methods to initialize a instance member.

/** Simple instance member initialization */
public double myLocalVariable = 10.234;

/** Method call instance member initialization */
public double myLocalVariable = <method you want to call> // e.g. ClassA.getNumber()

/** Complex instance member initialization */
private double myLocalVariable;
{
   myLocalVariable = (myLocalVar2 + myLocalVar3 * myLocalVar4);
   .....
   .....
   ..... 
   //even more complex operation you want to do
}

Certainly yours is also ok which is a simple instance member initialization.

Hope this help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消