Chapter 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 11

Inside Class Methods PowerPoint PPT Presentation


  • 58 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 4. Inside Class Methods. What are variables?. Variables store values within methods and may change value as the method processes data. Variables. The scope of a variable determines how long it holds its value.

Download Presentation

Inside Class Methods

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 4

Chapter 4

Inside Class Methods


What are variables

What are variables?

  • Variables store values within methods and may change value as the method processes data.


Variables

Variables

  • The scope of a variable determines how long it holds its value.

    • Local variables maintain their scope within the block of code in which they are declared.

    • Local variables are not fields of the class.


Declaring and initializing variables

Declaring and Initializing Variables

  • Declare a variable by identifying its type and the identifier (name):

    • double averageSpeed;

  • Initialization is when you declare a variable and assign it a value at the same time:

    • double averageSpeed = 21.6;


What are operators

What are operators?

  • Operators are symbols that take action within a program.

    • Assignment operator (=) assigns a value to a field or variable:

      • averageSpeed = 21.6;

    • Mathematical operators include:

      • +, -, *, and /

    • Relational operators include:

      • <, >, ==, and !=


A self assignment operator

A Self-Assignment Operator

  • Manipulates a variable and assigns the results back to itself.

    • Self-assignment operators include +=, -=, *=, and %=

    • int x = 5;

    • int y = 6;

    • x += y;

    • x has the value (5 + 6) = 11


Precedence

Precedence

  • Java follows mathematical rules of precedence.

    • Multiplication and division are handled first, followed by addition and subtraction

    • Use parentheses to force evaluation


Increment and decrement operators

Increment and Decrement Operators

  • The increment operator (++) means increment (add) by one.

    • ++x;

  • The decrement operator (--) means decrement (subtract) by one.

    • --x;


Prefix vs postfix

Prefix vs. Postfix

  • Prefix notation increments, then fetches:

    • int x = 5;

    • int y = ++x;

    • Value of y is 6 (1 + 5), value of x is 6

  • Postfix notation fetches, then increments:

    • int x = 5;

    • int y = x++;

    • Value of y is 5, value of x is 6


What is a constant

What is a constant?

  • A constant is a variable with a fixed value (cannot be changed).

    • Use the keyword final to designate a constant.

    • Constant identifiers are typically UPPER_CASE notation to distinguish them from other variables.


Relational operators

Relational Operators

  • Evaluate the equality or inequality of two intrinsic types.

    • Return a boolean value (true or false)

    • Equality: ==

    • Inequality: <, >, <=, >=, != (not equal)


  • Login