This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Pages: 1 - Last Post: 30-Jun-2020 12:55 Last Post By: davidekholm Threads: [ Previous | Next ]
AndreWolff

Posts: 1,635
Registered: 14-Dec-2007
Batch file generates the wrong code
Posted: 29-Jun-2020 22:28
 
  Click to reply to this thread Reply
I have in my common.css file this code:
	String h1ThemeFontSizeAndUnit = "";
	System.out.println("h1ThemeFontSizeUnit: " +  h1ThemeFontSizeUnit);
if (h1ThemeFontSizeUnit == "px") {
	h1ThemeFontSizeAndUnit =  Integer.toString(h1ThemeFontSizeNr) + "px";
} else {
    	sz= (float) h1ThemeFontSizeNr / 10f;
	h1ThemeFontSizeAndUnit =  Float.toString(sz) + "vw";
};
System.out.println("h1ThemeFontSizeAndUnit: " +  h1ThemeFontSizeAndUnit);

If I make my album by clicking the Make button I see in the system console what I expect:
Make changes
h1ThemeFontSizeUnit: px
h1ThemeFontSizeAndUnit: 42px
Make album: 3.857s
However if I make the album with a batch file I see in the log file:
jAlbum v20.2.7 started in console mode
h1ThemeFontSizeUnit: px
h1ThemeFontSizeAndUnit: 4.2vw

Who can explain why the batch file generates the wrong code?
jGromit

Posts: 7,653
Registered: 31-Jan-2006
Re: Batch file generates the wrong code
Posted: 29-Jun-2020 22:52   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
I'm surprised that BeanShell tolerates this - it's not how you do String comparisons in Java:
if (h1ThemeFontSizeUnit == "px") { do something }
It needs to be:
if (h1ThemeFontSizeUnit.equals("px")) {do something}
Not sure why BeanShell and the command line are different.
AndreWolff

Posts: 1,635
Registered: 14-Dec-2007
Re: Batch file generates the wrong code
Posted: 29-Jun-2020 23:05   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
Yes that solves indeed the problem, the batch file reports now too 42px.

Thanks for your help!
davidekholm

Posts: 3,385
Registered: 18-Oct-2002
Re: Batch file generates the wrong code
Posted: 30-Jun-2020 09:19   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
In Java, string comparisons should always be made by calling .equals(). == is for identity comparison, i.e. not just checking if the two strings contains the same characters, but checking if the two strings are one and the same (compare with two people named "Bob". equals() checks if they have the same name while == checks if "Bob" and "Bob" are actually the same person).

The reason why doing a == string comparison sometimes works is because Java optimizes memory usage and tries to reuse one single string for multiple string references if they actually contain the same characters. This trick can be done safely as strings are "immutable", but you can't bet that this optimization will always kick in. For that reason, always compare strings with .equals.
AndreWolff

Posts: 1,635
Registered: 14-Dec-2007
Re: Batch file generates the wrong code
Posted: 30-Jun-2020 10:33   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
David, thanks for the explanation. I am a dummy JAVA programmer who never used that language in real live, so this helps!
davidekholm

Posts: 3,385
Registered: 18-Oct-2002
Re: Batch file generates the wrong code
Posted: 30-Jun-2020 12:55   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
You're welcome
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums