Understanding the ? notation for Java [duplicate] - java

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 8 years ago.
I came across a code snippet online that used a notation that from what I gather seems to do a comparison and then returns back possible multiple outputs. I am still confused about it, even after research. Can someone re-write the code snippet to an equivalent, more basic version so that I can make sure I am understanding what I am seeing?
int mPart = i < mParts.length ? Integer.parseInt(mParts[i]) : 0;
Thanks in advance!

This is ternary IF operator. This line is equal to
int mPart;
if(i < mParts.length) {
mPart = Integer.parseInt(mParts[i]);
} else {
mPart = 0;
}

Related

In what version of Java did assigning false to an integer become invalid? [duplicate]

This question already has an answer here:
why would you want to declare a true false variable as type int?
(1 answer)
Closed 2 years ago.
I have the following legacy code from what looks to be a Java 1.1 library:
int colon_index = false;
for(int i = 0; i < params_split.length; ++i) {
int colon_index = params_split[i].indexOf(":");
if (colon_index > 0) {
// ...
}
}
It appears to be assigning false to a variable of type int. This is decompiled code, so it's also possible the IntelliJ decompiler has made a mistake.
I've checked release notes for old versions, but haven't been able to spot this change yet.
Assuming this was correct at some point in Java's history, in what version of Java did this syntax stop being valid?
You can't trust decompiled code. Originally, it would've been int colon_index = 0
Check out this answer for more details.

Boolean not breaking while loop. Assigning and checking equality are OK [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I can't believe I can't see what I'm doing wrong as this is something a beginner like me should already be used to...
I have the following code:
boolean doWhile = true;
while (doWhile == true){
String s = sc.nextLine();
if (s == "something"){
doWhile = false;
}
//DoStuff
}
Unfortunately, when s == "something", it doesn't break the loop, but instead continues to read the rest of the code.
I've searched around and a lot of people seem to make this mistake, but in their cases they messed up with the = and == things.
I'm pretty sure = is for assigning and == to check equality.
I also tried while(doWhile) and while(!doWhile) with the boolean to false, but nothing seems to work and I can't seem to wrap my head around something so simple.
EDIT: .equals() doesn't work either. Also, I've used == in this way before and it worked without a problem. Not sure why it's making a fuss right now.
EDIT3: No solution here: How do I compare strings in Java?
The solution was provided by harold.
Try this:
if (s.equals("something")){
}

Triple Dot (...) Syntax in Java? Meaning and conversions? [duplicate]

This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
I have this AVL tree with a balance method:
private void setBalance(Node... nodes) {
for (Node n : nodes)
n.height = height(n.right) - height(n.left);
}
It uses a (...) syntax I have not encountered before. I can't find it on google or SO. It seems to be some type of list syntax, or array. Looks like something I would find in ruby.
Could someone knowledgeable in Java's syntax explain this code to me, and perhaps show me a version without the ... syntax?
Thanks.
These are varargs . Basically same are arrays but can be zero to many.
More info that might also assist.

Java 8 way to repeat a block of code x times [duplicate]

This question already has answers here:
Does Java 8 provide a good way to repeat a value or function?
(6 answers)
Closed 7 years ago.
Maybe a normal for loop is still the right way but I wanted to see if there is a more succinct way to do it in java 8.
for (int i = 0; i < LIMIT; i++) {
// Code
}
Is there a more java 8 way to do this. I don't actually need i just need to repeat something x number of times.
Thanks,
Nathan
The best way I can see on how to do this would be something like IntStream.range(0, LIMIT).forEach($ -> code).
One of the reasons to use IntStream is to add parallel-ism, assuming you understand the impact of this.
IntStream.range(0, LIMIT).parallel().forEach($ -> {
// some thing thread safe.
});

Are backward loops faster in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicates:
Loop counter in Java API
Which of these pieces of code is faster in Java?
for(int i = 100000; i > 0; i--) {}
for(int i = 1; i < 100001; i++) {}
Which one is faster?I read that first for loop is faster.is it true?Then how it become faster than other?please help.
There is no way to tell which of the two is faster.
If all you provide is a snippet of Java code, all we have to go on is the Java Language Specification. Since the Java Language Specification never mentions any timing aspects there's no way to answer the question.
It is similar to asking your math teacher, "Which is faster to compute, 17+17 or 17*17?" Your math teacher will just stare at you and at best respond with something like, "are you using pen and paper or a pocket calculator?"

Categories

Resources