This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".
Related
This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001
This question already has answers here:
The left-hand side of an assignment must be a variable
(6 answers)
"The left-hand side of an assignment must be a variable" problem with charAt
(4 answers)
How do I edit my program in order to assign a variable to an array list
(2 answers)
Closed 2 years ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
In the following loop how may I create a new List<>(), but I have a problem in the moment I try to set a value:
ListlistOfString = new ArrayList()<>;
listOfString.set(index, value);
Can anyone explain me how does it work?
Unlike arrays, you cannot do array[i] = val. You should use ArrayList.set:
// this must be set
announcementDTO.set(j, new ArrayList<>());
// this can still be get
announcementDTO.get(j).setIdShop(shopBean.getId());
announcementDTO.get(j).setNombreComercio(comercioBean.getNombre());
announcementDTO.get(j).setRazonSocial(comercioBean.getRazonSocial());
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.
This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Operators : |= bitwise OR and assign example
boolean bAlive;
bAlive |= this.properties.containsKey(name);
In the above , the code uses '|'.
Why using '|' ?
Thanks in advance.
The boolean is being OR'ed with the value on the right.
If this.properties.containsKey(name) is TRUE, then bAlive is set to TRUE.
Otherwise, bAlive remains the same.