[JAVA] Basics of conditional branching and return

I was worried because an error would occur if I did not return from the mysterious conditional branch

When I was writing code with an if statement, I was often angry with Eclipse saying "There is no return !!". But I don't know why. .. ..

In order to think in a more primitive form, I wrote a method that returns the larger one when two int type arguments are passed (it is one shot using the max function, but I experimented with an easy-to-understand example. Because I wanted to).

//Code 1

public class BiggerNumber {

	public static void main(String[] args) {
		//TODO auto-generated method stub
		System.out.println(returnBiggerNumber(250,500));
	}

	public static int returnBiggerNumber(int num1,int num2) {
		if (num1 < num2) {
			return num2;
		}else if (num1> num2 ) {
			return num1;
		}else if (num1 == num2) {
			return num1;
		}else {             //← This!
			return num1;
		}
	}
}

It was the last else that I couldn't understand. If you do not write this, you will get a compile error. .. ..

For this very simple method, there are only three possible branches.

  1. num1 is larger
  2. num2 is larger
  3. num1 and num2 are equal

Theoretically, this should cover all the possibilities. .. .. I should be able to return whatever number comes in the argument Is there any other possibility? I was worried.

Cause and how to write based on it

When I asked a senior with a long history of java about the cause of the error, ** "The compiler cannot determine whether all conditional branches are written without exception. Therefore, even if it is a conditional branch that cannot actually be reached, it is necessary to write it formally. 』** is what they said.

"I see!"

If so, the only possibility of branching other than <,> is ==. It may not be necessary to write the conditional expression of ==. So (I) the first thing that comes to mind is this.

//Code 2

	public static int returnBiggerNumber(int num1,int num2) {
		if (num1 < num2) {
			return num2;
		}else if (num1> num2 ) {
			return num1;
		}else {
			return num1;
		}
	}

Or there is this too.

//Code 3

	public static int returnBiggerNumber(int num1,int num2) {
		if (num1 < num2) {
			return num2;
		}else if (num1> num2 ) {
			return num1;
		}
		return num1;
	}

It feels a little strange, but this is also effective. Is this a matter of taste?

//Code 4

	public static int returnBiggerNumber(int num1,int num2) {
		if (num1 < num2) {
			return num2;
		}else if (num1> num2 ) {
			return num1;
		}else if (num1 == num2) {

		}
		return num1;
	}

Confused with Swift's switch {case} (half as a personal note)

In the first place, there should be no problem because we can return for all patterns! I was convinced that it was due to ** Swift's switch statement rules **.

That is this. ** "Is there a case clause that covers all possible values of the value type given to the switch? The default clause must exist. 』** (* By the way, it is not necessary in Java, and it is not necessary in the if statement even in the same Swift)

No, ** This is a story that has nothing to do with the return value. .. .. **I had a misunderstanding.

Whether it's a Java switch statement, a swift if statement, or a switch statement, If it is a function / method that returns a return value, it is necessary to return (return) all the values that come in.

Just in case, for example

public int returnTest(int i) {
	switch (i) {
	case 1:
		return 1;
	case 2:
		return 2;
	case 3:
		return 3;
     //In this way, you must always write return outside the default or switch block.
     //In swift, this writing method causes an error, so write it outside the switch block.
	default:
		return 4;
	}
}

This is a common rule, but it seems that it could not be organized due to half-finished language-specific rule knowledge.

So, I was convinced for the time being, but I would appreciate it if you could comment if you have any tsukkomi or + α.

Recommended Posts

Basics of conditional branching and return
Conditional branching of numbers
This and that of conditional branching of rails development
java (conditional branching and repetition)
Basics of Java development ~ How to write a program (flow and conditional branching) ~
Basics of java basics ② ~ if statement and switch statement ~
Basics of Ruby
Ruby on Rails ~ Basics of MVC and Router ~
Basics of threads and Callable in Java [Beginner]
[For beginners] DI ~ The basics of DI and DI in Spring ~
Program using conditional branching
Basics of try-with-resources statement
Calendar implementation and conditional branching in Rails Gem simple calendar
I summarized the types and basics of Java exceptions
[Ruby] Class nesting, inheritance, and the basics of self
Also complicated conditional branching
Java conditional branching: How to create and study switch statements
I tried to summarize the basics of kotlin and java
What I learned in Java (Part 4) Conditional branching and repetition
behavior of didSet and willSet
Overview of Docker and containers
Setup of JMeter and jEnv
[Rails] Introduction of devise Basics
Background and mechanism of Fabric-loader
Docker monitoring-explaining the basics of basics-
Ruby study memo (conditional branching)
Summary of FileInputStream and BufferedInputStream
Docker Compose basics and commands
[GCD] Basics of DispatchQueue class
Basics of character operation (java)
Understand the basics of docker
Combination of search and each_with_index
Judgment of JSONArray and JSONObject
The basics of Swift's TableView
[Ruby] Conditional branching Conditional expression order
A little complicated conditional branching
Summary of Java language basics
Operator of remainder and exponentiation (exponentiation)
Advantages and disadvantages of Java
Basics of Java development ~ How to write programs (variables and types) ~
Collection of programming selection tasks to make and remember (Java basics)
Creating a mixed conditional expression of Rails if statement and unless
[Rails] Read the RSS of the site and return the contents to the front