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...

Data Types

The meaning of data type in programming

To properly define what is a data type in computer science, we must understand first what "type" means. After perusing the web for a definition, I found this:

-A category of people or things having common characteristics.

In computer science, in this case specifically for programming in Java, a data type is simply a categorization of things (objects) that have common characteristics. For illustration purposes, let us consider something we are all familiarized with: money. Since I am American, I am more familiarized with American currency. Therefore, I will use that for my first example.

American currency comes in bills or banknotes (generally referred as paper currency) and in metallic form called coins. Using the definition of data types, the generic data type known as "American Currency" can be further classified as "Paper Currency" and "Coin". Why do this? Because, although all forms of American currency have some common attributes, the truth is that a quarter and a one dollar bill have very little in common. However, a five dollar bill and a one dollar bill have much more in common.

Classifying objects by types

To model this for programming purposes, I need to understand how I need to categorize these different things in order to group like items. In this example, it makes sense to create two classes: One called "Coin" and the other one called "Bill" or "Banknote". All American coins are made of metal, but not the same metal. Therefore, I should add a data member that would hold the type of metal used to make this coin. Because the material doesn't change once the coin is made, I should make this data member a constant by adding the keyword "final" to the declaration of this data member. The same thing goes for the value of a coin. On the other hand, American banknotes are all made of paper. Although the type of paper can differ, let us assume it is the same for the sake of this illustration. Since "paper" is an inferred characteristic of a banknote that is identical regardless of value, the material doesn't need to be explicitly defined. Therefore, I will not include a data member to denote "material" in my paper currency class.

public class Coin
{
    private final double value; // final so value cannot be changed after creation
    private final String material;
    private final double size; // in millimeters
    ... (other properties omitted)

    public Coin(double value, String material, double size)
    {
        this.value = value;
        this.size = size;
        this.material = material;
    }

    public double getValue()
    {
        return value;
    }

    public String getMaterial()
    {
        return material;
    }

    public double getSize()
    {
        return size;
    }
}

public class Banknote
{
    private final int value; // int instead of double and material not included
    ... (other properties omitted)

    public Banknote(int value)
    {
        this.value = value;
    }

    public int getValue()
    {
        return value;
    }
}

Executable Example


public class CurrencyDemo
{
    public static void main(String[] args)
    {
        Coin penny = new Coin(0.01, "Copper-plated Zinc", 19.05);
        Coin nickel = new Coin(0.05, "Cupro-Nickel", 21.21);
        Coin dime = new Coin(0.1, "Cupro-Nickel", 17.91);
        Coin quarter = new Coin(0.25, "Cupro-Nickel", 24.26);

        Banknote oneDollar = new Banknote(1);
        Banknote fiveDollar = new Banknote(5);
        Banknote tenDollar = new Banknote(10);

        System.out.println("I have " + (oneDollar.getValue() + fiveDollar.getValue() + tenDollar.getValue()) + " dollars and " + (int)(100 * (penny.getValue()  + nickel.getValue() + dime.getValue()  + quarter.getValue())) + " cents.");
    }
}
To properly execute this, make sure you have both Coin and Banknote in your project's workspace.

This program outputs:


I have 16 dollars and 41 cents.

Due to the imprecise nature of primitive data types float and double, it is not recommended to use them when precision is required; for currency. According to Java Documentation, float or double should never be used when precision in calculation is required. Instead, the class BigDecimal should be used when calculation require a high degree of accuracy.

Primitives vs Reference types (objects)

In the previous section, I created two classes (reference types) called Coin and Banknote. Beginner programmers often struggle understanding what is a primitive data type and how they differ from reference types. Here are a few differences with a quick explanation for each one:
  1. A primitive type is predefined by the language and is named by a reserved keyword. Java has eight primitive data types: byte, char, short, int, long, float, double, and boolean.
  2. Primitive types have a specified size (determined by Java), with the exception of boolean. Reference types don't have a predetermined size. This is an advanced topic.

  3. Type Size
    byte 1 byte
    char 2 bytes
    short 2 bytes
    int 4 bytes
    long 8 bytes
    float 4 bytes
    double 8 bytes

  4. Reference types are stored in Heap memory and primitives are stored in Stack. This is also an advanced topic that will be discussed in a future blog.

Range of numeric primitives

There is one particular characteristic of Java numeric primitives that I neglected to mention when I first published this article and it is the concept of signed vs unsigned numbers. Unlike C++, there is no keyword to make a number unsigned. In Java, numeric primitives are signed all the time. The reason why this is important is because this fact affects the range of of values that can fit into a numeric primitive. This is because the Most Significant Bit (MSB) of a numeric primitive is reserved for the sign. A positive number will have a '0' as the MSB whereas a negative number will have a value of '1' in the MSB position. The remaining bits are used for the value. For example, a primitive byte is 8 bits in size where the lower 7 bits are used to hold the value. Therefore, a byte primitive in Java has a range of -128 to 127. The following table show the primitive data types and their respective range.

Type Min. Value Max. Value
byte -128 127
short -32,768 32,767
int -2,147,483,648 2,147,483,647
long -9,223,372,036,854,775,808 -9,223,372,036,854,775,807
float 1.40239846 x 10-45 3.40282347 x 1038
double 4.9406564584124654 x 10-324 1.7976931348623157 x 10308

Since primitive char is not a numeric data type, it does not follow the sign rule. Therefore, when typecast to a number, it is strictly unsigned. For that reason, its range it from Unicode '\u0000' (or 0) to '\uffff' (or 65,535).

A final note...

This illustration is good enough for a beginner programmer. Advanced programmers should know that I could've used an enumeration to model American currency instead of two classes. Or, I could've used abstract classes for Coin and Bill and children classes for the more specific types of coins and bills. Lastly, I could've demonstrated inheritance and polymorphism by creating a base class "Currency" from which "Coin" and "Banknote" could inherit the traits they both have in common (i.e. value).

Comments

  1. this blogs simply explains data type with the american mnoey so th author could explain it rally simple

    ReplyDelete
    Replies
    1. Thank you mac! I appreciate your endorsement. That was exactly my goal when I decided to use money to illustrate this concept. I am glad you liked it. Hopefully others did as well.

      Delete

Post a Comment

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. ...