{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chapter_5_beginning_javabook - These are sample pages from...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS Now, finally, we really begin studying computer programming with the Java language. Variables are important elements in computer programs, and they are needed in almost every computer program. In this chapter we shall study different types of variables and use the variables in simple arithmetic computations. We shall also explore names and key- words which are very basic elements in source programs. This chapter, as well as the following chapters, introduces many examples of Java source programs for you to study. A source program is a textual description of what the computer should do when the compiled version of the source program is executed by a computer. The source programs that you will see in this chapter contain variable declarations fol- lowed by executable program statements, action statements, which do something with the variables. The general structure of the programs is the following: As was already discussed in Chapter 2, the structure of our first programs is such that a static method named main() contains the action statements of the program, and the main() method is written inside a class declaration. At this phase, we do not attempt to profoundly understand the meaning of the class declaration. We’ll just try to figure out how the internal statements of method main() operate. import java.util.* ; class ClassName { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; Variable declarations. Action statements that modify and print the contents of the variables that were declared above. } } These lines are present only in those programs which read data from the keyboard. These are sample pages from Kari Laitinen’s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
100 Chapter 5: Variables and other basic elements in Java programs 5.1 Integer variables (int, short, long, byte, char) A variable in a source program is a basic program element that can be used to store a numerical value. The value of a variable usually changes when the program is being exe- cuted. When we declare a variable in a program, we actually reserve a few bytes of com- puter's main memory to be used for a special purpose. The following is an example of a variable declaration : int integer_from_keyboard ; This source program line which introduces a variable into a program can also be called a variable definition . The above source program line means that four bytes (32 bits) of memory are reserved to store an integer that will be read from the keyboard, and these four bytes can be referred to with the name integer_from_keyboard . Integers are whole numbers that have no decimal point. Integers can be positive or negative. Variables of type int are said to be 32-bit variables because they occupy 32 bits (four bytes) in the main memory of the computer A variable always has a type, such as int which is an abbreviation of the word "inte- ger". The programmer, the person who writes the variable declarations in a program, must
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}