Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:

Similar presentations


Presentation on theme: "Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:"— Presentation transcript:

1 Java – Variables and Constants By: Dan Lunney

2 Declaring Variables All variables must be declared before they can be used A declaration takes the form: Example: int length; It is good practice to declare related variables together Example: int length, width;

3 Variables cont. Variable names must start with a letter (should be lowercase) Variable names may include numbers and some special characters – but this practice is not encouraged Variables are declared at the beginning of classes and methods

4 Primitive Data Types int – positive or negative integers between -2.1 billion to + 2.1billion double – positive and negative numbers that may contain a decimal (sometimes refered to as floating point) char – a single character boolean – true or false

5 Abstract Data Types Abstract data types typically come from a class – either a built in class that is imported or a class written by the programmer A variable declared using a class is called an object = new (args) Example: Scanner input = new Scanner(System.in);

6 Java Packages Some classes in Java that are already written come in Java Packages The only package that loads automatically in java is the java.lang package used to define the java language Other packages must be imported to be used by the java program Example: import java.util.Scanner; This imports the scanner class used to get input from the keyboard

7 Scanner Class Methods The scanner class comes with several methods to read info from the input stream: next() returns a string from the input stream (to the first blank space) nextLine() returns the next line from the screen nextInt() returns the next integer nextDouble() returns the next double nextBoolean() returns the next boolean close() closes the input stream

8 Type Casting Type casting converts a number of one type to a number of a different type i.e. a double to and int or vice versa Example: x = I + int(d); The d which was declared as a double type is turned into an integer type. Therefore any decimal portion is truncated Example: area = double(length) * double(width) This turns length and width to doubles for this calculation

9 NumberFormat Class Imported with the line: import java.text.NumberFormat; Example: NumberFormat money = new NumberFormat.getCurrencyInstance(); This line sets up an object used to format numbers into money Example : System.out.println(money.format(dollars)); This will take a variable named dollars and print it to the screen as a currency with the format $X.XX

10 Assignment Operators In an assignment statement the variable name on the left side of the = sign is given the value of the expression on the right. Examples: intPlayer = 12; intPlayer = intPlayer + 2;

11 Constants A constant is a name used to store a value that does not change Should be written in all uppercase letters It is declared as follows: final Example: final double PI; A constant can only be assigned a value once. Trying to change the value of a constant will result in an error Constants are declared at the beginning of methods and classes before any variables

12 Identifiers and Keywords Identifiers and keywords are words reserved by the java language These words cannot be used as variable or constant names There is a list of 51 words that java uses as identifiers and keywords. The list can be found on page 88. Examples: for, if, while, goto, int, double, etc


Download ppt "Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:"

Similar presentations


Ads by Google