What does this statement in Java mean? [duplicate] - java

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
I am confused by this statement. Please help me explain this statement:
return third == Long.MIN_VALUE ? (int)first : (int)third;

Considering your top tags in SO is Python, let's explain this statement with Python:
return first if third == sys.maxint else third
Of course the Long.MIN_VALUE is not necessarily equal to sys.maxint in Python.

Related

What does "+=" operator do? [duplicate]

This question already has answers here:
What does the "+=" operator do in Java?
(8 answers)
Closed 5 years ago.
I've seen code example that had
x += y
and I can't seem to find any explanation for this.
Please help. Thanks.
From:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.

Select all element that start with a specific numbers [duplicate]

This question already has answers here:
Retrieving the first digit of a number [duplicate]
(9 answers)
Closed 6 years ago.
Here is my code:
if(Rs.getInt("Number") == 100){
//Do something
}
Rs is a PreparedStatement.
Instead of defining the exact number, I want to look for numbers
which start with 1. Maybe there are '110' or '120' ... what should i write in the loop
instead of the operator == ?
This guy here seems to have the right idea.
Retrieving the first digit of a number
if ("1".equals(Integer.parseInt(Rs.getInt(number).substring(0, 1)))){
//do stuff
}

java string validation for 3 choices [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
I'm trying to make a user validation for multiple choices using a while loop and If the user doesn't match the choices then the program has to terminate. This is what I got so far
while(input!="air" || input!="water" || input!="steel"){
System.exit(0);
}
Unfortunately, it doesn't work.
I would prefer to do exceptions, but my professor is strictly against using it for this assignment. I really want to grow as a programmer so any advice, tips and tricks are welcomed, thank you all for trying to help me out.
Change it as
while(!input.equals("air") && !input.equals("water") && !input.equals("steel")){
System.exit(0);
}
Or
while(!(input.equals("air") || input.equals("water") || input.equals("steel"))){
System.exit(0);
}

Does an if clause go through all of its statements even if it's not necessary? [duplicate]

This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 8 years ago.
For instance if I have an if statement as follows:
if(returnsFalse() && timeConsumingFunction()){
//do whatever
}
Will the program run the time consuming function or will it realise that the if evaluates as false after the "returnsFalse()" function returns its value?
How does this work in different languages? Mainly interested in java and c.
No if you use && it will not continue on if the first statement is false.(Java) If you use & it will evaluate all expressions.

Weird issue with String [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am working on a multiplayer game.. and i came up with a really weird situation.
It's even possible? -
System.out.println(foo); // left
System.out.println(foo instanceof String) // true
System.out.println(foo == "left") // false
I don't understand how it's even possible..
I really don't think it will be a good idea to post all of my code.
Any ideas why its like that? and how can i fix it?
Thanks in advance
You shouldn't compare Strings by == operator, use equals method instead.

Categories