IntelliJ code for specific project [duplicate] - java

This question already has answers here:
Java conditional compilation: how to prevent code chunks from being compiled?
(9 answers)
Conditional Java compilation
(11 answers)
Closed 7 years ago.
I am writing a cross platform game in IntelliJ IDEA using Java and I have run into a situation where I can't seem to find this feature which Visual Studio had:
This feature allowed me to have a condition, (2 lines of code), for example #if keyword1/#endif, and the code between those 2 lines, compiled only when the current project had keyword1 declared as compilation symbol through the project settings.
Is there any similar feature in IntelliJ IDEA ?

This is a preprocessor symbol, nothing IDE-dependant. There are some workarounds for this in java. For example you could use something like this:
public static final boolean DEBUG = false;
public void someMethod(){
if(DEBUG)
dosomething();
else
dosomethingElse();
}
Most precompilers will optimize this such that the result won't contain the if-else statement and reduce this to a simple call to dosomethingElse. But there's no preprocessor/precompiler statement like in c++ for java.

Related

Determine the user’s location and translate error messages [duplicate]

This question already has answers here:
how to detect operating system language (locale) from java code
(4 answers)
Closed 4 years ago.
I'm not sure if this is possible off-hand so bear with me. I'm trying to use no external API calls from Java and determine the users language and somehow show translated strings.
Is this something you can do in Java without getting the data from online or something similar? I'm trying to make it available as an offline application but I just don't know how.
Edit:
I'm looking for a way to check with only the standard Java API's
How about something like this:
LanguageTest lt = new LanguageTest();
System.out.println(lt.getGreeting());
private class LanguageTest{
String lang = Locale.getDefault().getLanguage().toLowerCase();
public String getGreeting(){
return (lang.contains("fr") ? "Bonjour" : "Hello");
}
}
In the above example if the language is not french it will default to english.

Java syntax: { ... } - What's that? [duplicate]

This question already has answers here:
Labeled Statement block in Java?
(4 answers)
Closed 5 years ago.
some time ago I randomly saw (in a decompiled Java code) something like this:
syntax: {
//some code
}
What does this do? And is there more stuff like this?
I was not able to find anything about this.
Greetings
I think this is called a label. And can be used with loops. See also using labels in java without "loops"
Apart from labels if you remove : from start
{ } can be used as init block which is used to write code that will execute as first line in constructor.

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.

What does statement "abc: {..}" mean? [duplicate]

This question already has answers here:
Labeled Statement block in Java?
(4 answers)
Closed 8 years ago.
I have never come across such expression in Java. It is not even a switch case
//no code above to make it look like a switch case or loop
abc: {
// do some stuff
break abc;
}
Do you have any idea what this does?
They are labels, see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html for a complete explanation.
abc:
is a label and {} introduces a new scope for that block.
This is likely a label referring to the code enclosed by the block. This allows you to transfer the flow of the program with more control as opposed to something like breaking out of a while loop.

Choose function in Java math? [duplicate]

This question already has answers here:
Combinatoric 'N choose R' in java math?
(17 answers)
Closed 8 years ago.
Sorry about the ambiguous title, I basically need to know how to use the choose function in java, to work out the chance of something happening, for example say I wanted to work out my chance of winning the lottery, on a calculator I'd do:
QUANTITY_OF_LOTTERY_NUMBERS CHOOSE 6
and that would return the chance of those six numbers appearing at random. I believe it's called the binomial theorem, but I dropped out of college before I could learn anything else about it :P
Anyway, if you do 49 choose 6 on Google for example, you'll see the function I'm trying to use
Question: How do I implement this function in Java code?
Apache Commons Math has it. If you are curious, you can read about Pascal's triangle.
http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/CombinatoricsUtils.html#binomialCoefficient(int, int)

Categories