chapter_5_beginning_javabook - These are sample pages from...

Info icon This 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
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
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern