Computer Science

Java Data Types

Java Data Types refer to the different types of data that can be used in Java programming language. These include primitive data types such as integers, floating-point numbers, and characters, as well as non-primitive data types such as arrays, classes, and interfaces. Understanding data types is essential for writing efficient and effective Java code.

Written by Perlego with AI-assistance

9 Key excerpts on "Java Data Types"

  • Book cover image for: Java Programming
    eBook - PDF
    Java’s eight data types are described in Table 2-1. Later in this chapter, you will learn more spe- cific information about several of these data types. The eight data types in Table 2-1 are called primitive because they are simple and uncomplicated. Primitive types also serve as the building blocks for more complex data types, called reference types, which hold memory addresses. The classes you will begin creating in Chapter 3 are examples of reference types, as are the System class you used in Chapter 1 and the Scanner class you will use later in this chapter. Declaring and Using Constants and Variables A data item is constant when its value cannot be changed while a program is running. For example, when you include the following statement in a Java class, the number 459 is a constant: System.out.println(459); Every time an application containing the constant 459 is executed, the value 459 is displayed. Programmers refer to a number such as 459 in several ways: • It is a literal constant because its value is taken literally at each use. • It is a numeric constant as opposed to a character or string constant. • It is an unnamed constant as opposed to a named one, because no identifier is associated with it. Keyword Description byte Byte-length integer short Short integer int Integer long Long integer float Single-precision floating point double Double-precision floating point char A single character boolean A Boolean value (true or false) Table 2-1 Java primitive data types A programmer also might say that when a constant value such as 459 appears in a program, it is hard-coded. Copyright 2019 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.
  • Book cover image for: Java All-in-One For Dummies
    • 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.
  • Book cover image for: Programming language theory
    • Alvin Albuero De Luna(Author)
    • 2023(Publication Date)
    • Arcler Press
      (Publisher)
    DATA TYPES CHAPTER7 CONTENTS 7.1. Introduction .................................................................................... 188 7.2. Primitive Data Types ....................................................................... 191 7.3. Character String Types..................................................................... 195 References ............................................................................................. 199 Programming Language Theory 188 7.1. INTRODUCTION A data type specifies both a set of data elements as well as a set of activities that may be performed upon these values in a predetermined order. Data is manipulated by computer software to get the desired results. The degree whereby the data types that are accessible in the language that is used are similar to the things that exist in the actual world that are being addressed by the challenge is an essential component in deciding how easily they will be capable to do this job. As a result, a programming language must enable an adequate assortment of data types. For example, In the past 60 years, there has been an evolution in the ideas that constitute modern data types. All challenge space structures would have to be represented in the first languages using just a select handful of the most fundamental data structures that were supported by those languages. Linked sets and branching trees, for instance, were both implemented using arrays in versions of Fortrans before 1990 (Liskov and Zilles, 1974; Mitchell, 1996). In COBOL, the data models made the first step out from the Fortran I paradigm by enabling programers to define the precision of numeric data value, as well as by offering an organized data form for recordings of information (Scott, 1976; Herlihy and Liskov, 1982). This was the first move away first from Fortran I paradigm. The capacity of precision specification has been enhanced by PL/I to include both integer and flying types.
  • Book cover image for: Basic Data Structures and Program Statements
    • 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.
  • Book cover image for: Programming Languages
    No longer available |Learn more

    Programming Languages

    Principles and Practices

    In Java the simple types are called primitive types , and types that are constructed using type constructors are called reference types (since they are all implicitly pointers or references). The primitive types are divided into the single type boolean (which Copyright 2011 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. 8.4 Type Nomenclature in Sample Languages 351 is not a numeric or ordinal type) and the numeric types, which split as in C into the integral (ordinal) and floating-point types (five integral and two floating point). There are only three type constructors in Java: the array (with no keyword as in C), the class , and the interface . The class and interface constructors were described in Chapter 5. Java Types Primitive boolean Numeric Integral Floating point float char byte short int long Array class Reference interface double Figure 8.6 The type structure of Java 8.4.3 Ada Ada has a rich set of types, a condensed overview of which is given in Figure 8.7. Simple types, called scalar types in Ada, are split into several overlapping categories. Ordinal types are called discrete types in Ada. Numeric types comprise the real and integer types. Pointer types are called access types. Array and record types are called composite types . Copyright 2011 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.
  • Book cover image for: C# Programming
    eBook - PDF

    C# Programming

    From Problem Analysis to Program Design

    Neale Cousland / Shutterstock.com All Microsoft screenshots used with permission from Microsoft Corporation. D ATA T YPES AND E XPRESSIONS IN THIS CHAPTER, YOU WILL: ? Examine how computers represent data ? Declare memory locations for data ? Explore the relationship among classes, objects, and types ? Use predefined data types ? Use integral data types ? Use floating-point types ? Learn about the decimal data type ? Declare Boolean variables ? Declare and manipulate strings ? Work with constants ? Write assignment statements using arithmetic operators ? Learn about the order of operations ? Learn special formatting rules for currency ? Work through a programming example that illustrates the chapter’s concepts 2 CHAPTER © zeljkodan/Shutterstock.com 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. 72 | Chapter 2: Data Types and Expressions Chapter 1 introduced you to the basic elements of a C# program. You discovered the requirements for developing, compiling, and executing applications. This chapter focuses on data. Data is the raw facts, the basic numbers and characters that are manipulated to produce useful information. In this chapter, you will begin to see the power of programming when you write applications to perform calculations. You will learn how to declare variables that hold different types of data in memory and see how arithmetic operators react given different kinds of data. Data Representation You might hear someone say that his computer has a 64-bit processor with 8 GB of RAM and 1 TB of hard disk space.
  • Book cover image for: Learn C Programming
    This is very much like the byte sequences the compiler generates, which the CPU processes. Internally, commands, addresses, and data in the computer are nothing more than sequences of 1s and 0s of various sizes. So, how the computer interprets the patterns of 1s and 0s is entirely dependent upon the context given to them by the computer language and the programmer.
    We, as programmers, must provide the guidelines to the compiler, and consequently to the CPU, on how to interpret the sequence. We do this in C by explicitly assigning a data type to the data we want to manipulate. The data type of a value provides the context to correctly interpret a sequence of bits.
    C is a strongly typed language. That is, every value must have a type associated with it. It should be noted that some languages infer the type of a piece of data by how it is used. They will also make assumptions about how to convert one data type into another. These are called loosely typed languages . C also does automatic, or implicit, conversions from one type into another, but the rules are fairly specific compared to other programming languages. We can also perform manual, or explicit, conversions with type casting . Type casting is an operation that we might need to do to a value; it will be covered in greater detail in Chapter 5 , Exploring Operators and Expressions .
    In C, as in most programming languages, there are five basic, and intrinsic , data types. The five basic types are as follows:
    • Whole numbers : These numbers can represent a positive-only range of values or a range that includes both positive and negative values.
    • Numbers with fractions or decimal numbers : These are all of the numbers between whole numbers, such as ½, ¾, 0.79, and 1.125, as well as other numbers that aren't easily represented using fractions. One such common number is 3.14159, which is a very approximate value for π, or even 3.1415926535897932384626433 – an even more precise but still approximate value for π. Decimal numbers can always include negative values.
    • Characters : These are the basis of C strings. Some languages have a separate string type. In C, strings are a special case of arrays of characters – they are not a data type but a special arrangement of contiguous character values. We will begin our encounter with arrays in Chapter 11 , Working with Arrays , and then look at strings specifically in Chapter 15 , Working with Strings
  • Book cover image for: Elementary Synchronous Programming
    eBook - PDF

    Elementary Synchronous Programming

    in C++ and Java via algorithms

    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.
  • Book cover image for: Students' Guide to Programming Languages
    • 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.