This question already has answers here:
String valueOf vs concatenation with empty string
(10 answers)
Is conversion to String using ("" + <int value>) bad practice?
(8 answers)
Closed 3 years ago.
I was just wondering, i've always used the first version but ran into instance of doing the second variation in our old codebase.
Is there any dangers related to second variation ?
Are they interchangeable ?
It feels strange to me to convert into String by doing just variable + "";
Thanks!
In Java the strings have the special operator + which is concatenation. If the operands of the concatenation are not strings their respective .toString method is called, and if it is the value is null the "null" string is concatenated. Most modern JITs or even compilers might decide to use a stringbuilder. So the only danger here is that you create extra objects which get discarded. Additionally, this code might be less readible or more confusing.
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 4 years ago.
How do I make a method that receives from the user a Mathematical function represented by (x) like (x + 2), then receives a number which is (x) .. then return the result !! ..
Here's a link that gives a example on how to implement the shunting yard algorithm for expression parsing:
https://eddmann.com/posts/shunting-yard-implementation-in-java/
You can use the ideas in this to build your expression parser and read the stack of commands using switch statements to perform the operation entered. Good luck on the project!
If you mean what i think you do, you just need to make a function with a parameter, do the calculation and return the result.
This question already has answers here:
Is there an eval() function in Java?
(14 answers)
Closed 5 years ago.
In java the following code works:
double a = 15.5+0.5;
System.out.println(a);
This will print 16.0.
So why does the following return a runtime error:
String a = "15.5+0.5";
Double b = Double.parseDouble(a);
System.out.println(b);
How can I get the second example to not give an error and behave like the first when converting the string to double and calculating a value?
parseDouble, parseInteger etc. don't perform expression evaluation, they just perform conversions from the string representation of some value to the numeric representation (so "5.0" gets converted to 5.0).
See the documentation here for the precise definition of parseDouble's behaviour.
If you want to perform expression evaluation, you could look into Reverse Polish Notation and the Shunting Yard algorithm, or ideally a library designed for this. Alternatively, you can use a more "hacky" solution.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Can someone explain why Java concatenates strings and integers in the following examples?
public class main {
public static void main(String[] args) {
System.out.println("test" + 5);
System.out.println(5 + "5");
}
}
And what is the difference from Python's implementation of the + operator.
In Python, this raises a TypeError:
"5" + 5
What makes you think + should behave in the same way for two different languages? In Java, when you have something like
System.out.println(5 + "5");
55
string concatenation is performed, as you can see. This is outlined by the Java language's specification (see JLS §15.18.1). By contrast, Python doesn't coerce the types like Java (hence the TypeError); in order for string concatenation to occur, both operands of + must be strings, which is why you need
>>> "5" + str(5)
'55'
This is outlined in Python's specification (see Python Language Reference §5.6). There is no universal rule that states + must behave in a specific manner across languages: if I wanted to, I could create a language in which + performs subtraction!
As #Wooble points out, Perl's + behaves in yet another way:
$ perl -e 'print 5 + "5"; print "\n"'
10
Simply put, in Java, the + operator is overloaded for Strings. Python obviously doesn't implement that override so you'll need to find out what the concatenation operator is for Python to make the latter case work.
You get a TypeError because "5" is of type string and 5 is of type int. Like someone said, its just how it was designed. Im guessing in part to prevent confusion. Do you want "5" + 5 to equal 10 or 55?
If you want 55 you can do this by converting the int to a string
'5' + str(5)
If you check docs
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
So ,it's java specific
Basically in Python you have to do the following:
"5" + str(5)
Because, although operator + is overridden for pairs of strings in both Python and Java, in Python for a string and an integer there is no appropriate override, and integers are not implicitly casted to strings when needed. Hence you need to explicitly cast 5 to a string, in order to have strings on both sides ot the concatenation.
The compiler java converts 5 + "5" into a StringBuilder internally and uses .append(int) to "add" the integer to the String.
In java, if there is a string type in a + b, then + will do concatenation where as in python the + of two different types has not been defined by default. Thus, as suggested, in python, one needs to do str(Int) before concatenation.
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.