Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Final parameter in a method in java

I have 2 questions with this code segments

  1. method 1 is working fine and method 2 doesn't. What is the reason for this?
  2. In method 1 return value is byte(8 bit). But we actually return a char value(16 bit). what is actually happening here?

//method 1

static byte m1() {
    final char c = 'b'-'a';
    return c; 
}

//method 2

static byte m3(final char c) {
    return c; // 3
}
like image 907
chathura Avatar asked Aug 11 '13 10:08

chathura


People also ask

What is final parameters in Java?

You can pass final variables as the parameters to methods in Java. A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed.

How do you make a parameter final in Java?

The final keyword when used for parameters/variables in Java marks the reference as final. In case of passing an object to another method, the system creates a copy of the reference variable and passes it to the method. By marking the new references final, you protect them from reassignment.

Can final be used in methods Java?

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .

What are final methods in Java?

Final Method in Java We can declare Java methods as Final Method by adding the Final keyword before the method name. The Method with Final Keyword cannot be overridden in the subclasses. The purpose of the Final Method is to declare methods of how's definition can not be changed by a child or subclass that extends it.


2 Answers

char in Java is a 16 bit unsigned value, while byte is 8 bit signed value. Allowed range for byte is [-128, 127]. So, not all character can be assigned in byte.

In your first method, you are returning a char with code point = 1 ('b' - 'a'). Now since you have defined char as final, and assigning to it a constant expression, it becomes a compile time constant. So, compiler doesn't give any compiler error.

From JLS Section 5.2:

If the expression is a constant expression (§15.28) of type byte, short, char, or int:
- A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

Emphasis mine.

However, if you make c non-final, it will also result in a compiler error:

static byte m1() {  // This will be an error
    char c = 'b'-'a';
    return c; 
}

The reason is, c is not a compile time constant any more, and compiler doesn't do an implicit downcast.

In 2nd method you are returning the char that you passed. The parameter c there is not a compile time constant. It isn't known at compile time what value the method might get. Like, if you pass a char with code points not in range of allowed byte value, it won't work.

To make the 2nd method work, you can do an explicit cast:

static byte m3(final char c) {
    return (byte)c; // 3
}
like image 164
Rohit Jain Avatar answered Oct 01 '22 19:10

Rohit Jain


In m1() the compiler sees that char c is constant with value 1 and therefore does not complain as it knows it can be fit into byte. If you changed it to final char c = 128 where 127 is the maximum size of the byte you would get complaint, as you also would when you removed the finalvariable descriptor from char c.

like image 31
ps-aux Avatar answered Oct 01 '22 18:10

ps-aux