Skip to main content

Java Keywords (Part XXIV): native

Java keyword list abstract continue for new switch assert default goto * package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static void class finally long strictfp volatile const * float native super while Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers. This is the last chapter of the Java Keyword series. This is probably the keyword I have used the least. In my 20 year career as a software developer, I have used this keyword once, and that was to make some addition to legacy code. The keyword native is a method modifier . Basically, it is a keyword that can only be applied to methods. According to the Java Language Specification (JLS), A method that is native is implemented i...

Java Keywords (Part III): Returning Values from Methods

I have covered 7 keywords so far in the "Java Keyword" series, plus the eight primitive data types in my "Data Types" blog. In this article, I will discuss the keywords return and void. But, in order to understand their usage, I must get into a bit deeper discussion of some fundamentals of computing.

Java keyword list

abstract continue for new switch
assert default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const* float native super while
Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers.
Although the list above is arranged in alphabetical order, I will go through them in a different order.

Method return values

return and void

It makes sense to discuss these two keywords together. They are almost symbiotic in nature. In a method definition, void indicates no value will be returned to the caller of the method, while return "returns" from the method. Let us review method declaration for just a moment.

public class Coin
{
    private final double value;

    // Constructor and other members omitted

    // returns a value
    public double getValue()
    {
        return value; // returns the value stored in the "value" data member
    }

    // returns no value
    public void printValue()
    {
        System.out.println((int)(value * 100) + " cents");
    }
}

In the method signature, the parameter located just before the method name is the return type. In the code above you can see that the getValue() method returns the value of the Coin object. This value is of type double. This is important because the entity calling this method must know what type of data it is receiving (if any) in order for it to know how to process it. This is how invoking this method will look on the client's side (assume the Coin object instance was property constructed):


double value = myCoin.getValue();

The data type of the entity to the left of the equals sign must match the return type of the method being invoked on the right side of the sign. In this example, the method getValue() returns a primitive double and the variable on the client's side is also a primitive getValue().

What about void? I mentioned before that void returns no value which is different than saying "returns nothing". It was worded like that for a very good reason. All methods return something. If you have taken an introductory class in computer science or have taken Assembly Language, you should know that all functions (methods) return the address of the next instruction stored in stack.


public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
        System.out.println("Java is fun!");
    }
}

Without going in a lot of details, when a program is executed, the address of the first instruction is loaded into memory. As each line of code is executed, the address of the next instruction is loaded so the program knows what to do next. Here you can see two instructions inside the main method. But, what it may not be immediately obvious is that those instructions are calling methods belonging to another class that is loaded in another area of memory. In this example, when System.out.println("Hello World!") is called, the address of the next instruction (System.out.println("Java is fun!")) is stored in something called the "Instruction Pointer". This instruction is stored in stack. As the name implies, a stack is a data structure where the first item you store in it is the last item that you take out (like a stack of pancakes!). Once the address of the next instruction is stored in stack, the call to the println() method is made, the instruction pointer receives the address of the first instruction inside the println() and the process continues. Each time a line is executed, the Instruction Pointer is updated with the address of the next instruction. When the last instruction is executed, the stack is popped so that the address that was stored before the initial call is once again set in the Instruction Pointer. In our example, this is the address of the System.out.println("Java is fun!") instruction.

In short, when a method returns void the method might not return a value to the client who called it, but the Operating System must return the address of the next instruction to the Instruction Pointer that was previously stored in stack. Lastly, you might have noticed that a method returning void does not have an explicit return line. In Java, it is not necessary to include a return at the end of the method.

Next up, Part IV: Classes and Objects

Comments

Popular posts from this blog

Implementing Interfaces with Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Customizing Java Records " first and then come back to this one. Now that you know how to customize Java records, implementing an interface using Java records should be very easy to understand. If you don't know about interfaces in Java, you can read more on my article about interfaces. The recipe for implementing an interface is simply an expansion of what you learned in my previous blog on how to customize a Java record. Following our Rectangle example, let's create an interface with the same two methods we used before. public interface Shape { double area(); double perimeter(); } Now, let's further customize the previous example by doing two things: Add implements Shape at the end of the record declaration (after the record constructor), and Add @Override to the existing methods to ensure these methods com...

Customizing Java Records

If you have not read my article on Java records and do not know about this topic, please read my blog titled " Java Keywords Addendum: The Java Record " first and then come back to this one. What is a customization of a record? A customization of a record is simply the addition of code inside the body of the class. Before proceeding further, let's recap important aspects of a Java Record: Java records are immutable Because of item 1 above, you cannot add new fields unless defined in the record constructor Java records already override: Object#equals(Object) and Object#hashCode() , and then override Object#toString() You could redefine overridden methods as part of your customization if you would like. For example, if you want a fancier implementation of the Object#toString() method, you could do so. Let's look at our first customization example. Using the example from my previous blog, public record Student(...

Object-Oriented Programming Basics: What is in a Class?

EDITORIAL NOTE : This article was published briefly back in 2016 and quickly set back to draft because I wasn't happy with its contents. It is a shame that it was taking me three years to revisit this topic and work on a new and improved version. At least, I'm hoping it will be to the liking you the reader. Keep in mind that the opening paragraph will still read as if I just wrote it for my (former) students at Texas Wesleyan. I started working on lecture on the topic of Object-Oriented (OO) Programming by gathering some material, old and new, when I realized this might be good and simple post for my second attempt at blogging. To be completely honest, in the 8 hours I spent collecting information and preparing material for this week's lecture, I realized I still made some of the mistakes I am about to blog about. I am actually hoping I can write a series of postings regarding Object-Oriented Programming (OOP). But to do so, I must start from the very beginning. ...