By Alvin Alexander. Last updated: June 4, 2016
Question: Can you show me an example of the Java if/then
syntax?
Answer: Sure, here you go:
package com.devdaily.javasamples; public class IfThenTest { public static void main(String[] args) { String month ="April"; if (month.equals("January")) { System.out.println("Month was January"); } else if (month.equals("February")) { System.out.println("Month was February"); } else { System.out.println("Month was something else"); } } }
Note that you can write simple if/then
statements without using the curly braces ("{" and "}"), but in practice I strongly recommend using them (because when your code changes later on those curly braces make nice little containers that make your tests really obvious).