Net Conditional Assignment Java

Java has a shortened version of an if else command. The use of it is very easy, if you understand it once.

It’s written:

x ? y : z;

Here the question mark and the colon will take the place of the commands if and else.
This means:

condition ? inCaseOfTrue : elseCase;

Explanation:
If the condition is true the part behind the questions mark will be taken, else the part behind the colon.
This shortened version has big advantages for coding, because it prevents blowing up your code.
e.g. if you want to return a string in a method depending on a parameter:

classic „version“, which is even short, just using the direct return.

public String getMood(boolean hasGoodMood){ if(hasGoodMood){ return "happy"; } return "sad"; }

And here the real short and smooth verion:

public String getMood(boolean hasGoodMood){ return hasGoodMood ? "happy" : "sad"; }

Especially for setting strings the short if else show their potential, e.g.:

longString = longString.length()>5 ? longString.substring(0, 5) : longString;

This short line will substring a string and prevents an indexOutBounds in case that the string is not long enough.

At the end both (short and classic if else) will be compiled to the same, but the meaningful use will prevent blowing up your source code and keep it more structured.

Douglas MacArthur's escape from the Philippines during World War II began on 11 March 1942, after U.S. President Franklin D. Roosevelt ordered him to withdraw. MacArthur left Corregidor Island and traveled in PT boats with his forces through stormy seas patrolled by Japanese warships, reaching Mindanao two days later. Arriving in Australia, he declared, "I came through and I shall return". MacArthur, a well-known general who had a distinguished record in World War I, had retired from the army in 1937 to become a defense advisor to the Philippine government. He was recalled to active duty in July 1941, a few months before the outbreak of the Pacific War with the Empire of Japan, to become commander of the U.S. Army Forces in the Far East, which included Philippine forces. By March 1942, the Japanese invasion of the Philippines had compelled him to withdraw his forces on Luzon to Bataan. The doomed defense of Bataan captured the imagination of the American public, and MacArthur became a living symbol of Allied resistance to the Japanese, at a time when the news from all fronts was uniformly bad. (Full article...)

Did you know...

  • ... that at the inquest into the death of Catherine Lynch(pictured), the presiding coroner described her as "one of a class who were a nuisance to themselves, their husbands and everybody else"?
  • ... that the appearance of Ash Lynx, the main character in the manga Banana Fish, is based on tennis player Stefan Edberg and actor River Phoenix?
  • ... that laughter, a signal of amusement, helps us cope with stress?
  • ... that the Diet of Transylvania elected Sigismund Rákóczi prince in 1607, although he had proposed his former son-in-law to the delegates?
  • ... that the landmarked 19th-century Robert and Anne Dickey House, which formerly housed the French consulate in New York City, is being incorporated into a new school building?
  • ... that the Dhammakaya Movement teaches that Nirvana exists as a literal reality within each individual?
  • ... that Rie Takahashi decided to pursue a voice acting career after noticing that many male characters were voiced by females?
  • ... that the American Bank Note Company Printing Plant included an office for a counterfeiter?

Catherine Lynch

Categories: 1

0 Replies to “Net Conditional Assignment Java”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *