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
Related key terms
1 of 5
10 Key excerpts on "Java Primitive Data Types"
- eBook - PDF
- Alvin Albuero De Luna(Author)
- 2023(Publication Date)
- Arcler Press(Publisher)
7.2. PRIMITIVE DATA TYPES Primitive types of data are those that have been not measured in terms of other kinds. A collection of basic data types is offered by almost all computer languages. The majority of the basic types, for instance, are essentially reflections of both the hardware. Others can be implemented with just a little amount of nonhardware assistance. The basic data kinds of languages are combined with one or more category constructors to describe the structured types (Figure 7.2) (Bruce, 1996; Sekiyama et al., 2015). Figure 7.2. Fundamental data types. Source: https://codeforwin.org/2017/08/data-types-in-c-programming.html. 7.2.1. Numeric Types Primitive types in some earlier computer languages were merely numbers. Among some of the types offered by modern languages, numerical types continue to be at the center of things. 7.2.1.1. Integer An integer is the most popular data type for basic numbers. Numerous systems’ hardware allows integers in a variety of sizes. Some programers offer these integer values as well as sometimes a few more. For instance, Java offers the bytes, short, int, and large verified integer types. Integer variable types, or types for decimal numbers without signed, are included in several languages, such as C++ and C#. Binary code often uses unsigned Programming Language Theory 192 types. Computers store signed integer values as strings of bits, including one bit (usually the leftmost) serving as that of the sign. The hardware natively supports the majority of integer types (Bobrow and Raphael, 1974; Ernst and Ogden, 1980). The Python large integer type (F# also offers such integers) is one illustration of such an integer data type which is not funded either by hardware. - eBook - PDF
- Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
As a programmer, you need a good understanding of data types because they are a key element of the variables that are the foundation for your programs. Programmers work with two main classifications of data types: primitive data types and composite data types. Primitive Data Types (4.1.2, 4.1.3) Primitive data types are built into your programming language. Figure 4-2 provides a list of them. Primitive data types are important time savers because your programming language knows how to allocate memory for storing these data types. For example, suppose you want to store the number of songs—23—for the first act of the Hamilton soundtrack in a variable called act1_song_count. Because Figure 4-1 The data displayed for a music album is stored in variables Source: apple.com/itunes Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Module 4 NuMeric data types aNd expressioNs 51 Figure 4-2 Primitive data types Primitive Data Type Keyword Description Examples Character char A single letter, symbol, or numeral 'a' '@' Integer int A whole number without any decimal places 1 128 3056 –2 Floating point float A number that includes decimal places 12.99 0.26 –3.56 Double floating point double A very large number that includes decimal places 3.84147098480789672 Boolean bool A logical value of True or False True False Valueless void No value Wide character wchar_t A Unicode character that requires more than 8 bits € ® ¥ the integer 23 is classified as a primitive data type, your programming language knows how much memory space to allocate for it. - eBook - PDF
- Doug Lowe(Author)
- 2023(Publication Date)
- For Dummies(Publisher)
Also, a special type of variable called an array, which can hold mul- tiple occurrences of primitive- or reference-type variables, is considered to be a reference type. Java defines a total of eight primitive types, listed in Table 2-1. Of the eight primitive types, six are for numbers, one is for characters, and one is for true/ false values. TABLE 2-1 Java’s Primitive Types Type Explanation int A 32-bit (4-byte) integer value byte An 8-bit (1-byte) integer value short A 16-bit (2-byte) integer value long A 64-bit (8-byte) integer value float A 32-bit (4-byte) floating-point value double A 64-bit (8-byte) floating-point value char A 16-bit character using the Unicode encoding scheme boolean A true or false value Working with Variables and Data Types CHAPTER 2 Working with Variables and Data Types 69 Integer types An integer is a whole number — that is, a number with no fractional or decimal portion. Java has four integer types, which you can use to store numbers of vary- ing sizes. The most commonly used integer type is int. This type uses 4 bytes to store an integer value that can range from about negative 2 billion to positive 2 billion. If you’re writing the application that counts how many hamburgers McDonald’s has sold, an int variable may not be big enough. In that case, you can use a long integer instead. long is a 64-bit integer that can hold numbers ranging from about negative 9,000 trillion to positive 9,000 trillion. (That’s a big number, even by federal deficit standards.) In some cases, you may not need integers as large as the standard int type pro- vides. For those cases, Java provides two smaller integer types. The short type represents a two-byte integer, which can hold numbers from –32,768 to +32,767, and the byte type defines a single-byte integer that can range from –128 to +127. - eBook - PDF
Elementary Synchronous Programming
in C++ and Java via algorithms
- Ali S. Janfada(Author)
- 2019(Publication Date)
- De Gruyter Oldenbourg(Publisher)
The reference data types are dis-cussed in Section 3.3. Primitive data types contain four integer data types including byte , short , int , long , as well as two floating point data types float and double , character data type char , Boolean data type boolean , and finally, the void data type void . A brief description of each data type is presented as follows. 1) Integer data type byte is used to save memory when dealing with a large number of integers which are in the range of this data type (Tab. 3.3). This is because a byte data type occupies the least space of the memory needed for a numerical data type, which includes eight bits (one byte). 2) Integer data type short is utilized to save memory for the integers lying in the range of this data type (Tab. 3.3). The space in memory which a short data type occupies is 2 times larger than that of byte . 3) Integer data type int is employed for integers of normal size. Furthermore, an int data type in the memory is 4 and 2 times larger than a byte and a short , respec-tively. 4) Integer data type long is applied when a wider range is needed compared to the int . It needs an eight-byte space to be stored in the memory. 5) Floating point data type float is used for real numbers with less precision. 6) Floating point data type double is utilized for real numbers with high precision. 7) Character data type char is employed to store a Unicode character in 2 bytes of the memory while the C++ language uses 1 byte to store the char data types. This is because C++ supports only ASCII codes for character data types which includes only the English letters and symbols and to do this 1 byte is sufficient. However, Java language supports the Unicode characters (i.e., letters and symbols) of more than 18 international languages and 1 byte of memory is insufficient for storing all these characters. - eBook - PDF
- Cay S. Horstmann(Author)
- 2014(Publication Date)
- Wiley(Publisher)
Table 1 Primitive Types Type Description Size int The integer type, with range –2,147,483,648 (Integer.MIN_VALUE) . . . 2,147,483,647 (Integer.MAX_VALUE, about 2.14 billion) 4 bytes byte The type describing a single byte, with range –128 . . . 127 1 byte short The short integer type, with range –32,768 . . . 32,767 2 bytes long The long integer type, with range –9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 8 bytes double The double-precision floating-point type, with a range of about ±10 308 and about 15 significant decimal digits 8 bytes float The single-precision floating-point type, with a range of about ±10 38 and about 7 significant decimal digits 4 bytes char The character type, representing code units in the Unicode encoding scheme (see Computing & Society 3.2 on page 139) 2 bytes boolean The type with the two truth values false and true (see Chapter 4) 1 bit Java has eight primitive types, including four integer types and two floating- point types. 3.1 Numbers 109 Table 2 Number Literals in Java Number Type Comment 6 int An integer has no fractional part. –6 int Integers can be negative. 0 int Zero is an integer. 0.5 double A number with a fractional part has type double. 1.0 double An integer with a fractional part .0 has type double. 1E6 double A number in exponential notation: 1 × 10 6 or 1000000. Numbers in exponential notation always have type double. 2.96E-2 double Negative exponent: 2.96 × 10 –2 = 2.96 / 100 = 0.0296 100,000 Error: Do not use a comma as a decimal separator. 3 1/2 Error: Do not use fractions; use decimal notation: 3.5 When a value such as 6 or 0.335 occurs in a Java program, it is called a number literal. If a number literal has a decimal point, it is a floating-point number; other- wise, it is an integer. Table 2 shows how to write integer and floating-point literals in Java. Generally, you will use the int type for integer quantities. Occasionally, however, calculations involving integers can overflow. - eBook - PDF
Brief Java
Early Objects
- Cay S. Horstmann(Author)
- 2019(Publication Date)
- Wiley(Publisher)
99 C H A P T E R 4 FUNDAMENTAL DATA TYPES C H A P T E R G O A L S To understand integer and floating-point numbers To recognize the limitations of the numeric types To become aware of causes for overflow and roundoff errors To understand the proper use of constants To write arithmetic expressions in Java To use the String type to manipulate character strings To write programs that read input and produce formatted output C H A P T E R C O N T E N T S © samxmeg/iStockphoto. 4.1 NUMBERS 100 SYN Constant Declaration 104 PT 1 Do Not Use Magic Numbers 106 ST 1 Big Numbers 106 4.2 ARITHMETIC 107 SYN Cast 110 CE 1 Unintended Integer Division 111 CE 2 Unbalanced Parentheses 112 PT 2 Spaces in Expressions 112 ST 2 Avoiding Negative Remainders 112 ST 3 Combining Assignment and Arithmetic 113 ST 4 Instance Methods and Static Methods 113 C&S Bugs in Silicon 114 4.3 INPUT AND OUTPUT 114 SYN Input Statement 115 HT 1 Carrying Out Computations 118 WE1 Computing the Volume and Surface Area of a Pyramid 121 4.4 PROBLEM SOLVING: FIRST DO IT BY HAND 121 WE2 Computing Travel Time 122 4.5 STRINGS 122 PT 3 Reading Exception Reports 127 ST 5 Using Dialog Boxes for Input and Output 128 C&S International Alphabets and Unicode 128 100 Numbers and character strings (such as the ones on this display board) are important data types in any Java program. In this chapter, you will learn how to work with numbers and text, and how to write simple programs that perform useful tasks with them. We also cover the important topic of input and output, which enables you to implement interactive programs. 4.1 Numbers We start this chapter with information about numbers. The following sections tell you how to choose the most appropriate number types for your numeric values, and how to work with constants—numeric values that do not change. 4.1.1 Number Types In Java, every value is either a reference to an object, or it belongs to one of the eight primitive types shown in Table 1. - No longer available |Learn more
- Gary Bronson(Author)
- 2012(Publication Date)
- Cengage Learning EMEA(Publisher)
To prevent programmers from attempting to perform an inappropriate operation, C++ allows performing only certain operations on certain data types. The types of data permitted and the operations allowed for each type are referred to as a data type . Formally, a data type is defined as a set of values and a set of operations that can be applied to these values. For example, the set of all integer (whole) numbers constitutes a set of values, as does the set of all real numbers (numbers containing a decimal point). These two sets of numbers, however, don’t constitute a data type until a set of operations is included—in these examples, mathematical and comparison operations. The combination of a set of values plus operations becomes a true data type. C++ categorizes data types in two basic groupings: class data types and built-in data types. A class data type (referred to as a “class,” for short) is a programmer-created data type, which means the programmer defines the acceptable values and operations, using C++ code. This data type is discussed in Part II. Built-in data types , also called primitive types , are provided as part of the C++ compiler and require no external C++ code. They consist of the basic numerical types shown in Figure 2.5 and the operations listed in Table 2.2. As shown in this table, most operations for built-in data types are designated as symbols. For class data types, most operations are provided as functions. Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 61 Chapter 2 Data Types To introduce C++’s built-in data types, literals are used. - eBook - PDF
- Xingni Zhou, Qiguang Miao, Lei Feng(Authors)
- 2020(Publication Date)
- De Gruyter(Publisher)
3 Basic data types Main contents – Basic data types, the essence of types, storage mechanism of integers, and floating-point numbers – Definition of variables, referencing method, and their way of storage in memory – Operators and their usage, the concept of expressions, categorization of results of operations – Summary of data elements Learning objectives – Understand and master concept of data types, data storage, data referencing, and data operation – Know how to use common operators and expressions – Understand and master the usage of constants and variables 3.1 Constants and variables At the checkout in supermarkets, we are given a receipt by a cashier, on which in-formation of our purchases is written as shown in Figure 3.1. The column total is computed by multiplying per-unit price with quantity, where the per-unit price is a constant and quantity is a variable. Some values are fixed, whereas others keep changing in many problems. For example, we have speed, time, and distance in moving object problems. In circles, we have a radius, perimeter, and Pi. In the shopping example above, we have the number of purchased goods, per-unit price, and total. Data in programs can be categorized into two types based on how they are used: constants and variables. The value of a constant cannot be modified during the execu-tion of programs, whereas the value of a variable can be changed during execution. 3.1.1 Constants There are two kinds of constants: literals and symbolic constants. One can use lit-eral constants directly in programs as needed without having to define in advance. However, if a constant is used multiple times in a program, we can use a symbolic constant instead to allow easy modification. In this case, we only need to modify once if we need to change the value of the constant. Symbolic constants should be defined before being used. - eBook - PDF
C# Programming
From Problem Analysis to Program Design
- Barbara Doyle, , , (Authors)
- 2015(Publication Date)
- Cengage Learning EMEA(Publisher)
To represent a single character, such as the first initial of your last name, a char data type is used. Your age, for example 21, is a whole number without a fractional part. The data type most often used to represent a number without a fractional part is an int . Table 2-7 gives some examples of different data types, with examples of what might be stored in each of these data items. Description grade Identifier Data type Data Grade point average gradePointAverage double 3.99 Current age age int 19 Full name studentName string Elizabeth Hill Final grade in a course courseGrade char A TABLE 2-7 Sample data types Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Predefined Data Types | 85 2 You learned in Chapter 1 that every program must define a class . There are thousands of classes that make up the .NET FCL. The following section describes some of the predefined data types that are considered primitive data types in most languages. These value and reference types will be explored: int , double , decimal , bool , char , and string . Predefined Data Types The .NET Framework includes a common type system (CTS) that is supported by all .NET languages, including C#. This enables cross-language integration of code meaning a single application can be written using multiple languages, such as C#, Visual C++, and Visual Basic. The types in the CTS are referred to as common types and are divided into two major categories, as seen in Figure 2-3: value types and reference types. - eBook - PDF
- Malcolm Bull(Author)
- 2016(Publication Date)
- Newnes(Publisher)
Activity 4 Make a list of the programming languages with which you are familiar. For each language: • list the data types which are available; • write down examples of the statements and declarations which you might use to declare variables of each type; • write down some examples of invalid uses of the data types in each language. Draw up a table showing which data types are available in which languages. Which data type(s) are most widely available? Which data type(s) are least widely available? If you are not familiar with any programming languages yet, use the languages which we have described in the text. Besides the primitive data types which we have mentioned, there are a number of additional facilities to be found in the various programming languages. These may not be widely available. In some languages, you may have to construct your own versions of these facilities, if you need 144 Data and data types them, using the tools which the language offers. With some languages, they may regarded as data structures, rather than data types. Arrays Most programming languages have some facilities for handling an array of data. This is a sequence of contiguous storage locations. The array is identified by name, and each individual storage location - each element - within the array is identified by its numeric position within the array - its subscript. Thus if we had an array called A comprising 10 elements, we could visualize the array as in Figure 3.15. A ( 1 ) A ( 2 ) A ( 3 ) A ( 4 ) A ( 5 ) A ( 6 ) A ( 7 ) A ( 8 ) A ( 9 ) A ( 1 0 ) Figure 3.1 5 A simple array The first element would have the subscript 1 and would be identified by the reference: A(l) the second element would be identified by A(2) and so on. The programmer must declare the amount of space required for the array.
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.









