Download as pdf
Download as pdf
You are on page 1of 32
PART 2 Basic Java Programming [Es programming language offers some basic features. The most basic features of a program- ming language are the data types and the operators to handle and manipulate data in a program. Being an object-oriented programming language, Java, in addition to supporting basic data types called primitives, lets you write your own data types called classes, giving you endless freedom to build your own data types. A class encapsulates data and methods to operate on that data accord- ing to the business logic in the problem that's being solved. The business logic is implemented by using execution flow control: the order in which the instructions in a program are executed, Because Java is an object-oriented language, it implements several object-oriented programming features such as encapsulation, polymorphism, and class hierarchy. So, in this part, we explore the basics of Java programming, including data types and opera- tors, methods, classes, and class hierarchy. CHAPTER 2 Data Types and Operators Exam Objectives 1.3 Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names, 7.6 Write code that correctly applies the appropriate operators including assignment operators (limited to: arithmetic operators limited to: +, -,*, J, %, ++, ~), relational operators (limited to: <, <: =), the instanceof operator, logical operators (limited to: & |, *,', && |), and the conditional operator (?:), to produce a desired result. Write code that deter- mines the equality of two objects or two primitives A computer program, written in any programming language, is basically made of three elements: data, operations on data, and the logic that determines the operations. Therefore, manipulating the data (ie. holding and operating on it) isat the core of a typical computer program. The data is held by variables, and the operations are made by using what are called operators. Data handled by the program may come in different types, such as integer or character. Every language supports certain basic data types, called primitive data types. However, a given variable may hold data of one specific type only. As you know from Chapter 1, Java, in addition to supporting primitive data types, supports, an infinite number of data types by letting you write classes. In this chapter, I discuss the primitive data types in detail, and introduce some nonprimitive data types built into the Java language. The core topic in this chapter is how you hold and operate upon the data in a Java program, To understand that, we explore three avenues: variables, data types, and operators. Data-Related Concepts Problems solved by a computer program often involve operations on data. From a program’s perspective, data is stored and retrieved by using variables. We refer to the variables by using their names, called identifiers. The data is operated upon by using the operators along with the variables. This section introduces all of these data-related concepts: variables, data types, identifiers, and operators, Understanding Variables, Data Types, and Operators Each data item in a computer program has a type such as integer or character. Because there are two kinds of datatypes, primitive (basic) and nonprimitive (advanced), there are two corresponding kinds of variables: primitive variables, which are commonly called just variables, and reference variables, which are also called object references. CHAPTER 2 © DATA TYPES AND OPERATORS As shown in Figure 2-1, a primitive variable holds the value for a primitive data type, such as an integer value, while a reference variable holds the reference to an object stored elsewhere in memory. For example, a student ID may be represented by a primitive variable with a primitive type int and an object of the class Student may be represented by a reference variable, say John, that refers to the actual object of the class Student. (You were introduced to classes and objects in Chapter 1.) We explore the primitive data types and variables in this section, and cover the reference variables later in the chapter, in the section "Working with Nonprimitive Data Types.” @ Primitive variable name Reference variable name Figure 2-1. A primitive variable holds the value of the data item, while a reference variable holds the ‘memory address where the data item (object) is stored. As just mentioned, a primitive data item such as an id of a student of int type is stored in a piece of memory referted to as a variable (the primitive variable). The values stored in a variable (piece of memory) may generally be changed, hence the name variable. A given variable can hold only a specific kind of data, which is called the data type. You need to specify the data type when you declare the variable. Declaring a variable includes naming the variable and specifying the data type that the variable will hold; for example: int id; This code statement, by declaring the variable, requests the allocation of memory for storing an integer number, and the memory will be referred to by using the variable named id, For example, we can store the number 9 in this piece of memory by the following code statement id Here, the symbol = is an example of one type of operator, called an assignment operator. You learn more about operators later in this chapter. The name of a variable, id in this example, is called an identifier. The following section explores the rules for naming variables. Naming the Variables: Legal Identifiers Each variable has a name, given to it by the programmer while declaring the variable. This name is. called an identifier For example, id in the example in the previous section is an example of an iden- tifier. Following are the rules to name a variable (or define an identifier):

You might also like