Computer Science

Java Non Primitive Data Types

Java Non-Primitive Data Types are data types that are not defined by the programming language itself but are created by the programmer. These data types include classes, arrays, and interfaces. They are used to store complex data structures and are essential in object-oriented programming.

Written by Perlego with AI-assistance

4 Key excerpts on "Java Non 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
  • Engineering Informatics
    eBook - ePub

    Engineering Informatics

    Fundamentals of Computer-Aided Engineering, Second Edition

    • Benny Raphael, Ian F. C. Smith(Authors)
    • 2013(Publication Date)
    • Wiley
      (Publisher)
    Figure 3.1b .
    Figure 3.2
    A partial decomposition of building information

    3.3 Derived Data Types

    Most programming languages support simple data types such as integers and real numbers; these are called basic or primitive data types. Earlier languages such as Fortran did not support grouping of primitive data types to create more complex data types. Such grouping of data is necessary in order to represent data structures so that there is a close correspondence between real objects and their representations in a programming language. For example, the object called Door in Figure 3.2 might be represented by a structure consisting of two primitive data types, height and width . Data types that are created by grouping primitive data types are called derived data types.

    3.3.1 Examples of Derived Data Types

    The difference between a basic data type and a derived data type is dependent on the programming language. What is considered to be a primitive data type in one language may not be primitive in another language. For example, arrays and strings are useful derived data types even though in some languages they may be considered to be basic data types.

    3.3.1.1 Arrays

    Arrays represent a collection of data of the same type. Arrays may be composed of integers, real numbers and character strings. Figure 3.3 illustrates the representation of an array. An array may be viewed as a piece of paper divided into a number of rows. Each data item is stored in a row. The size of each element (number of bits used to represent the data item) is constant. Each element has an index in the array. Indices are integers and they usually start from 0. It is common to have arrays of constant dimensions to; that is, the maximum number of elements in the array is specified a priori and cannot be changed during the execution of a program. This is analogous to a piece of paper having a fixed size and hence containing a fixed number of rows. If the size of the data is known a priori
  • 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
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.