What is ?. operator in java? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java “?” Operator for checking null - What is it? (Not Ternary!)
Java Null-pointer-safe accessor
Recently I read in one of the java forums about ?. operator. They wrote that ?. could not make it to the java 7. Can anybody explain what exactly ?. is?
Also, I like to know if this operator has any specific name or not like ?: is known as ternary operator.

It's called the Null-safe operator.

I believe you mean the Null-safe operator, explained here. It was under consideration for Java 7, but subsequently dropped.
The other common use for the ? in Java is the ternary operator, which has been in Java since the dark ages and is explained here.
The two are completely different features however, the only common element is that they use the ? in some way.

Related

Working in Eclipse IDE, values that should equal not equal [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.

What's wrong with this Java Coded if STatement? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am not a professional programer. I am still learning, so my code is a little basic right now.
Scanner UserInput = new Scanner(System.in);
String UserChoose = UserInput.next();
if (UserChoose=="Quit"){
I have deduced that there is something missing in the if statement, but I cannot figure out what. Can someone please tell me what I am missing? I have been searching online for an hour with no luck.
To compare objects in java use .equals() method instead of "==" operator
Replace if (UserChoose=="Quit"){ with if (UserChoose.equals("Quit")){
Common mistake, use UserChoose.equals("Quit") to compare strings.
Since String is an object, using == will likely compare the memory location the 2 strings or something that would always result with false.
if (UserChoose.equals("Quit")){
In java, the default == operator compare if they both are the same object, even if the content is the same, if the object reference is not the same, it isn't ==.
See this link for more complete explanation: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

Can you implement a method to act as a +... but for objects instead of primitives? [duplicate]

This question already has answers here:
Operator overloading in Java
(10 answers)
Why doesn't Java need Operator Overloading? [closed]
(10 answers)
Closed 9 years ago.
I think that there is something like this in C/C++, but what about in java? Can I add a method that will add two of them together, but instead of doing say obj1.add(obj2) you can do obj1 + obj2? Can you do the same for -, *, /, and == & other comparatives? Can you create a primitive, or would that take modifying the JVM?
That's called operator overloading. And no Java does not support it. You can find more information here.
No you cannot. There is no operator overloading in Java. The only thing that comes close to an overloaded operator in Java is + which can concatenate strings and perform standard addition.

Python inline if when setting variable [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 9 years ago.
In Java, setting a variable to a value based on a condition could be done in one line
like so:
variable = (!true) ? 1 : 2
This would result to '2'.
Is there python equivalent to this code?
Thank you.
variable = 1 if not True else 2
General ternary syntax:
<value_if_true> if <condition> else <value_if_false>
This is called a conditional expression in Python, and is mostly equivalent to the "ternary operator" in C-family languages (although it's not actually an operator). The original proposal PEP 308 has more details.

# in java string instead of backslashes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)

Categories

Resources