I am a little confused in declaring variable in android, I know two ways of doing it.
1. int EC, CC, PT, HC = 1;
2. int EC = 1, CC = 1, PT = 1, HC = 1;
I want to know the difference between 1 & 2.
Are both the ways same ?
This question is not really related to Android, it is only dependent on Java.
Anyway,
the first approach creates the variables EC, CC, PT and HC. HC gets the value 1.
In the second approach you create all the variables and give them the value 1.
Just do a search for Java and Variables if you want to find some information about the subject. Here are two links for you:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
http://www.roseindia.net/java/master-java/variables-in-java.shtml
On a side note, you should follow the Java naming conventions and keep your variables lowercase, or capitalize the first letter of any subsequent words in your variable name.
No, it's not the same.
In the first case, only HC will have the value 1 assigned. The other variables will be left unassigned.
If these are instance or static variables, that means they will get the default value 0. If they are local variables, you will need to assign them before you can use them.
only HC will be initialized
all variables will be initialized (with value 1)
Assuming instance variables, frst way, EC,CC,PT are ZERO. Second way all are ONE.
Instance variables are assigned to default values, if nothing assigned.
If you define first one as local variable (inside any method), you will get compilation error.
If you declare int EC,CC,PT,HC=1;
The values for EC,CC and PT are Zero(0) and HC value is 1,
If you decalre int EC=1,CC=1,PT=1,HC=1 means all EC,CC,PT and HC values are one.
In the first case, the first 3 variables are uninitialized, that means when object is created compiler will initialize those uninitialize variables with the default values hich is ZERO in case of integer type and the last variable will be initialized with ONE.
In the second case each variable will be initialized with ONE. You can also do it as follows
First declare all variables
int EC, CC, PT, HC;
Then assign value
EC =CC = PT = HC = 1;
The first example is like :
int EC;int CC;int PT;int HC = 1; // and only hc gets the value of 1
The second example is like :
int EC=1;int CC=1;int PT=1;int HC = 1; // all of the vars get the value of 1
In first case, except EC, all are uninitialized.
In second case, all are initialized.
Related
i create hash table in java but there is aproblem in it when adding elements in it.
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<sum2 , g>;
the error in second line where define sum2 is variable contain integer value and g variable contain string value. i dont know where the problem in putting elements in hash table.i want to add the values of this variables in hash table each time as the values change.
To mutate an object, you have to call its properties. An object property is either a field or a method. In your case put is a method of the hashT object. A method call is done by writing the object name, then the dot operator, then the method name and finally the arguments surrounded by parenthesis:
objectName.methodName(argument1, argument2, ...);
The problem is here:
hashT.put<sum2 , g>;
put is a method and to call it you have to surround the arguments (sum2 and g) with parenthesis:
hashT.put(sum2, g);
You need to follow the comments and edit your title so its a meaningful title that will help other people understand what your question is about.
Also, when you get an error, copy the big red text that is displayed in the console (color depends on which editor you are using), also known as the error's "Stack Trace" and past it into your question somewhere. This will help us pinpoint what is going on, and the error title itself will likely give away to us exactly what's wrong.
However, without any context on what the error is and what is before or after those two lines of code it is difficult to determine if you have previously defined sum2 or g as a variable that stores values. I am going to assume you haven't assigned at least one of them, likely g as a variable.
For experimental purposes, try replacing those two lines of code with:
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<0 , "g">;
That is putting zero (0) as an explicit integer and g as an explicit string into the hashtable. If you need to put variables into there, then you need to define them before hand, like this:
int sum2 = 3 + 4;
String g = "Some String";
Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<sum2 , g>;
Now the integer value 7 is stored as the hash and the string Some String as the mapped value.
This question already has answers here:
Why doesn't the post increment operator work on a method that returns an int?
(11 answers)
Closed 5 years ago.
private void calculateForEachQuestion() {
...
if (questions.size() >= answers.size()) {
answers.forEach(answer -> {
if (questions.contains(answer))
this.answer.setPoints((this.answer.getPoints())++);
// Variable expected ^
});
}
The error I encountered is:
unexpected type
required: variable
found: value
What is wrong with the statement?
this.answer.setPoints((this.answer.getPoints())++);
The ++ operator only makes sense when applied to a declared variable.
This operator will add one to an integer, long, etc. and save the result to the same variable in-place.
If there is no declared variable, there is nowhere to save the result, which is why the compiler complains.
One way to allow use of the ++ operator in this situation would be (not the most concise code, but to illustrate the point):
int myVariable = this.answer.getPoints();
myVariable++;
this.answer.setPoints(myVariable);
this.answer.getPoints() will return a value, and you can't use increment ++ or decrement -- on that. You can only use it on variables.
If you want to add 1 to it, you can do it as:
this.answer.setPoints((this.answer.getPoints())+1);
I know that, but why?
You can do this:
int x = 5;
x++; // x is a variable and can have value 6
but not this:
5++; // 5 is a value and can't have value 6
Change this:
this.answer.setPoints((this.answer.getPoints())++);
to:
this.answer.setPoints((this.answer.getPoints())+1);
++ will increment a variable with 1, but since this.answer.getPoints() will return a value and its not a defined variable, it won't be able to store the incremented value.
Think of it like doing:
this.answer.getPoints() = this.answer.getPoints() + 1, where would the incremented value be stored?
The error is just in this bit:
(this.answer.getPoints())++
The first part of this (this.answer.getPoints()) creates an rvalue: effectively, an anonymous variable that lasts almost no time.
The second part ++ increments that anonymous variable after it is passed into setPoints().
Then, the anonymous variable (now incremented) is thrown away.
The compiler/IDE knows that you're doing something that will never be used (incrementing a temporary value), and is flagging it.
I try to use following code to initialize a BigInteger at time of compiling (using breakpoint) in JAVA. So, I cannot use BigInteger x = new BigInteger(String str); because this is at time of debug the code by breakpoint.
the code:
BigInteger x = BigInteger.valueOf(36894801013086644936129270378595901597221307673940538800722758616305130630160);
error:
The literal 36894801013086644936129270378595901597221307673940538800722758616305130630160 of type int is out of range.
If I use BigInteger x = new BigInteger(String str); and then run the code, it will be OK, but I want to debug the code by some breakpoint and initialize BinInteger x (or change previous value of x) manually at time of debug.
The problem is that x keeps its previous value regardless to new value. apparently, only the "first initialization" value is kept. But, I want to change it to another value at time of debug, by a new value that is : 36894801013086644936129270378595901597221307673940538800722758616305130630160.
I appreciate any help.
In fact, I found a solution to do my test. I needed the value of BigInteger in another class that I changed the type of BigInteger to public static. So, I think now it would be better I delete my question.
This is for a red black tree.
For the pseudocode "p[z] <-- y", would the interpretation in java be:
z.getParent() = y;
or
z.setParent(y);
Thanks :)
The correct code would be z.setParent(y);
Be aware that z.getParent() = y; is invalid code. The assignment operator = stores the result of evaluating the expression to the right of the = into a variable, attribute or array position to the left of the =. In an expression like the first one in your question, you'd be trying to assign the value y into the result of calling z.getParent() - that is, trying to assign a value into another value, and that won't work.
The right way to change an attribute is by calling the corresponding setXXX() method, or by directly assigning to the attribute if it was declared non-private, like this: z.parent = y;
I've got the following line of code:
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)+1);
in a block where suffix has already been declared as an empty String (""). The block is looking for duplicate file names and adding a number on to any duplicates so they don't have the same name any more.
The line of code above compiles fine, but if I change it to this,
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)++);
I get Invalid argument to operation ++/--. Since Integer.parseInt() returns and int, why can't I use the ++ operator?
The ++ operator should update the value of its argument, so the argument should have a fixed position in memory to be updated. For this reason, the argument should be a variable*. In this case, the argument is Integer.parseInt(suffix), has no fixed memory address to be updated at all.
Intuitively, Integer.parseInt(suffix)++ is roughly equivalent to Integer.parseInt(suffix) = Integer.parseInt(suffix) + 1. But Integer.parseInt(suffix) is just an integer value, not associated to a fixed position in memory, so the code above is almost the same thing of, let us say, 32 = 32 + 1. Since you cannot assign a new value to 32 (neither to Integer.parseInt(suffix)) then there is no sense in supporting the ++ operator.
The good news is that this does not cause any problems at all! Instead of Integer.parseInt(suffix)++, write Integer.parseInt(suffix)+1.
* Or, as it is most commonly called, an l-value, or an address value.
++ requires an lvalue (an assignable value).
Integer.parseInt(suffix) is not an lvalue.
Note that i++ is not the same as i+1.
Writing i++ is a shortcut for i=i+1; if you were to 'read it in english' you'd read it as "i becomes current value of i plus one"
which is why 3++ doesn't make sense you can't really say 3 = 3+1 (read as 3 becomes current value of 3 plus one) :-)
The int is an rvalue. Since it isn't bound to a variable you cannot use post-incrementation.
++ expects an assignable value, i.e. a variable. Integer.parseInt returns a value that cannot be assigned. If you need a value plus one, use Integer.parseInt(suffix)+1 instead.