Computer Science

Java Primitive Data Types

Java Primitive Data Types are the basic building blocks of Java programming language. They are predefined data types that are not objects and are used to store simple values. There are eight primitive data types in Java: byte, short, int, long, float, double, boolean, and char.

Written by Perlego with AI-assistance

9 Key excerpts on "Java Primitive Data Types"

  • OCA Java SE 8 Programmer I Certification Guide
    primitive variable.
    Primitive data types, as the name suggests, are the simplest data types in a programming language. In the Java language, they’re predefined. The names of the primitive types are quite descriptive of the values that they can store. Java defines the following eight primitive data types:
    • char
    • byte
    • short
    • int
    • long
    • float
    • double
    • boolean
    Examine figure 2.1 and try to match the given value with the corresponding type.
    Figure 2.1. Matching a value with its corresponding type
    This should be a simple exercise. Table 2.1 provides the answers.
    Table 2.1. Matching a value with its corresponding data type
    Character values Integer values Decimal values Boolean
    a 100 7.3 true
      4573    
    In the preceding exercise, I categorized the data that you need to store as follows: character, integer, decimal, and Boolean values. This categorization will make your life simpler when confronted with selecting the most appropriate primitive data type to store a value. For example, to store an integer value, you need a primitive data type that’s capable of storing integer values; to store decimal numbers, you need a primitive data type that can store decimal numbers. Simple, isn’t it?
    Let’s map the types of data that the primitive data types can store, because it’s always easy to group and remember information.
    Note
    The category Boolean is not the same as the primitive data type boolean or wrapper class Boolean. Java Primitive Data Types and class names are displayed using code font.
    The primitive data types can be categorized as follows: Boolean, character, and numeric (further categorized as integral and floating-point) types. Take a look at this categorization in figure 2.2 .
    Figure 2.2. Categorization of primitive data types
    As shown in figure 2.2 , the char primitive data type is an unsigned numeric data type. It can only store positive integers. The rest of the numeric data types (byte, short, int, long, float, and double) are signed numeric data types (they can store both negative and positive values). The categorization in figure 2.2
  • Introduction to Java Programming, 2nd Edition
    b.All keywords are in lowercase.
    c.true , false , and null are literals, not keywords.
      Data Types
    Data type describes the size and type of values that can be stored in a variable. In any program , you need to store a particular type of data in the computer’s memory. The compiler should know the amount of memory that has to be allocated to that particular data. For this purpose, the data types are used. The main role of a data type is to direct the compiler to allocate a specific amount of memory to a particular type of data. Java is a strongly typed language, which means each type of data is predefined as a part of the language. In Java, a data type is divided into three categories:
      1. Primitive data types 2. Derived data types 3. User defined data types
    Note
    Variables will be discussed later in this chapter.
     
    Primitive Data Types
    The primitive data types are predefined by the Java programming language. These are the basic data types. They are declaration types and are used to represent single values but not multiple values. Java provides eight primitive data types that are as follows:
     
    byte
    short
    int
    long
    float
    double
    char
    boolean
      These eight primitive data types are grouped into four different categories which are discussed next.
    Note
    In most of the programming languages such as C++, the amount of memory allocated to a particular data type depends upon the machine architecture. But in Java, the size of all data types is strictly defined and it does not depend upon the machine architecture.
     
    Integers
    The integer data type is used only for those numbers that do not contain any fractional part or decimal point. In other words, this data type is used only for signed whole numbers, either negative or positive. In Java, four integer types are defined, byte , short , int , and long . The main difference among them is the amount of memory allocated to each of them and the maximum
  • OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide
    • Jeanne Boyarsky, Scott Selikoff(Authors)
    • 2019(Publication Date)
    • Sybex
      (Publisher)
    primitive types. These eight data types represent the building blocks for Java objects, because all Java objects are just a complex collection of these primitive data types. That said, a primitive is not an object in Java nor does it represent an object. A primitive is just a single value in memory, such as a number or character.
    The Primitive Types
    The exam assumes you are well versed in the eight primitive data types, their relative sizes, and what can be stored in them. Table 2.1 shows the Java primitive types together with their size in bits and the range of values that each holds.
    Table 2.1
    Primitive types
    Keyword Type Example
    boolean true or false true
    byte 8-bit integral value 123
    short 16-bit integral value 123
    int 32-bit integral value 123
    long 64-bit integral value 123L
    float 32-bit floating-point value 123.45f
    double 64-bit floating-point value 123.456
    char 16-bit Unicode value 'a'
    Is String a Primitive?
    No, it is not. That said, String is often mistaken for a ninth primitive because Java includes built-in support for String literals and operators. You’ll learn more about String in Chapter 5 , but for now just remember they are objects, not primitives.
    There’s a lot of information in Table 2.1 . Let’s look at some key points:
    • The float and double types are used for floating-point (decimal) values.
    • A float requires the letter f following the number so Java knows it is a float.
    • The byte , short , int , and long types are used for numbers without decimal points. In mathematics, these are all referred to as integral values, but in Java, int and Integer refer to specific types.
    • Each numeric type uses twice as many bits as the smaller similar type. For example, short uses twice as many bits as byte does.
    • All of the numeric types are signed in Java. This means that they reserve one of their bits to cover a negative range. For example, byte ranges from -128 to 127 . You might be surprised that the range is not -128 to 128 . Don’t forget, 0
  • OCA Java SE 7 Programmer I Certification Guide
    primitive variable.
    Primitive data types, as the name suggests, are the simplest data types in a programming language. In the Java language, they’re predefined. The names of the primitive types are quite descriptive of the values that they can store. Java defines the following eight primitive data types:
    • char
    • byte
    • short
    • int
    • long
    • float
    • double
    • boolean
    Examine figure 2.1 and try to match the given value with the corresponding type.
    Figure 2.1. Matching a value with its corresponding type
    This should be a simple exercise. Table 2.1 provides the answers.
    Table 2.1. Matching a value with its corresponding data type
    Character values Integer values Decimal values true/false
    a 100 7.3 true
      4573    
    In the preceding exercise, I categorized the data that you need to store as follows: character, integer, decimal, and true/false values. This categorization will make your life simpler when confronted with selecting the most appropriate primitive data type to store a value. For example, to store an integer value, you need a primitive data type that is capable of storing integer values; to store decimal numbers, you need a primitive data type that can store decimal numbers. Simple, isn’t it?
    Let’s map the types of data that the primitive data types can store, because it’s always easy to group and remember information. The primitive data types can be categorized as follows: Boolean, character, and numeric (further categorized as integral and floating-point) types. Take a look at this categorization in figure 2.2 .
    Figure 2.2. Categorization of primitive data types
    This categorization in figure 2.2 will help you further associate each data type with the value that it can store. Let’s start with the Boolean category.
    2.1.1. Category: Boolean
    The Boolean category has only one data type: boolean. A boolean variable can store one of two values: true or false. It is used in scenarios where only two states can exist. See table 2.2
  • Creating Components
    eBook - ePub

    Creating Components

    Object Oriented, Concurrent, and Distributed Computing in Java

    Float is an object.
    Because the Java language has no union constructs or pointers,* the data type of the variable referenced by the identifiers for primitives cannot be changed in Java. When a variable of a different data type is used in an assignment statement for a primitive, it always involves copying the variable and converting the copy to the correct type. For example, when setting an int variable to a float variable, the float variable is copied to another variable that has a data type of int, so the reference cannot change the data type it represents. And, because both the Java compiler and the JVM share the same representations and operations for primitive data types, they can agree that the compiler can generate the code to manipulate these variables, and their data types can be safely forgotten after the compilation step. This saves space, makes operations on primitives faster than operations on objects, and is similar to the mechanisms employed by the C/C++ compiler. However, these primitives are safe in Java because no mechanism is included to change the data type of any identifier or the referenced variable once it is created. In Java, then, the programmer can always rely on the fact that the data type of the identifier is the same data type of the variable it references. Thus, for Java primitives, while it is strictly incorrect to refer to the identifier and the variable as being the same, not making the distinction has no serious consequence; however, the mechanism for ensuring safe access of objects is very different than that for primitives in Java.
    Exhibit 3. Program4.3: Compiler Error in Referencing an Object of Data Type Object with a Variable Data Type of Person
  • Getting Started with V Programming
    Chapter 4 : Primitive Data Types
    In the previous chapter, we learned about working with variables and constants in detail. We also learned how to make single- and multi-line comments in the code that offer readability. In this chapter, we will learn about primitive data types.
    As we learned when defining variables in the previous chapter, the variables we define hold data that belongs to certain types, such as string, Boolean, or rune, or it could be one of the numeric types, such as the family of integer types or floating-point types. This chapter will cover in detail the nature of these types and how to work with them.
    In this chapter, we will learn various types and concepts related to primitive types as follows:
    • Primitive data types
    • The Boolean data type
    • Numeric data types
    • Operations on numeric data types
    • The string data type
    • The rune data type
    • Operations on the string data type
    By the end of this chapter, you will understand various primitive data types, such as strings, runes, and numeric types that include the family of integers and float types. You will also study string manipulation techniques and learn about what signed and unsigned integers are, and gain knowledge of the minimum and maximum values that these numeric ranges hold.

    Technical requirements

    Most of the code in this chapter can be run by accessing V's REPL as detailed in Chapter 2 , Installing V Programming , under the Accessing V Programming REPL
  • Introduction to Programming
    eBook - ePub

    Introduction to Programming

    Learn to program in Java with data structures, algorithms, and logic

    eywords are used to denote types, and we will also discuss them in this chapter. Passage contains an image

    Types

    Java is a strongly typed language, which means that any variable declaration must include its type. Type limits the value a variable can hold and how this value can be passed around. All types in Java are divided into two categories:
    • Primitive types : Described in the Primitive types and literals section
    • Reference types : Described in the Reference types and String section
    Some of the reference types require more attention, either because of their complexity or other particulars that have to be explained to avoid future confusion:
    • Arrays : Described in the Arrays section
    • String (the uppercase first character indicates it is the name of a class): Described in the Reference types and String section
    • Enum types : Described in the Enum types section
    Passage contains an image

    Comments

    The Java Specification provides the following information about comments:
    "There are two kinds of comments: /* text */ A traditional comment: all the text from the ASCII characters /* to the ASCII characters */ is ignored (as in C and C++). // text An end-of-line comment: all the text from the ASCII characters // to the end of the line is ignored (as in C++)."
    Here is an example of comments in the SimpleMath class that we have written already:
    public class SimpleMath { /* This method just multiplies any integer by 2 and returns the result */ public int multiplyByTwo (int i){ //Should we check if i is bigger than 1/2 of Integer.MAX_VALUE ? return i * 2 ; // The magic happens here }}
    The comments do not affect the code in any way. They are just programmer's notes. Also, don't confuse them with JavaDoc or another documentation generating system.
    Passage contains an image

    Identifiers and variables

    Identifiers and variables are among the most often used elements of Java. They are closely coupled because every variable has a name and the name of a variable is an identifier.
    Passage contains an image

    Identifier

    An identifier is the first in the list of Java tokens. It is a sequence of symbols, each may be a letter, a dollar sign $, an underscore, _, or any digit 0-9. The restrictions are as follows:
  • Learn C Programming
    Chapter 3 : Working with Basic Data Types
    Everything in a computer is a sequence of binary digits. C's basic data types enable the compiler to tell the computer how to interpret binary sequences of data. Intrinsic data types are predefined and built into the language.
    A binary sequence plus a data type results in a meaningful value. The data type not only leads to a meaningful value, but it also helps you determine what kind of operations on that value make sense. Operations involve manipulating values along with converting or casting a value from one data type into a related data type.
    In this chapter, we will cover the following topics:
    • Understanding bytes and chunks of data
    • Working with whole numbers
    • Representing numbers with fractions
    • Representing single characters
    • Understanding false (or zero) versus true (or anything not exactly zero)
    • Understanding how types are implemented on your computer with sizeof()
    • Discovering the minimum and maximum values for each type on your computer
    Once we have explored C's intrinsic data types in this chapter, we can then use them as building blocks for more complex data representations. This chapter will be the basis for the more complex data representations that we will encounter from Chapter 8 , Creating and Using Enumerations , to Chapter 16 , Creating and Using More Complex Structures .

    Technical requirements

    For the remainder of this book, unless otherwise stated, you will continue to use your computer with the following:
    • A plain text editor of your choice
    • A console, Terminal, or command-line window (depending on your OS)
    • The compiler, either GCC or Clang, for your particular OS
    For the sake of consistency, it is best if you use the same computer and programming tools. By doing so, you can focus more closely on the details of C on your computer.
  • Computer Programming for Absolute Beginners
    eBook - ePub

    Computer Programming for Absolute Beginners

    Learn essential computer science concepts and coding techniques to kick-start your programming career

    Usually, a naming convention is a recommended way for naming things, such as variables. This means that we can break these rules and the program will still work. However, there are several good reasons for us to obey these recommendations. One could be that if many programmers are involved in writing some code, the style will be consistent and, therefore, more straightforward to interpret for human readers.
    Some software companies have their own naming conventions. This is typically the case when the language itself has a weak or non-existing convention.
    When coming across a new language, we should always learn its conventions. If you work on several projects that use different programming languages, it can be tricky to remember what convention to use.
    Read more about naming conventions in Chapter 12 , Code Quality in the Using code conventions section.
    Now that we know how to name a variable, let's explore the different types that a variable can have.

    Primitive data types

    Every variable has both a name and a type. The type defines what kind of data can be stored in the variable. Typically, a language will have some built-in types, called primitive or basic types, to handle a single value.
    Primitive types can be divided into two categories—Boolean and numeric—which we will look at next.

    Boolean type

    In Chapter 1 , Introduction to Computer Programs , we talked about George Bool and his Boolean algebra. This defined how we can combine values of true and false with and , or , and not . To be able to use these values in our programs, we have a type that is named after Bool, called Boolean. A variable that uses this type can only have one of two values—true or false . For languages that have these types, we use the actual true and false words.
    Languages that have this type either call it Boolean or just bool .

    Numeric type

    Numeric types fall into one of two categories—integer types and floating-point types. We will look at them in detail, next.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.