1 / 8

Constant Variables

Constant Variables. Although the name may sound contradictory, (how can something be a variable and be constant?), you can make variables constant by putting the reserved word final in front of them.

helmut
Download Presentation

Constant Variables

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constant Variables • Although the name may sound contradictory, (how can something be a variable and be constant?), you can make variables constant by putting the reserved word final in front of them. • When you have constants in your program, the convention is to use all caps for the variable identifier, so it is easy to see it is a constant

  2. Constant (Final) Variables • A constant must be declared and initialized before it can be used, and you cannot change the constant value once it is declared. • final double PI = 3.14159; • final int LUCKYNUMBER = 7;

  3. Speaking of Coding Conventions... • Class names are typically written with the first letter capitalized • Non-constant variables are typically written in lower case • If your variable name is more than one word, capitalize the second word, like: int myName; • Variables should be named something that indicates what they will be used for...integer1, integer2, sum, and mean instead of w, x, y, z. • Whenever you have a curly brace (around the class, and around the main method), you should indent everything inside the braces, to show where it belongs

  4. Shortcut Operators • Many times when you are writing code, you will take a variable, modify it, and reassign the results back to the same variable • Because of this, we have shortcut operatorsthat help make the code easier to write (but harder to read): i = i + 1; i++; x = x + 3; x+=3

  5. Shortcut Operators Operator Example Equivalent += i+=8; i= i + 8; -= i-=8; i= i - 8; *= i*=8; i= i * 8; /= i/=8; i= i / 8; %= i%=8; i= i % 8;

  6. Prefix & Postfix Operators • ++ and -- are two unique shortcut operators because, depending how they are used, have different operator precedence • i++ is the same as i = i + 1, and it is the LAST thing performed in the operation.It is called a postfix operator. • ++i is same thing as i = i + 1, but it is the FIRST thing performed in the operation.It is called a prefix operator.

  7. Prefix & Suffix Operators • What is the difference? int x = 2; System.out.println(x++); //prints out 2, then //changes x = x+1; int y = 2; System.out.println(++y); //changes y = y+1, // then prints out 3;

  8. Prefix & Suffix Operators

More Related